Disable EventValidation for single control, is it possible?
Asked Answered
G

4

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.

Germinative answered 11/1, 2010 at 13:37 Comment(0)
O
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!

Ochre answered 11/1, 2010 at 21:58 Comment(1)
After trying different things I came up with the same answer to my question.Kodak
B
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

Boogiewoogie answered 20/12, 2011 at 20:11 Comment(4)
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.Curvature
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. :-)Tweeter
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?Cauterant
NM - I figured it out. FYI (In case somebody wants to know). Created a separate ASP.NET Server Control Project (See: msdn.microsoft.com/en-us/library/vstudio/…) 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.Cauterant
C
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.

Counterpoint answered 14/5, 2014 at 15:56 Comment(0)
F
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);
    } 
Fruitcake answered 11/1, 2010 at 16:18 Comment(3)
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.Kodak
+1 for suggesting using a generic HTML equivalent. Good thinking!Lettie
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"))]Butacaine
O
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!

Ochre answered 11/1, 2010 at 21:58 Comment(1)
After trying different things I came up with the same answer to my question.Kodak

© 2022 - 2024 — McMap. All rights reserved.