Invalid PostBack or callback argument

Source: Internet
Author: User
Original article: http://weblogs.asp.net/davidfowler/archive/2009/03/09/invalid-postback-or-callback-argument-in-the-datacontrols.aspx

Invalid PostBack or callback argument

I'm sure because 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 VoidPage_load (ObjectSender,EventargsE ){
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 inIf (! Ispostback). This is a good proof of why you shoshould use datasource controls.

Hope this helps

Published Monday, March 09,200 9 am by David fowl Filed under: ASP. NET, datacontrols

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.