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

前端之家收集整理的这篇文章主要介绍了jquery – 如何在asp.net代码隐藏中获取HiddenField值前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
如何在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:

<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>

解决方法

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

另请注意,您必须将脚本放在document.ready标记内,或将脚本放在页面底部 – 否则脚本将找不到HiddenField1,因为它尚未加载到DOM中.

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

猜你在找的jQuery相关文章