Invalid postback or callback argument in C# ASP.NET website

Today, in my c# asp.net website, I had the error message:

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.

The Problem

I had a function called BindGrid() which performed the bind on the dataview. It was called on Page_Load and in the functions associated with the dataview (for example, rowupdating, rowediting etc). When I tried to perform an update on the row (calling the onRowUpdate function) it showed this error.

The Solution

Quite simply, I was calling the BindGrid() too early, before the table had updated. I believe it’s ASP.net’s way of security checking for code injection (but I could be wrong!). Anyway, the solution was simply to add the initial BindGrid() in to the IsPostBack function.

protected void Page_Load (object sender, EventArgs e)
{
   if (!IsPostBack)
   {
      BindGrid();
   }
}

So now the BindGrid() is called only when the page loads for the first time. When the postback is done, the update/edit/cancel commands associated with the gridview will handle the bind.

Hopefully this might help someone who else has this issue!

Leave a Reply

Be the First to Comment!

Notify of
avatar
wpDiscuz