最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - Value of a HiddenField not updating - Stack Overflow

programmeradmin1浏览0评论

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 ponent), 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...

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 ponent), 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...

Share Improve this question edited Jun 9, 2011 at 18:39 Jeremy asked Jun 9, 2011 at 18:25 JeremyJeremy 5,43315 gold badges53 silver badges82 bronze badges 3
  • What does this do? -> document.getElementById("dummyButton").click() – R1234 Commented Jun 9, 2011 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 Commented Jun 9, 2011 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 Commented Jun 9, 2011 at 18:41
Add a ment  | 

3 Answers 3

Reset to default 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' );

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)

  1. 暂无评论