I have an asp page and I have set the hidden field value through the javascript. Now I need to fetch the value of hidden field in the server side, but it returns the blank. Can anyone tell me how to get the value of hidden field in the server side.
Set the value of hidden field
<input type="hidden" id="hdnMy" name="hdnMy" class="hdnMy" runat="Server" />
in the javascript file
$(document).ready(function () {
$(".hdnMy").val("Pankaj");
});
Now need to fetch the value of Page_load event
protected void Page_Load(object sender, EventArgs e)
{
var value = hdnMy.value;
}
I have an asp page and I have set the hidden field value through the javascript. Now I need to fetch the value of hidden field in the server side, but it returns the blank. Can anyone tell me how to get the value of hidden field in the server side.
Set the value of hidden field
<input type="hidden" id="hdnMy" name="hdnMy" class="hdnMy" runat="Server" />
in the javascript file
$(document).ready(function () {
$(".hdnMy").val("Pankaj");
});
Now need to fetch the value of Page_load event
protected void Page_Load(object sender, EventArgs e)
{
var value = hdnMy.value;
}
Share
Improve this question
edited Feb 5, 2015 at 7:07
Pankaj Saha
asked Feb 5, 2015 at 6:29
Pankaj SahaPankaj Saha
9693 gold badges19 silver badges38 bronze badges
1
- stackoverflow./questions/13600109/… – Sadikhasan Commented Feb 5, 2015 at 6:34
4 Answers
Reset to default 2You can get its value by using its Value property.
this.hdnMy.Value
$(".hdnMy") is a class Selector returns an array of dom objects. So either you should use :
$("#hdnMy").val("Pankaj")
or
$(".hdnMy").each(function(){
$(this).val("Pankaj");
});
In server side you should be able to get it by name of the field.
string hdnName= Request.Form["hdnName"].ToString();
HTH!
You can do it in different ways
1) var Hiddenvalue= document.getElementById('<%= idofhiddenfield.ClientID%>');
2) Assign value to your Hidden field like below
$("#<%=idofhiddenfield.ClientID%>").val("value");
Then code behind you can call it like
string value=idofhiddenfield.value;
3)Or you can pass it as a querystring then take it in code behind
4) or add ClientIDMode="Static"
to hiddenfield
then do something like you had done in your code
like
<input type="hidden" id="hdnMy" name="hdnMy" class="hdnMy" runat="Server" ClientIDMode="Static" />
then the below code will work
$(document).ready(function () {
$(".hdnMy").val("Pankaj");
});
Once the form is submitted to the server there is no difference between hidden fields and normal fields.
ment: use the id
instead of class
if you want to set the value of one input field.
To fetch the value:
var val = $("#hdnMy").val()