如何在asp.net $ C $得到HiddenField值c-背后 [英] How to get HiddenField value in asp.net code-behind

查看:98
本文介绍了如何在asp.net $ C $得到HiddenField值c-背后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获得HiddenField值asp.net code-背后?在此先感谢!

 公共部分类ReadCard:System.Web.UI.Page
    {
        保护无效的Page_Load(对象发件人,EventArgs的发送)
        {
            Response.Cache.SetCacheability(System.Web.HttpCacheability.NoCache);        }        保护无效的button1_Click(对象发件人,EventArgs的发送)
        {
            this.ClientScript.RegisterStartupScript(this.GetType(),了MyClick,&所述;脚本> ReadCard();&下; /脚本>中);
            字符串B = HiddenField1.Value; //如何获取值123?
        }
    }

ASPX:

 < HTML的xmlns =htt​​p://www.w3.org/1999/xhtml>
<头=服务器>
    <标题>< /标题>
    < META HTTP-EQUIV =过期的内容=0/>
    < META HTTP-EQUIV =缓存控制内容=无缓存/>
    < META HTTP-EQUIV =编译内容=无缓存/>
    <脚本SRC =jQuery的-1.5.2.min.js类型=文/ JavaScript的>< / SCRIPT>
         <脚本类型=文/ JavaScript的>
             功能ReadCard(){
                 $(#HiddenField1)VAL(123)。
             }
        < / SCRIPT>
< /头>
<身体GT;
    <表ID =form1的=服务器>
    < ASP:HiddenField ID =HiddenField1=服务器/>
    < ASP:按钮的ID =Button1的=服务器文本=按钮的onclick =的button1_Click/>
    < /表及GT;
< /身体GT;
< / HTML>


解决方案

客户端ID不一定是相同的服务器ID(除非你使用 CliendIDMode =静态,您可以将一台服务器标签获取客户端ID。

还请注意,你必须把脚本中的的document.ready 标签里面,或者把脚本在页面的底部 - 否则脚本将不会发现HiddenField1,因为它不会被加载到DOM中还

  $(文件)。就绪(函数(){
    $(&下;%= HiddenField1.ClientID%gt;中)。VAL(123);
});

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

  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>

解决方案

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");
});

这篇关于如何在asp.net $ C $得到HiddenField值c-背后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆