Search code examples
asp.netpostbacklinkbutton

Invalid postback or callback argument. Why?


So I get the exception

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.

With the following stack trace

[System.ArgumentException: Untrapped Exception: 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.] at System.Web.UI.ClientScriptManager.ValidateEvent(String uniqueId, String argument) at System.Web.UI.Control.ValidateEvent(String uniqueID, String eventArgument) at System.Web.UI.WebControls.LinkButton.RaisePostBackEvent(String eventArgument) at System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

The exception occurs after submitting a form, and then quickly clicking on a LinkButton to download a file on the same page before the page reloads again.

Can someone explain the details of why this exception is occurring upon executing the actions described above?

Thanks in advance!


Solution

  • This has to be one of the most frustrating error messages in .NET, but once you get a feel for what's going on, it makes sense. .NET likes to know EVERYTHING that's going on. It keeps track of all the elements that it has placed on the page. Along those same lines, .NET gets offended when it receives input from something it didn't know about. In your case, it sounds like, at the time you click on the LinkButton, .NET doesn't think it should be there. In my experience, there are two likely reasons for this:

    1. You're doing to client-side wizardry that is creating new inputs or cloning existing inputs.

    2. While the form submission is being processed, .NET does something to the LinkButton which causes it to be no longer available. Some examples of this I've run into are when your LinkButton is dynamically created in the backend or you're using UpdatePanels and their content get changed during the form's submission.

    Basically, I believe if you step through the form submission code and watch that LinkButton, you'll see .NET forget about it, which understandably triggers this "Security Exception" when the LinkButton is clicked.