HiddenField 的值未更新
我已经尝试过这两种方法:
<asp:HiddenField ID = "selectedHour" runat="server" Value="blahblah" />
<input type="hidden" id="myHour" name="hour" Value="blahblah" runat="server"/>
我尝试用Javascript更新它:
<script type="text/javascript">
function addEventByClick(hour) {
document.getElementById("myHour").Value = hour;
alert(document.getElementById("myHour").Value);
document.getElementById("dummyButton").click();
}
</script>
它“有效”:警报给了我正确的数字。
然后,当我单击“提交”时,它会调用一个 C# 方法(通过单击 asp.net 组件来调用),该方法执行以下操作:
String h = myHour.Value;
//or
//String h = Request.Form["myHour"];
并且始终返回“blahblah”,即初始值。
所有这些东西都在一个更新面板中,但它在同一个更新面板中,都在同一个 ContentTemplate 中。
那么为什么不更新呢?
编辑:谢谢大家。我讨厌当我得到 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...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试使用
value
而不是Value
。浏览器对这些事情很挑剔。或者,使用 jQuery,你的问题就会神奇地消失:
$('#myobject').val( 'new value ');
Try using
value
instead ofValue
. Browsers are picky about these things.Alternatively, use jQuery, and your problems magically disappear:
$('#myobject').val( 'new value' );
对于原始 html,尝试使用非大写值:
try with uncapitalized Value, for the raw html:
JavaScript 不区分大小写。
尝试:
javascript is not case-sensitive.
try: