Logo

Maarten Balliauw {blog}

ASP.NET, ASP.NET MVC, Windows Azure, PHP, ...

About the author

Maarten Balliauw is currently employed as a Technical Evangelist at JetBrains. His interests are mainly web applications developed in ASP.NET (C#) or PHP and the Windows Azure cloud platform.
More about me More about me
Send mail E-mail me


ASP.NET MVC Quickly Pro NuGet Subscribe to my RSS feed Follow me on Twitter! View Maarten Balliauw's profile on LinkedIn
Maarten Balliauw - MVP - Most Valuable Professional
Maarten Balliauw - ASPInsider

Search

Archive

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright Maarten Balliauw 2013


ASP.NET 2.0 Event Validation

Event Validation is a new feature in ASP.NET 2.0 which provides an additional level of checks on postback actions. It verifies whether a postback from a control on client-side is really from that control and not from a malicious person trying to break your application.

Even if you forget to add security checks of your own, ASP.NET provides this functionality, because this feature is enabled by default. Sometimes, it is safe to turn this of, but Microsoft tries to have developers turn this of when they know what they are doing.

Unfortunately: I came across Event Validation… A user control on a master page convinced ASP.NET that a postback within that same user control was unsafe, resulting in the following error:

"Invalid postback or callback argument.  Event validation is
enabled using <pages enableEventValidation="true"/> in
configuration or <%@ Page EnableEventValidation="true" %>
in a page. For security purposes, this feature verifies that
arguments to postback or callback events originate from
the server control that originally rendered them.
If the data is valid and expected, use the
ClientScriptManager.RegisterForEventValidation method
in order to register the postback or callback data for validation."

There are some options to overcome this… One is to add a EnableEventValidation="false" in your @Page directive, another is to globally disable this in your Web.config (don’t!). The best solution, however, is telling ASP.NET to allow events from your user control’s inner controls, by adding the following snippet of code in the user control:

protected override void Render(HtmlTextWriter writer)
{
    // Register controls for event validation
    foreach (Control c in this.Controls)
    {
        this.Page.ClientScript.RegisterForEventValidation(
                c.UniqueID.ToString()
        );
    }
    base.Render(writer);
}


Categories: ASP.NET | C# | General

Comments (2) -

kamila United States |

Sunday, October 26, 2008 4:31 AM

kamila

this solution didnt work for me. Adding the enableeventvalidation = false worked

lyall South Africa |

Thursday, September 16, 2010 4:32 PM

lyall

it does work. but some times a$p.net cant uniquely identify the controls in a user control.

Just use:
this.Page.ClientScript.RegisterForEventValidation( <YourControlID> ) ;

Comments are closed