简体   繁体   English

使用Button提交表单但不使用LinkBut​​ton提交表单时,无效的回发或回调参数错误

[英]Invalid postback or callback argument error when submitting form with Button but not with LinkButton

I have a form where I dynamically populate a DropDownList using Jquery's ajax function to retrieve a list of values from a web service. 我有一个表单,在其中使用Jquery的ajax函数动态填充DropDownList来从Web服务中检索值列表。 I originally had a Button control which submitted the form. 我最初有一个Button控件,它提交了表单。 This caused the "exception:Invalid postback or callback argument. Event validation is enable...." error. 这导致“ exception:Invalid postback或callback参数。事件验证已启用...。”错误。

After researching options, such as disabling event validation (bad) and registering for event validation (which would not work in this case) the best option seemed to be to swap the Button control for a LinkButton control. 在研究了诸如禁用事件验证(错误)和注册事件验证(在这种情况下将不起作用)之类的选项之后,最好的选择似乎是将Button控件交换为LinkBut​​ton控件。 I did this and, sure enough, it works fine now. 我这样做了,果然,现在可以正常工作了。

My question is...why? 我的问题是...为什么?

What is different about the LinkButton that means that it does not cause the event validation error and have I, by changing to a LinkButton, introduced a new security risk because event validation isn't happening? LinkBut​​ton有什么不同之处,那就是它不会引起事件验证错误,并且由于没有发生事件验证,我是否通过更改为LinkBut​​ton引入了新的安全风险?

The postback validation error is happening because the data you send back at the postback is no the same than when it was sent by the server. 由于您在回发时发送回的数据与服务器发送时的数据不同,因此发生了回发验证错误。

You should take a look at this blog post by Scott K. Allen. 您应该看一下Scott K. Allen的这篇博客文章 He suggests to add all the possible values for your dropdown in the Render event for your web page. 他建议在网页的“渲染”事件中为下拉菜单添加所有可能的值。

You could also create your own version of the DropDownList since it won't require event validation as this guy suggests . 您也可以创建自己的DropDownList版本,因为它不需要此人建议的事件验证。

My personnal take is that you might have to rethink how you interact with your data. 我个人的观点是,您可能必须重新考虑如何与数据交互。 If you need to feed dynamically your DropDownList and you use ASP.NET WebForms then you are required to have a PostBack for that. 如果需要动态提供DropDownList,并且使用ASP.NET WebForms,则需要为此提供一个PostBack。 You could use a UpdatePanel to make it feel "Ajax" if you want. 如果需要,可以使用UpdatePanel使其具有“ Ajax”的感觉。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM