开发者

Value of a HiddenField not updating

开发者 https://www.devze.com 2023-03-11 23:52 出处:网络
I\'ve tried both of these: <asp:HiddenField ID = \"selectedHour\" runat=\"server\" Value=\"blahblah\" />

I've tried both of these:

    <asp:HiddenField ID = "selectedHour" runat="server" Value="blahblah" />
    <input type="hidden" id="myHour" name="hour" Value="blahblah" runat="server"/>

And I try to update it with Javascript:

     <script type="text/javascript">
      function addEventByClick(hour) {
        document.getElementById("myHour").Value = hour;
        alert(document.getElementById("myHour").Value);
        document.getElementById("dummyButton").click();
      }
     </script>

which "works": the alert gives me the correct number.

Then, when I click submit it calls a C# method (called by clicking an asp.net component), which does this:

String h = myHour.Value;
//or
//String h = Request.Form["myHour"];

and this always returns "blahblah", that is, the initial value.

All of this stuff is in an up开发者_开发百科date panel, but it's in the SAME update panel, all within the same ContentTemplate.

So why isn't it updating?

Edit: Thanks guys. I hate when I get 3 perfect answers, how do I know which one to check...


Try using value instead of Value. Browsers are picky about these things.

Alternatively, use jQuery, and your problems magically disappear:

$('#myobject').val( 'new value' );


try with uncapitalized Value, for the raw html:

document.getElementById("myHour").value = hour


javascript is not case-sensitive. try:

replace document.getElementById("myHour").Value = hour; by 
        document.getElementById("myHour").value = hour; and 

document.getElementById("myHour").Value by 
document.getElementById("myHour").value
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号