在ASP.NET 4.5中将值设置为HiddenField [英] Setting a value to a HiddenField in ASP.NET 4.5

查看:66
本文介绍了在ASP.NET 4.5中将值设置为HiddenField的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在ASP.NET 4.5中将一个值设置为HiddenField时遇到了一些问题。

I'm having some issues setting a value to a HiddenField in ASP.NET 4.5.

从我看到的情况来看,我已经尝试了以下内容而没有任何内容运气:

From what I've seen I've tried the following without any luck:

在ASPX中:

<asp:HiddenField ID="HiddenField" runat="server" value="" />
<script type="text/javascript">
    function SetHiddenField() {
        var vv = "HELLO WORLD";
        document.getElementById('<%=HiddenField.ClientID%>').value = vv;            
    }
</script>

在代码隐藏中:

ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "SetHiddenField", "SetHiddenField();", true);
ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "alert", "alert('" + HiddenField.ClientID + "');", true);

这会在ClientID中提醒垃圾。

This alerts garbage in the ClientID.

我尝试的其他解决方案如下。

The other solution I've tried is the following.

在.ASPX中:

<asp:HiddenField ID="HiddenField" runat="server" value="" />
<script type="text/javascript">
    function SetHiddenField() {
        var vv = "HELLO WORLD";
        document.getElementById('HiddenField').value = vv;            
    }
</script>

这里的一个问题是 .value IntelliSense中不存在,只有 .ValueOf

One issue here is that .value does not exist in the IntelliSense, only .ValueOf.

在代码隐藏中:

ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "SetHiddenField", "SetHiddenField();", true);
ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "alert", "alert('" + HiddenField.Value + "');", true);

没有任何反应,可能是JavaScript中的错误,因为没有显示警报。

Nothing happens, probably an error in the JavaScript, since no alert is shown.

有人能指出我正确的方向吗?

Can anyone point me to the right direction, please?

推荐答案

你的第一个标记是好:

<asp:HiddenField ID="HiddenField" runat="server" value="" />
<script type="text/javascript">
    function SetHiddenField() {
        var vv = "HELLO WORLD";
        document.getElementById('<%=HiddenField.ClientID%>').value = vv;
    }
</script>

将代码更改为此(查看第二行):

Change the code to this (check the second line):

 ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "SetHiddenField", "SetHiddenField();", true);
 ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "alert", "alert(document.getElementById('" + HiddenField.ClientID + "').value);", true);

输出应如下所示:

编辑:在您的方案中,您可以运行javascript来获取值并强制回发以使用代码中的值。我会将我的标记更改为:

EDIT : In your scenario, you can run javascript to get a value and force a postback to use the value in your code. I would change my markup to this:

<script type="text/javascript">
    function SetHiddenField() {
        var vv = "HELLO WORLD";
        document.getElementById('<%=HiddenField.ClientID%>').value = vv;
        __doPostBack('<%=HiddenField.ClientID%>', '')
    }
</script>

在代码中,我的Page_Load如下所示:

And in code my Page_Load is like below:

protected void Page_Load(object sender, EventArgs e)
{

    if (!IsPostBack)
    {
        // Register JavaScript which will collect the value and assign to HiddenField and trigger a postback
        ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "SetHiddenField", "SetHiddenField();", true); 
    }
    else 
    {
        //Also, I would add other checking to make sure that this is posted back by our script
        string ControlID = string.Empty;
        if (!String.IsNullOrEmpty(Request.Form["__EVENTTARGET"]))
        {
            ControlID = Request.Form["__EVENTTARGET"];
        }
        if (ControlID == HiddenField.ClientID) 
        { 
            //On postback do our operation
            string myVal = HiddenField.Value;
            //etc...
        }
    }

}

这篇关于在ASP.NET 4.5中将值设置为HiddenField的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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