3

I'm trying to change a hidden field with javascript and then use that changed value in my codebehind. I have a breakpoint in Page_Load to check if the value of HiddenField1 has changed but it always remains 0 on 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 to pass information between javascript and c# codebehind ?

11
  • Is it a page lifecycle issue? Is the value updated in Page_PreRender? Commented Mar 3, 2012 at 2:17
  • Value is still the default value of 0 in Page_PreRender Commented Mar 3, 2012 at 2:57
  • could you post the html that is generated? Commented Mar 3, 2012 at 3:15
  • Why did you disable ViewState? Was it causing problems? Or were you just trying everything to prevent the hidden field from being lost? Commented Mar 3, 2012 at 3:44
  • Was trying everything to prevent the hidden field from being lost. Thought viewstate might be resetting it to the default value of 0. Commented Mar 3, 2012 at 3:50

6 Answers 6

1

You may want to note the client side ClientID of you HiddenField and then look at the corresponding value in the Request.Form collection server side during postback.

This way you will check the value sent to the server. If the value is correct, the problem might occur because something disturbs ProcessPostData (manual resetting or dynamic change of form organization for example)

Though it is difficult to give an advice without the whole code, I agree with those saying that EnableViewState=false is surprising.

Sign up to request clarification or add additional context in comments.

Comments

0

Check out the example on the MS documentation for HiddenField:

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

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

3 Comments

Tried that but says Form1 is undefined. Also I don't think it matters since in the question code I'm referencing it by the ID, and searching the document for it.
form id is form1 but there's a master page involved so might be more nesting ?
Is there a chance that this field might not even be in form1? Can you test this by setting it to something else on the server (and posting that back)
0

try to do the same task using html hidden variable

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

As we have provided runat="server" we will be able to access this variable on server side and on hdName.Value we can get the value on server side. Thought it is html control we need to provide hdName.ClientID in javascript as we have given runat=server.

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

Hopefully this will solve your issue and give you desired result. Thanks

2 Comments

Same result as the asp hidden field
Can you please check the name of control from the page source and share here in here for html hidden field .
0

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

case 1 - If control is just for storing purpose ,
put display : none in it's style and make visible = true in the attribute of the control.

case 2 - if control is to be displayed but in disabled mode put ReadOnly="true" instead of

   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

Comments

0

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

Comments

0

My answer is similar to what others said to get a hidden variable/control that can be passed back to the Server Side Code Behind like Button Event or Page_Load, etc. I got the idea from them. But here it is:

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

This can be set in JavaScript, and it will return to Server Side Asp.NET and be used and examined. If you want to preserve it when it goes back to the client, you may have to put it back and all other things that you need. In my case it was an error message. I wanted it to stop in the ASP.NET button click event for the save and exit, but I needed to put the error stuff back so that when it went back to the client it would be there again.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.