Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Invalid postback or callback argument" with Databound controls

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?

like image 777
Robert Wagner Avatar asked Nov 16 '08 08:11

Robert Wagner


3 Answers

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.

like image 164
Robert Wagner Avatar answered Nov 05 '22 01:11

Robert Wagner


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.

like image 7
Daniel Liuzzi Avatar answered Nov 04 '22 23:11

Daniel Liuzzi


I was experiencing the same issue and it took me few hours to solve my problem. Robert answer partially helped me and despite databinding my repeater regardless of post back or not, problem still persisted. After lot of research i came across a post which suggested setting UseSubmitBehavior="false", bingo and it solved the issue. Hopefully this will help.

like image 3
Taimur Khan Avatar answered Nov 05 '22 01:11

Taimur Khan