assign values from javascript and get values from c# - javascript

On the btn1 click:
<input type="hidden" runat="server" id="HTxtPath" />
or
<asp:HiddenField runat="server" ID="HTxtPath" />
<input id="btn1" type="button" value="save" onclick="Save()">
function Save() {
document.getElementById('HTxtPath').value = "~/path/Order.pdf";
}
Upto here the values assigning to the hidden text, then
On the btn2 click:
<input id="btn2" runat="server" type="button" value="save" onclick="Get()">
C#:
String data = HTxtPath.Value;
Here i m not getting the value
Pls help me

The easiest way I can do is to make the hidden input and asp.net hidden control instead of normal html input

used asp hidden field to store value
asp:HiddenField ID="hdnResultValue" Value="0" runat="server"
document.getElementById("hdnResultValue").value = 123;
following line read hidden field value
string codeBehindValue = hdnResultValue.Value;

Related

How to reset form fields the elegant way with javascript or jquery?

How do I reset the form fields more elegant compared to the code below? The code is working. But if I have lots of form fields it does not look so nice. Is there a more compact way?
<script>
function setSearchCriteria(){
var searchcriteria = document.getElementById("TherapistSearch-SearchCriteria");
searchcriteria.value = "";
}
function setCity(){
var city = document.getElementById("TherapistSearch-City");
city.value = "";
}
function setLastName(){
var lastname = document.getElementById("TherapistSearch-LastName");
lastname.value = "";
}
function setFirstName(){
var firstname = document.getElementById("TherapistSearch-FirstName");
firstname.value = "";
}
}
</script>
<input type="button" class="btn btn-default" value="Reset form"
onclick="javascript:clearForms();javascript:setSearchCriteria();setCity();setLastName();setFirstName();" title="Reset form"/>
Give a css class to your input fields.
<input type="text" id="firstName" class="resettable" />
<input type="text" id="firstName" class="resettable" />
<select id="states" class="selectResettable">
<option value="0">Select an option </option>
<option value="1">Michigan</option>
<option value="2">Ohio</option>
<select>
<input type="button" id="resetBtn" />
And use this css class(es) as jQuery selector(s) to get all of this inputs and set the value to empty/default value. I also removed the onclick from button HTML markup as we are going to do it in the unobtrusive javascript way.
Add this javascript where we are registering the code for the click event on our button.
$(function(){
$("#resetBtn").click(function(e){
alert("Reset button clicked");
//Set the input text fields to empty string
$("input.resettable").val("");
//Reset the dropdowns.
$(".selectResettable").val("0")
//If you want to do something else, Do it here.
});
});
Simply use HTML the way it's meant to be used, and take advantage of the reset button:
<input type="reset" class="btn btn-default" value="Reset form" title="Reset form"/>
This requires no JavaScript, no additional functionality, and is a native component of HTML forms. Using type="button" gives the element the appearance of a button, but strips the default functionality; using type="reset" gives the appearance of a button and gives the default functionality of resetting the parent <form> element to its default page-load state.
References:
<input> element type attribute-values.
<input type="reset" id="resetBtn" class="resettable" />
use reset type of button or call reset method on form as below
<input type="button"
class="btn btn-default"
value="Reset form"
onclick="document.getElementById('myForm').reset();"
title="Reset form"/>

Hide submit type input value from url

I have a form with GET method with some inputs like this:
<input type="text" name="something">
<input type="submit" name="submit" value="Click here to submit form">
I don't want the url to become
mypage.php?something=value&submit=Click+here+to+submit+form
I want to suppress the submit parameter in the url (should be like this: mypage.php?something=value).
I need only GET method please don't mention POST method.
Also you may mention to remove name="submit" but i need it also to identify this submit.
Also if there is a input from which is empty i don't want to show
<input type="text" name="input1">
<input type="text" name="input2">
<input type="submit" name="submit" value="Click here to submit form">
input1 gets value but if input2 doesn't have value the i want url like
mypage.php?input1=value
Thanks
If you don't want the submit parameter to show up in your GET request URL, don't give it a name, and instead use id or class to identify it uniquely:
<input type="submit" id="submit" value="Click here to submit form">
I would remove form submit the and just turn it into a button. Then I would use jquery to submit the form and do any logic processing.
<input type="test" id="favoriteCheese" value="nacho" />
<button id="submit" value="Click here to submit form">Click here to submit form</button>
$("#submit").on('click', function () {
var data = {};
var favCheese = $("#favCheese").val();
if(favCheese.length > 0) {
data.favCheese = favCheese
}
$.ajax({
url : ".../mypage.php",
data : data,
type : 'GET',
...
})
})

JavaScript - retain "textbox2" value for it to be available after refresh/reopen page

I have 2 textboxes. After writing something in the first one and clicking the OK button the value appears in textbox2 also.
I need that value to be saved after page refresh and not be modified untill someone introduces a new value in textbox1 and hits OK again.
<html>
<head> </head>
<script type="text/javascript">
function myfunction()
{
var first = document.getElementById("textbox1").value;
var textbox2 = document.getElementById("textbox2");
textbox2.value = first;
}
</script>
<body>
<input type="text" name="textbox1" id="textbox1" />
<input type="submit" name="button" id="button1" onclick="myfunction()" value="OK" />
<br/>
Your answer is:
<input type="text" name="textbox2" id="textbox2" readonly="true"/>
</body>
</html>
As per my knowledge, you can make the "Textbox2" a server control by declaring runat="server" which would retain the value even after server hit as shown below.
<input type="text" name="textbox2" id="textbox2" readonly="true" runat="server" />
Then store it in session (in case of C#) or something similar in the server side to restore the value.
Hope this Helps!!
This should do it every time. http://jsfiddle.net/CKLAg/
first, simplify your html a bit:
<input type="text" name="textbox1" id="textbox1">
<button name="button" id="button1" onclick="myFunction();">OK</button>
<br/>Your answer is:
<input type="text" name="textbox2" id="textbox2" readonly="true">
Then, add the following functions to check for localStorage, save and load storage on demand:
Note, that you will have to use jQuery to get the document ready state and set the input element when the DOM is ready.
$('document').ready(function(){
var prevAnswer = loadStorage;
$('#textbox2').attr('value', prevAnswer);
});
function loadStorage() {
if (supports_html5_storage) {
if (localStorage.getItem('myAnswer')) {
var answer = localStorage.getItem('myAnswer');
return answer;
}
}
}
function myFunction() {
var first = document.getElementById("textbox1").value;
var textbox2 = document.getElementById("textbox2");
textbox2.value = first;
if (supports_html5_storage) {
alert('storing');
localStorage['myAnswer'] = first;
}
}
function supports_html5_storage() {
try {
return 'localStorage' in window && window['localStorage'] !== null;
} catch (e) {
return false;
}
}

Python mechanize - selecting a value and submiting doesn't work

The HTML code looks like this (its a field with a button next to it, where you can select a value):
<input type="text" name="account_name" class="sqsEnabled" tabindex="0" id="account_name" size="" value="" title='' autocomplete="off">
<input type="hidden" name="account_id" id="account_id" value="">
<button type="button" name="btn_account_name" id="btn_account_name" tabindex="0" title="Select Account" class="button firstChild" value="Select Account"
onclick='open_popup("Accounts", 600, 400,"",true, false,{"call_back_function":"set_return","form_name":"EditView","field_to_name_array":"id":"account_id","name":"account_name"}},
"single", true);' >
.....
#the submit button
<input title="Save" accesskey="a" class="button primary" onclick="var _form = document.getElementById('EditView'); _form.action.value='Save'; if(check_form('EditView'))SUGAR.ajaxUI.submitForm(_form);return false;" type="submit" name="button" value="Save" id="SAVE_HEADER">
When I select the value the HTML modify a previous line which look like this:
<input type="hidden" name="account_id" id="account_id" value="30a4f430-5b15-8d7f-632a-52723fb0921a">
and in the input with name="account_name" has a string value "XXX LTD"
So I figure if will do this, I will submit the form successfully:
br.select_form(nr=0)
br.form.set_all_readonly(False)
form = br.form
form['account_name'] = "XXX LTD"
form['account_id'] = "30a4f430-5b15-8d7f-632a-52723fb0921a"
#other forms ...
response = br.submit()
There is no error from Python, but the form isn't submitted. Any ideas? Thanks in advance
The problem is that you create a dict form and doesn't use it, try:
form = br.form
form['account_name'] = "XXX LTD"
form['account_id'] = "30a4f430-5b15-8d7f-632a-52723fb0921a"
br.form = form # <--- here
response = br.submit()
Mechanize doesn't support JavaScript. Why do you have this tag?
You're not selecting form. Reaplce your for selection with:
br.select_form(nr=0)

How to retrieve hidden field value using javascript?

I've asp.net web site , I used master page for the design. I've child page which is placed in the contentplaceholder. On the child page i used one hidden field as -
<input id="Hidden1" type="hidden" value="This is hidden text"/>
I want to display the hidden field value using alert() function from javascript on the page load event. How to do this?
I tried following thing in my script but it is not working-
(function msgShow() {
var e1 = document.getElementById('Hidden');
alert(e1.value);
})();
Thanks.
window.alert(document.getElementById("Hidden1").value);
Make sure this code is executed after the DOM is ready.
With jQuery you do like this:
$(document).ready(function() {
alert($('#Hidden1').val());
});
without jQuery you do:
alert(document.getElementById('Hidden1').value);
Just like with any other element, you can get it with document.getElementById('Hidden1').value
Refer the code given below to know how to get
<html>
<body>
<script type="text/javascript">
function printIt(){
alert(document.getElementById('abcId').value);
alert(document.formName.elements['abcName'].value);
}
</script>
<h1>Access Hidden value in JavaScript</h1>
<form name="formName">
<input type="hidden" id="abcId" name="abcName"
value="I am Hidden value"/>
<input type="button" value="Get Value" onclick="printIt()" />
</form>
</body>
</html>
document.getElementById('Hidden1').value;
and alert the return value
<script type="text/javascript">
function dis() {
var j = document.getElementById("<%= Hidden1.ClientID %>").value;
alert(j);
}
</script>
<input id="Hidden1" type="hidden" runat="server" value="Hello" /><br />
<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="return dis();" />
With pure JavaScript:
var value = document.getElementById(id).value;
Also be sure not to reference a DOM element before it exists - like I just did and spent an hour trying to figure why even HelloWorld would not work.

Categories

Resources