Changing a HiddenField value with Javascript not changing in C # codebehind

I am trying to change a hidden field using javascript and then use this changed value in my code. I have a breakpoint in Page_Load to check if the value of HiddenField1 has changed, but it always remains 0 upon postback.

<script type="text/javascript"> $(document).ready(function () { var hiddenControl = '<%= HiddenField1.ClientID %>'; var s = $('#cbox'); $("#cbox").combobox({ selected: function (event, ui) { alert(s.val()); document.getElementById(hiddenControl).value = s.val(); alert(document.getElementById(hiddenControl).value); } }); }); <asp:HiddenField ID="HiddenField1" runat="server" EnableViewState="False" Value="0" /> 

If I can't get this to work, is there any other method for passing information between javascript and C # codebehind?

+4
source share
6 answers

You might want to mark the client ID of the HiddenField client, and then look at the corresponding value on the server side of the Request.Form collection during postback.

This way you check the value sent to the server. If the value is correct, the problem may occur because something violates the ProcessPostData (for example, manual reset or dynamic change in the organization of the form)

Although it is difficult to give advice without all the code, I agree that EnableViewState = false is surprising.

+1
source

See sample MS documentation for HiddenField:

 Form1.HiddenField1.value = s.val(); 

At the bottom of this page is http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.hiddenfield.aspx

0
source

try the same task using the html hidden variable

  <input id="hdName" runat="server" type="hidden" /> 

Since we provided runat = "server", we can access this variable on the server side, and by hdName.Value we can get the value on the server side. Thought it was a html control that we need to provide hdName.ClientID in javascript since we gave runat = server.

 document.getElementById('<%= hdName.ClientID %>').value 

I hope this solves your problem and gives the desired result. Thanks

0
source

Although you can always use the set get method to store values ​​from a java script ... But here is some simple way to solve it ...

case 1 - If the control is intended only to store the target,
put display: there is no style in it and make visible = true in the attribute of the control.

case 2 - if control should be displayed, but in disabled mode, set ReadOnly = "true" instead

  eg.. <asp:textbox runat="server" ID="textbox1" Enabled="false"></asp:textbox> will not work <asp:textbox runat="server" ID="textbox2" ReadOnly="true"></asp:textbox> will work 
0
source

Put your HiddenField inside the UpdatePanel and use the HiddenField with the ClientID.

0
source

My answer is similar to what others said to get a hidden variable / control that can be passed back to the server side code, like as a Button or Page_Load event, etc. I got an idea from them. But here it is:

 <asp:TextBox ID="hdnErrorMsg" ClientIDMode="Static" style="display: none;" runat="server"/> 

This can be installed in JavaScript, and it will return to the server side of Asp.NET and will be used and verified. If you want to save it when it returns to the customer, you may have to return it and all other things that you need. In my case, this is an error message. I wanted him to stop at the ASP.NET button click event to save and exit, but I needed to return an error message so that when he returned to the client he was there again.

0
source

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


All Articles