3

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

Jeremy
  • 5,365
  • 14
  • 51
  • 80
  • What does this do? -> `document.getElementById("dummyButton").click()` – R1234 Jun 09 '11 at 18:35
  • @Rohan It simulates clicking a button, and works perfectly. I'm using it to open a modal pop-up since there's multiple ways of the user getting that pop-up. – Jeremy Jun 09 '11 at 18:39
  • not sure though because `.click()` should technically work on a jquery object not on a DOM object as you have selected. – R1234 Jun 09 '11 at 18:41

3 Answers3

3

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

Alternatively, use jQuery, and your problems magically disappear:

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

Stefan Kendall
  • 66,414
  • 68
  • 253
  • 406
  • I... I can't believe that worked. I was seriously working on this for 30 minutes. – Jeremy Jun 09 '11 at 18:32
  • @Xaisoft: Even HTML is, too, occasionally, depending on your browser. Using jQuery eliminates the headache of things "magically not working". You can't mis-use the `.val` method. Every misspelling throws an explicit error, as opposed to silently failing. – Stefan Kendall Jun 09 '11 at 18:39
  • People have been suggesting jquery forever. I'm not really feeling comfortable with asp.net yet, but I guess it's time to take the plunge. – Jeremy Jun 09 '11 at 18:41
  • 1
    @Jeremy, asp.net and jquery are two completely different things. Stefan is talking about the encapsulation jquery gives you over raw javascript. – Xaisoft Jun 09 '11 at 19:20
  • 1
    @Xaisoft I was saying that I'm not very comfortable with the new technologies I'm learning (asp.net, javascript, html) and the last thing I want to do right now is add something else I don't quite know how to use. – Jeremy Jun 09 '11 at 19:30
  • @Jeremy, don't worry, it will come with time. Just remember, everyone was a beginner at one moment in time. – Xaisoft Jun 09 '11 at 19:32
2

try with uncapitalized Value, for the raw html:

document.getElementById("myHour").value = hour
m2o
  • 6,475
  • 6
  • 27
  • 24
2

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
The Mask
  • 17,007
  • 37
  • 111
  • 185