Jeremy
Jeremy

Reputation: 5435

Value of a HiddenField not updating

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 update 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...

Upvotes: 3

Views: 3571

Answers (3)

The Mask
The Mask

Reputation: 17427

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

Upvotes: 2

Stefan Kendall
Stefan Kendall

Reputation: 67802

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

Alternatively, use jQuery, and your problems magically disappear:

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

Upvotes: 3

m2o
m2o

Reputation: 6684

try with uncapitalized Value, for the raw html:

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

Upvotes: 2

Related Questions