Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
209 views
in Technique[技术] by (71.8m points)

c# - Setting a value to a HiddenField in ASP.NET 4.5

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:

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

In code-behind:

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

This alerts garbage in the ClientID.

The other solution I've tried is the following.

In .ASPX:

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

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

In code-behind:

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

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

Can anyone point me to the right direction, please?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your first markup is good:

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

And the output should be like this:

enter image description here

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>

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

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...