How to get HiddenField value in asp.net code-behind

浪子不回头ぞ 提交于 2019-11-29 15:19:59

The client ID isn't necessarily the same as the server ID (unless you're using CliendIDMode=Static. You can insert a server tag to get the client ID.

Note also that you have to put the script inside a document.ready tag, or put the script at the bottom of the page -- otherwise the script won't find HiddenField1, as it will not have been loaded into the DOM yet.

$(document).ready(function() {
    $("<%= HiddenField1.ClientID %>").val("123");
});

Try :

$("#<%= HiddenField1.ClientID %>").val("123");

And in .cs file:

string b= HiddenField1.Value;

Your issue is on how you set it.

$("#<%=HiddenField1.ClientID%>").val("123");

You need to use the rendered control id.

Follow up. This code

  protected void Button1_Click(object sender, EventArgs e)
        {
            this.ClientScript.RegisterStartupScript(this.GetType(), "MyClick ", "<script>ReadCard();</script> ");
            string b= HiddenField1.Value; //How to get the value "123"??
        }

is actually the same as :

  protected void Button1_Click(object sender, EventArgs e)
        {
            HiddenField1.Value = "123";
        }

Because you actually you try to set the value with registering a javascript code, but why ? you can direct set that value from code behind.

Where do you really wont to get that value ?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!