ASP.NET:JavaScript 在提交之前修改 HiddenField 的值
我在 Stack Overflow 上查看过类似的问题,但无法找到这个特定问题的解决方案。我有一个简单的(不是真的,但让我们假装)ASP.NET 页面,其中有一个更新面板。
在此更新面板中,有一个 HiddenField
控件,其值将通过我自己的自定义 JavaScript 设置/更新。正如我使用 Firebug 在 DOM 中看到的那样,值已更新。
在此 HiddenField
旁边有一个 LinkButton
,它调用 JavaScript 使用 OnClientClick
修改 HiddenField
的值(这有效)。 LinkButton
还有一个 OnClick
处理程序,该处理程序在修改值后执行。
但是,在 OnClick
处理程序中,我获得了 HiddenField
的初始值,而不是 JavaScript 设置的值!
这是为什么呢?这是“PEBKAC”的情况吗?
编辑:使用TextBox
尝试了同样的操作,但没有成功。还尝试在提交表单之前使用 Firebug 手动修改该值,但没有成功。
编辑2:我刚刚意识到Page_Load
是在OnClick
处理程序之前调用的,这把事情搞砸了。我不会删除这个问题,因为其他人可能也有同样的问题,并且可能会发现这个问题很有价值。
I have looked at similar questions on Stack Overflow but haven't been able to find the solution to this particular problem. I have a simple (not really, but let's pretend) ASP.NET page with an update panel inside it.
In this update panel there is a HiddenField
control with a value that will be set/updated via my own custom JavaScript. The value is updated as I can see in the DOM using Firebug.
Next to this HiddenField
there is a LinkButton
which calls the JavaScript to modify the value of the HiddenField
using OnClientClick
(this works). The LinkButton
also has an OnClick
handler which is executed after the value has been modified.
However, in the OnClick
handler, I get the initial value of the HiddenField
and not the value which was set by JavaScript!
Why is this? Is this a case of "PEBKAC"?
EDIT: Tried the same thing with a TextBox
without any success. Also tried modifying the value manually using Firebug before submitting the form, without any success.
EDIT 2: I just realised that Page_Load
is called before the OnClick
handler, which was messing things up. I will not delete the question as other people may have the same problem and may find this question valuable.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在我看来,这似乎是视图状态给你开了一个糟糕的玩笑(就像经常发生的那样)。我的猜测是,当页面回发时,隐藏字段将使用 Viewstate 中包含的值进行初始化。
尝试一件事:不要直接通过
yourHiddenField.Value
访问该值,而是在页面的 OnInit 中使用Request.Forms[yourHiddenField.UniqueId]
获取该值并进行转换到 HiddenField 并提取值。我不确定这是否是正确的语法。如果我没记错的话,“发布”值应该位于 Request 对象中的某个位置。看看你是否获得了那里的价值。如果你得到它,问题是在页面的 OnInit 事件之后映射回的视图状态。然后我们必须找到解决方案......This appears to me as if the viewstate makes you a bad joke (as often). What I guess is that when the page posts back, the hidden field is initialized with the value contained in the Viewstate.
Try one thing: Don't access the value directly through
yourHiddenField.Value
but in the OnInit of your page get it withRequest.Forms[yourHiddenField.UniqueId]
and cast it to a HiddenField and extract the value. I'm not sure whether this is the right syntax. The "posted" values should be in the Request object somewhere if I remember right. See whether you get the value there. If you get it there, the problem is the viewstate which is mapped back after the OnInit event of the page. Then we have to find a solution for that...您必须使用 AJAX 更新面板提供的 EndRequestHandler 事件来完成此操作。
请看这个-->
http://www.codeproject.com/KB/ajax/AfterAjaxUpdate。 aspx?display=打印
You will have to do it using EndRequestHandler event, provided by the AJAX Update Panel.
Please look at this-->
http://www.codeproject.com/KB/ajax/AfterAjaxUpdate.aspx?display=Print
您如何在客户端上生成新输入?是否可以在服务器上执行此操作?
这样您就可以只使用
OnClick
,而不是同时使用OnClick
和OnClientClick
。How are you generating the new input on the client? Would it be possible to perform this on the server instead?
This way you can use the just the
OnClick
instead of both theOnClick
andOnClientClick
.