Disable EventValidation for single control, is it possible?

巧了我就是萌 提交于 2019-12-05 01:24:58

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!

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

Byron Katz

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.

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