Invalid postback or callback argument

I'm sure many of you have seen this error message when developing your web application:

Server Error in '/' Application.
--------------------------------------------------------------------------------

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.

I'm going to discuss this in the context of the data controls. This happens when a control that isn't registered for event validation causes a postback, but surely that can't be the case.. right?

Let's look at a small repro:

Markup:

<asp:GridView ID="GridView1" runat="server">
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:Button runat="server" Text="Button" />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

Code behind:

public partial class _Default : System.Web.UI.Page {
    protected void Page_Load(object sender, EventArgs e) {
        GridView1.DataSource = Enumerable.Range(0, 5);
        GridView1.DataBind();
    }
}

Now click on the button and see the dreaded error message. Why does this happen? EventValidation was added in ASP.NET to ensure that controls causing the postback came from the same page being rendered. Take a look at __EVENTVALIDATION hidden field on the page. It is a serialized version of all of the controls registered for postbacks(read more here). You might be wondering how they got in there and why is the button inside of a GridView a special case. It's not a special case, in fact, Button registers itself with the current page.

The reason this happens is because we rebind the data control in Page_Load every time which means that we will lose all of the posted data and viewstate. As a result, the ID of the button is different and when the event is validated there will be no matching unique id and hence event validation will fail. We are acutally raising an event for a button that is no longer in the control tree.

You can work around this by wrapping that code in if (!IsPostBack). This is a good proof of why you should use DataSource controls.

Hope this helps

Published Monday, March 09, 2009 12:54 AM by davidfowl
Filed under: ,

Comments

# Invalid postback or callback argument

Monday, March 09, 2009 5:59 AM by What's New

I&#39;m sure many of you have seen this error message when developing your web application: Server Error

# re: Invalid postback or callback argument

Monday, March 09, 2009 6:23 AM by san

thanks !!

this helps

# Dew Drop &ndash; March 9, 2009 | Alvin Ashcraft's Morning Dew

Pingback from  Dew Drop &ndash; March 9, 2009 | Alvin Ashcraft's Morning Dew

# re: Invalid postback or callback argument

Monday, March 09, 2009 1:10 PM by AndrewSeven

Although it is a detail that you don't have to consider when using a datasource, I'm not sure its proof of anything other then "using something wrong will give wrong results".

# re: Invalid postback or callback argument

Tuesday, March 10, 2009 2:50 AM by Thuzar

I created the Asp Website project then I tired to insert DB into it but it showed me such error

"SQL network interfaces error 26 , error locating server / instance specified "

I tried to create MS SQL 2005 DB  , but I couldnt .

Please tell me why

# re: Invalid postback or callback argument

Tuesday, March 10, 2009 4:02 AM by eanirudh

very helpful post

# re: Invalid postback or callback argument

Tuesday, March 10, 2009 8:30 AM by Joe Chung

Binding the GridView would work without affecting event validation if you did it in the PreRender event instead.

# re: Invalid postback or callback argument

Friday, September 04, 2009 3:50 PM by Yu

It works nicely. Thank you!

# re: Invalid postback or callback argument

Thursday, October 01, 2009 2:31 PM by MUKESH

what is view state pleaz Explain with Details?

# re: Invalid postback or callback argument

Thursday, October 29, 2009 11:03 AM by sdakoju

I was developing a workflow for a sharepoint site. I got the same error, after this article I realised what went wrong..thank you for this..

# re: Invalid postback or callback argument

Tuesday, January 26, 2010 2:33 PM by Rauland

Thanks!

# re: Invalid postback or callback argument

Wednesday, February 03, 2010 12:04 AM by cheng

Thanks for sharing this. It's really helpful.

(*_*)

# re: Invalid postback or callback argument

Wednesday, August 18, 2010 10:24 AM by gfe cim

Pretty cool site you've got here. Thanks for it. I like such topics and everything connected to them. I definitely want to read more on that blog soon.

Anete Swenson

# re: Invalid postback or callback argument

Thursday, August 19, 2010 2:08 AM by niravbhattsai

Nice post....

Check out url www.aspnetajaxtutorials.com/.../invalid-postback-or-callback-argument.html for same.

# re: Invalid postback or callback argument

Tuesday, September 07, 2010 4:54 PM by Bella Simpson

Rather interesting blog you've got here. Thanks the author for it. I like such topics and anything that is connected to them. I would like to read a bit more on that blog soon.

Bella Simpson

<a href="nycescort.org/">female escorts in new york</a>

# re: Invalid postback or callback argument

Friday, September 10, 2010 5:28 PM by Dustin

Thanks.  This saved me hours of trying to figure this out!

# re: Invalid postback or callback argument

Tuesday, September 14, 2010 12:02 PM by Hilary Benedict

Pretty nice place you've got here. Thanks for it. I like such topics and everything connected to this matter. I definitely want to read a bit more on that blog soon.

Hilary Benedict

<a href="suisse-escort.com/">escort asian schweiz</a>

# re: Invalid postback or callback argument

Monday, October 25, 2010 10:51 AM by Hilary Benedict

Pretty nice site you've got here. Thanx for it. I like such themes and anything connected to this matter. I would like to read a bit more soon.

What  do you think about changing it once in a few months?

Hilary Benedict

<a href="www.asianescort.us/">montreal oriental escorts</a>

# re: Invalid postback or callback argument

Wednesday, April 20, 2011 10:42 AM by ultravi0let

I've been pulling my hair out for 2 hours because of this. Thank you so much !

Regards,

Sanaa

# re: Invalid postback or callback argument

Friday, September 02, 2011 2:07 PM by Brij Sharma

Very helpful post.

Although I am still getting the error.

I wonder if it is related to a ControlAdapter attached to DropDownList.

I am getting the error when a dropDownList set to AutoPostback is posting data to server.

If have used if(!IsPostBack), but still the issue is not resolved.

Please help

Leave a Comment

(required) 
(required) 
(optional)
(required)