• Sign in
 
  •  
  • MSDN Blogs
  • Microsoft Blog Images
  • More ...
Common Tasks
  • Blog Home
  • Email Blog Author
  • RSS for comments
  • RSS for posts
Search
Tags
  • .NET Framework
  • Ajax/Javascript
  • ARR
  • ASP.NET
  • CLR
  • Cool stuff
  • DataAccess
  • Debugging/Windbg
  • Hotfix/Service Pack
  • IDEVDataCollector
  • IIS
  • Internet Explorer
  • Italian techs
  • LogParser
  • OT
  • Personal
  • Productivity
  • Random
  • Scripting/ASP
  • Security
  • Technology
  • Tools
  • Troubleshooting
  • Vista/Longhorn
  • Visual Studio
Archives
Archives
  • January 2013 (2)
  • November 2010 (1)
  • October 2010 (1)
  • July 2010 (2)
  • April 2010 (1)
  • March 2010 (2)
  • February 2010 (2)
  • January 2010 (1)
  • October 2009 (2)
  • September 2009 (2)
  • August 2009 (1)
  • July 2009 (5)
  • June 2009 (1)
  • May 2009 (1)
  • April 2009 (3)
  • March 2009 (3)
  • February 2009 (5)
  • January 2009 (3)
  • December 2008 (5)
  • November 2008 (3)
  • October 2008 (2)
  • September 2008 (3)
  • August 2008 (3)
  • July 2008 (3)
  • June 2008 (5)
  • May 2008 (4)
  • April 2008 (8)
  • March 2008 (4)
  • February 2008 (5)
  • January 2008 (2)
  • December 2007 (4)
  • November 2007 (6)
  • October 2007 (6)
  • September 2007 (8)
  • August 2007 (6)
  • July 2007 (7)
  • June 2007 (10)
  • May 2007 (9)
  • April 2007 (12)
  • March 2007 (8)
  • February 2007 (5)
  • January 2007 (3)
  • December 2006 (1)
  • November 2006 (4)
  • October 2006 (2)
  • September 2006 (9)
  • August 2006 (2)
  • July 2006 (1)

“Invalid postback or callback argument” in ASP.NET

MSDN Blogs > Never doubt thy debugger > “Invalid postback or callback argument” in ASP.NET

“Invalid postback or callback argument” in ASP.NET

Carlo Cardella
2 May 2008 8:44 AM
  • Comments 21

I saw this error twice recently, but as often happens for two completely different cases so here they are, hope it helps someone to same their time…

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

Nested forms

The first problematic application was dynamically building page layout and manipulating the HTML stream sent to the client; in my specific case there was some manipulation carried on the client through Javascript, but I think the same may happen if for example the HTML stream is changed through an HttpHandler after the main ASP.NET processing has been completed. The page was rendered correctly within the browser, but a postback thrown the exception above and this was due to a malformed page structure, where we had nested <form> tags like in the following example:

<body>
    <form id="form1" runat="server">
        <div>
            <asp:Button ID="Button1" runat="server" Text="Button" />
            <form></form>
        </div>
    </form>
</body>

There was a bug in the page, so the solution is to fix it to avoid the nested <fom> tags.

Hidden postback control

Here we had a DataGrid and on the ItemCreated event the customer was calling Item.Attributes.Add() to add a call to __DoPostBack() and have an automatic postback when a record was selected; needless to say, the postback attempt returned the error at the beginning of the post.

The customer had defined a “Select” column with a button to actually select the row within the DataGrid and set the Visible property of this button to false; ASP.NET renders the button to select the record as a LinkButton. This also emits the __DoPostBack() javascript method and registers it for event validation calling ClientScript.RegisterForEventValidation(); as you can imagine, if the control is not visible (i.e. Visible=”false”) the control is not rendered and therefore is not registered for event validation. But the DataGrid uses that event anyway, which ultimately throws the error.

The solution in this case is to manually register each row for event validation with code similar to the following:

protected override void Render(HtmlTextWriter writer) 
{ 
    foreach (DataGridItem row in DataGrid1.Items) 
    ClientScript.RegisterForEventValidation(row.UniqueID.ToString() + ":_ctl0"); 
    base.Render(writer); 
} 

The event validation must be registered within the Render() method of the page.

Also note that there could be different degrees of added complexity to this scenario: for example if the name of the control causing the postback is built dynamically and maybe the application has been migrated from ASP.NET 1.1 to 2.0, you have to be aware that the naming convention for dynamic controls has changed. ASP.NET 1.1 uses DataGrid1$_ctl2$_ctl0 while ASP.NET 2.0 uses DataGrid1$ctl02$ctl00. It is possible to control this behavior setting xhtmlConformance=”Legacy” (see xhtmlConformance); however note that reverting back to Legacy mode causes ASP.NET 2.0 to use a column (“:”) sign in control names for event validation, instead of the dollar sign (“$”) which uses for rendering.

 

Carlo

Quote of the day:
Read, every day, something no one else is reading. Think, every day, something no one else is thinking. Do, every day, something no one else would be silly enough to do. It is bad for the mind to be always part of unanimity. - Christopher Morley
  • 21 Comments
ASP.NET, CLR
Leave a Comment
  • Please add 1 and 2 and type the answer here:
  • Post
Comments
  • krg
    2 May 2008 9:53 AM

    Good morning.

    I've been tracking the thread about this issue on the ASP.NET forums (http://forums.asp.net/t/922994.aspx) and have tried a bunch of things, but I haven't found a good solution to my problem.

    The VeriSign seal JavaScript introduces its own <form> element which intermittently (I've never been able to hold my teeth just right to reliably reproduce it) produces this error.

    How can I go about registering an externally-referenced script with a <form> for event validation?

    Thanks.

  • Erick
    5 May 2008 1:07 PM

    This error also happens alot when the page is posted back before the __EventValidation hidden field is fully sent to the client. (which happens frequently since it gets rendered at the end of the page instead of the beginning like viewstate etc).  And often times the only real fix is to disable event validation completly.

    To be perfectly honest, I think this is fairly serious problem with the WebForms model.   I like the fact that MS is taking the "Secure by default" approach here, but this particular feature doesn't translate to the www very well.   It gets even worse as more and more processing is being moved to the client.  

    I guess the goal is to make sure that only valid values are submitted from the client, which is a good thing, but the way it is implemented is by having the client tell the server  what the valid values coming from the client are, and all someone has to do to get around this and submit a malicious value is to manually modify __EventValidation. (of course that is easier said that done, considering it is encrypted and all of that, but its still a pretty flacky design).

    I am not really sure what the solution is, the web is stateless and thats why we have to implement things like viewstate and eventvalidation on the client side.

    My company has a blanket policy that all sites we develop have eventvalidation disabled.  Even on a highly optimized page with the smallest html footprint we can get, there are still EventValidation exceptions coming from clients on slow connections.  The only way to deal with that is to disable eventvalidation and just be really careful about checking values on the server side to make sure they are legal.  And considering it is theoretically possible to bypass eventvalidation by cracking the hidden field, this is something we need to do anyways.

    Just my two cents, but i think that feature is poorly implement, and causes way to many problems.

  • Edddy
    22 May 2008 3:57 PM

    Erick: It seems that you explain why I'm getting this error from time to time, but I will not turn off  EventValidation, it is the first line of defense against cross site scripting attacks.

  • jct
    19 Jul 2008 3:29 PM

    THANK YOU for the info that this error can be caused by having multiple form tags...didn't see that mentioned anywhere else I googled for "Invalid postback.." error. I can now get back to work.

    Something else I noticed is that a postback generated from within the *nested* form seemed to work fine; interestingly, postbacks generated from outside the nested forms are the only ones that crapped out.

    thanks again

  • David
    31 Aug 2008 11:42 PM

    Thanks for this info. It helped me figure out where the problem was.

    My page was loading a java applet that needed to use a different cab file depending on whether or not it is a Mac and this is done using javascript to add the applet tag to the page. There is also a refresh button rendered to the page, so that users can re-initialised the applet when they need to. Whenever the page was being a bit slow, users would hit the refresh button thinking that the page hadn't loaded properly. It seems that because it was in the process of loading and javascript was adding html on the fly, the hidden eventvalidation field had not been loaded at the point of clicking refresh so was missing when the server recieved the request and produced this error. I can live without event validation on this page.

    On another note, it seems odd that this field would be added to the end of the form rather than the top where the eventtarget, eventarguments and viewstate are rendered. If it were rendered to the top of the form, then the situation that I had would not have occurred.

    Again thanks for the post, it was a great help.

  • Jason
    16 Sep 2008 1:16 PM

    I found that this error occurred when I had a repeater and I bound (binded?) it twice.

  • Alex
    10 Nov 2008 11:38 AM

    I also found the double binding was causing this error - exiting the Page load on postback fixed the problem

  • Dhanraj Agrawal
    20 Nov 2008 12:22 AM

    Hi,

    Please note the evidence of this issue:

    You will find that this error will come oftenly, and we will find that this error mainly comes after every 20 minutes by default.

    Please can you confirm back.

  • Carlo Cardella
    21 Nov 2008 2:22 AM

    Dhanraj, the error appears when you post the page back, no specific timeout that I am aware of... maybe you are referring to a different issue?

  • hi..... same error
    9 Dec 2008 3:32 PM

    hi.,

    i m also faced the same error...  my problem is ... i have dropdown,and button in the grid view...

    in dropdown i chage the status and i save by clicking button in the grid.. after the operation grid will bind to show the updated information...

    in this binding onlt i m getting error...

    how can i solve it ... vellore.balaji@gmail.com

  • Jay Foster
    7 Jan 2009 11:29 AM

    There's a hidden caveat with this error that took me a while to figure out. In my case, I had dynamic template columns in a gridview, all controls dynamically created. One of the template controls was a button and the other was a dropdown list. The dropdown list had an event wired up and a postback. Everything was working, however when I clicked the back button, and then the template button, I would get the error. As it turned out my code was fine. The back button does not invoke the page_load so I couldn't recreate my controls and therefore, due to the postback (on the ddl) my page did not look the same as the previously recreated page and threw the error. The answer: <%@ OutputCache Location="None" %>. This 'expires' the page so that the user must hit the refresh button. There might be other solutions but once you create and recreate the controls properly, look at how the page can be seen without the page_load event (like the back button)

  • Mick
    5 Mar 2009 3:07 PM

    THANK YOU for this post on "Invalid postback or callback argument" errors.  I too didn't see the Nested <form> tags cause mentioned anywhere else.   It seems that my error was the result of an outdated Google search box I was using prior to moving to ASP.Net.

    BTW, it does not happen running locally using VWD, only with remote host.  Also I agree from the earlier comment that a postback generated from within the nested form seemed to work fine - in my case the search action.

    Thanks again! :-)

  • JAMC
    15 Mar 2009 10:11 AM

    I struggled with this problem for days, but with much experimentation have managed to sort my problem out. As the number of potential causes for this error seems quite high, I don't know whether this will help anyone, but here goes...

    1. Check whether you're using a Page.Validate call.

    2. More importantly check the positioning of the Page.Validate call in your code execution sequence. I had placed my Validate call too late in the code execution - i.e. I was making changes between the Page_Load and Page_PreRender events that the Page.Validate call did not like. Place the Page.Validate call as early as possible, so that the call is validating the original data sent by the postback, not the data you've altered after the postback.

  • Sid
    13 Apr 2009 4:13 PM

    Thanks for this post. Helped a lot !

  • Vivek
    26 May 2009 6:48 AM

    Just add this to your web config and make pages..like ErrorPage.aspx , and see you will control this error by making your custom control error.

    <system.web>

    <customErrors defaultRedirect="ErrorPage.aspx" mode="On">       <error statusCode="500" redirect="servererror.aspx" />    <error statusCode="404" redirect="filenotfound.aspx" />    <error statusCode="403" redirect="AccessDenied.aspx" /> </customErrors>

    <system.web>

Page 1 of 2 (21 items) 12
  • © 2013 Microsoft Corporation.
  • Terms of Use
  • Trademarks
  • Privacy & Cookies
  • Report Abuse
  • 5.6.426.415