Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing HiddenField value with Javascript not changing in C# codebehind

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 ?

like image 445
John Doe Avatar asked Sep 24 '26 07:09

John Doe


1 Answers

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.

like image 80
jbl Avatar answered Sep 26 '26 22:09

jbl



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!