Setting a value for HiddenField in ASP.NET 4.5

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?

+6
source share
2 answers

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 result should be like this:

enter image description here

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

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

And in code, my Load_page looks like this:

 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... } } } 
+6
source

In the hidden field tag add clientid static like this -

 <asp:HiddenField ID="HiddenField" runat="server" value="" ClientIDMode="Static" /> 

Thus, ASP.Net will not replace it with a dynamic identifier and will always have the identifier that you provided, so now it will have the HiddenField identifier. Then your second attempt should work.

More details can be found here -

http://msdn.microsoft.com/en-us/library/system.web.ui.control.clientidmode(v = vs .110) .aspx

+4
source

Source: https://habr.com/ru/post/970905/


All Articles