jQuery 如何在asp.net代码隐藏中获取HiddenField值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13600109/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 12:47:20  来源:igfitidea点击:

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

jqueryasp.netjquery-1.5

提问by user441222

How to get HiddenField value in asp.net code-behind? Thanks in advance!

如何在 asp.net 代码隐藏中获取 HiddenField 值?提前致谢!

  public partial class ReadCard : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Response.Cache.SetCacheability(System.Web.HttpCacheability.NoCache);

        }

        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"??
        }
    }

aspx:

ASP:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <meta http-equiv="expires" content="0"/>
    <meta http-equiv="cache-control" content="no-cache"/>
    <meta http-equiv="pragma" content="no-cache"/>
    <script src="jquery-1.5.2.min.js" type="text/javascript"></script>
         <script type="text/javascript">
             function ReadCard() {
                 $("#HiddenField1").val("123");
             }
        </script>
</head>
<body>
    <form id="form1" runat="server">
    <asp:HiddenField ID="HiddenField1" runat="server" />
    <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
    </form>
</body>
</html>

回答by McGarnagle

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.

客户端 ID 不一定与服务器 ID 相同(除非您使用的是CliendIDMode=Static。您可以插入服务器标记来获取客户端 ID。

Note also that you have to put the script inside a document.readytag, 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标签内,或将脚本放在页面底部——否则脚本将找不到 HiddenField1,因为它尚未加载到 DOM 中。

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

回答by Aristos

Your issue is on how you set it.

你的问题在于你如何设置它。

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

You need to use the rendered control id.

您需要使用呈现的控件 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.

因为您实际上尝试通过注册 javascript 代码来设置该值,但为什么呢?您可以直接从后面的代码中设置该值。

Where do you really wont to get that value ?

你真的不会在哪里得到那个价值?

回答by Shree

Try :

尝试 :

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

And in .cs file:

在 .cs 文件中:

string b= HiddenField1.Value;