禁用单个控件的EventValidation,可能吗?
我知道这是一个非常有争议的话题,通常当您考虑将此作为解决方案时,您可能需要重新考虑您的 UI 逻辑。
我知道我可以使用 ClientScriptManager.RegisterForEventValidation 通过验证。但是,我真的很想知道。是否可以删除单个控件的事件验证?有办法解决这个问题吗?
我正在渲染 DropDownList 后从客户端修改它。
I know this is a very debated topic, and usually when you are thinking about this as a solution you might want to rethink your UI logic.
I know I can pass validation using ClientScriptManager.RegisterForEventValidation. But, I'd really like to know. Is it possible to remove event validation for a single control? Is there a way work around for this?
I'm modifying a DropDownList, from the client side after it's rendered.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
SupportsEventValidation 属性不会为子类继承,因此,如果您创建 DropDownList 的子类,不要将该属性应用于该子类,并在页面上使用该子类,您将拥有一个不会触发事件验证的控件,即使页面上是否启用了事件验证。
http://msdn.microsoft.com/ en-us/library/system.web.ui.supportseventvalidationattribute%28v=VS.110%29.aspx
The SupportsEventValidation attribute is not inherited for subclasses, so if you create a subclass of DropDownList, don't apply that attribute to the subclass, and use the subclass on your page, you will have a control that doesn't trigger event validation, even if event validation is enabled on the page.
http://msdn.microsoft.com/en-us/library/system.web.ui.supportseventvalidationattribute%28v=VS.110%29.aspx
处理此问题的另一种方法是:
在特定元素上设置属性
EnableViewState="False"
。推理:如果您使用 JavaScript 在 ASP.NET 的控制范围之外更改此值,那么 ViewState 此时就会妨碍您。停止将其用于该控件。
Another way to handle this:
Set the property
EnableViewState="False"
on the particular element.Reasoning: If you are using JavaScript to change this value outside of ASP.NET's control, then ViewState is just getting in your way at that point. Stop using it for that control.
对你的问题的简短回答是:不,没有办法做到这一点。您必须禁用整个页面的 EventValidation。
有多种解决方法...如果您确实不想禁用 EventValidation,请将所选值存储在隐藏字段中并恢复 DropDownList 的状态(或者可能只是清除选择?)。
如果您需要在客户端添加所有值,则无论如何都必须使用其他方法发送这些值,因为它们不会出现在服务器端控件中。这些值没有发布!
The short answer to your question is: No, there is no way to do this. You must disable EventValidation for the entire page.
There are various workarounds... If you really don't want to disable EventValidation, store the selected value in a hidden field and restore the state of the DropDownList (or maybe just clear selection?).
If you need all the values added client-side, you have to send those up using some other method anyway, because they will not be present in the server-side control. Those values are not posted!
我发现用 runat="server" 等效的 HTML 替换控件更容易,然后您可以使用 Request.Forms["id"] 检索旧时尚方式的值。不会进行任何验证,因此在存储或处理数据时要小心。
另一个选项是覆盖页面上的渲染并使用 Page.ClientScript.RegisterForEventValidation 以及所有可能的答案(不好)。
像这样的东西
I found it easier to replace the control with the HTML equivalent with runat="server", you can then retrieve the value the of old fashion way with Request.Forms["id"]. There will be no validation done, so be careful on storing or processing the data.
The other option is to override the Render on the page and use Page.ClientScript.RegisterForEventValidation with all the possible answers (not nice).
something like this