最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to get the value of hidden field in the server side - Stack Overflow

programmeradmin3浏览0评论

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
Add a ment  | 

4 Answers 4

Reset to default 2

You can get its value by using its Value property.

this.hdnMy.Value 
  1. $(".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()
发布评论

评论列表(0)

  1. 暂无评论