views:

801

answers:

3

I am getting the following error when an event (Add/Edit/Delete) occurs on my databound control.

Invalid postback or callback argument. Event validation is enabled using 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.

I am using a custom DataList control, but this problem also occurs with GridView, DetailsView, FormView and Repeater control (and maybe with other databound controls).

The answers I can find tell me to turn off the validation in the config file or page, but that does not sound like it is the best solution. What am I doing wrong?

+2  A: 

The problem is loading the data for the control in the page Load event and calling the DataBind() method. However it appears that if the DataBind() method is called before the events are raised the above exception is generated as the control naming has changed.

The solution is to change this to if(!IsPostback) DataBind() and then call the DataBind() method at the end of the event handler. You would need to call it most of the time anyway at the end of the handler to affect the changes.

If this is not your problem, and you are modifying controls client side using JavaScript, check out this article.

This is a self answered post as I was getting a lot of responses on my blog to this issue and thought I might share it further.

Robert Wagner
A: 

Thank you for this. I has facing this problem and your self-answer helped me fix it.

An alternative to calling the DataBind() method at the end of every event handler, is to do it once in the page PreRender event.

Daniel Liuzzi
A: 

THANKS THANKS THANKS THANKS THANKS THANKS THANKS THANKS