Disable EventValidation for single control, is it possible?

送分小仙女□ 提交于 2019-12-22 02:30:50

问题


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.


回答1:


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!




回答2:


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.

public Class DynamicDropDownList : DropDownList
{
}

http://msdn.microsoft.com/en-us/library/system.web.ui.supportseventvalidationattribute%28v=VS.110%29.aspx




回答3:


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.




回答4:


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

    protected override void Render(HtmlTextWriter writer)
    {
        this.Page.ClientScript.RegisterForEventValidation(ddlRisk.ID, "a");
        this.Page.ClientScript.RegisterForEventValidation(ddlRisk.ID, "b");
        this.Page.ClientScript.RegisterForEventValidation(ddlRisk.ID, "c");
        base.Render(writer);
    } 


来源:https://stackoverflow.com/questions/2042134/disable-eventvalidation-for-single-control-is-it-possible

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!