I'm having trouble setting the value for the HiddenField in ASP.NET 4.5.
From what I saw, I tried the following with no 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 the coding:
ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "SetHiddenField", "SetHiddenField();", true); ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "alert", "alert('" + HiddenField.ClientID + "');", true);
This prevents garbage in ClientID.
Another solution I 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 problem is that .value does not exist in IntelliSense, only .ValueOf .
In the coding:
ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "SetHiddenField", "SetHiddenField();", true); ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "alert", "alert('" + HiddenField.Value + "');", true);
Nothing happens, maybe a bug in JavaScript, because a warning is not displayed.
Can someone point me in the right direction please?