13

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.

Raúl Roa
  • 12,061
  • 13
  • 49
  • 64

4 Answers4

34

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

Dale K
  • 491
  • 4
  • 9
  • 5
    I've been on SO 4+ years. This is the best collision of "terrible answer" and "wonderful answer" I've seen. This answer needs a "donate" button. – lance Mar 12 '14 at 20:52
  • And to think I just spent time spelunking the validation code in JustDecompile to find a way -- and I came up with the same thing. Should have known to check SO first. BTW, I agree w/lance, need a "donate" button. :-) – Walden Leverich Aug 27 '14 at 18:58
  • This answer sounds promising, but can you give an example of how I could implement it. How do I place in on my .ascx web control? In Windows Forms I can create a User Control that inherits from DropdownList, but an ascx requires you to inherit from System.Web.UI.UserControl. Does this mean I will have to add this one via code, and not place it on the ascx page? – David P Aug 27 '15 at 21:50
  • NM - I figured it out. FYI (In case somebody wants to know). Created a separate ASP.NET Server Control Project (See: https://msdn.microsoft.com/en-us/library/vstudio/yhzc935f(v=vs.100).aspx) and subclassed all the typical WebControls I would use on an ASCX page. I then went back to ascx web control solution, and added the Server Control Project in as an existing project, and then referenced it from ascx web control project. I could then add my new subclassed controls to the ASCX page, and could avoid event validation, without worrying about the page or site settings. – David P Aug 28 '15 at 15:39
4

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.

famousgarkin
  • 13,687
  • 5
  • 58
  • 74
Byron Katz
  • 492
  • 5
  • 10
3

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);
    } 
Podge
  • 467
  • 3
  • 16
  • If you read the question, I am aware of this method. But implies knowing the values that are going to be added on the client side before the control renders, which I don't. – Raúl Roa Jan 11 '10 at 18:00
  • 1
    +1 for suggesting using a generic HTML equivalent. Good thinking! – Lucky Pierre Jan 14 '14 at 16:25
  • When trying to retrieve the value on PostBack and you're using MasterPages, the ID will not just be "ID". To get the value you can try Request.Form[Request.Form.AllKeys.First(x => x.Contains("id"))] – hacker Apr 03 '16 at 13:06
3

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!

Bryan
  • 8,748
  • 7
  • 41
  • 62