8

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!

Guillermo Gomez
  • 1,725
  • 4
  • 16
  • 21
  • I had got the same exception on production server when I installed .NET Framework 4.5. Because of that I had to restore back .NET 4.0. Btw, .NET 4.5 is just update (or service pack) of .NET 4.0, they could not live side-by-side. What a goofy stuff... – Evgeni Nabokov Dec 21 '12 at 07:33

5 Answers5

17

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.

chprpipr
  • 2,039
  • 16
  • 17
  • I agree with your suggestions. What's happening in my case has to do with point #2. However, I am unable to identify how it fails. The controls should be rendered in the same way as they did on the original page load (pre-postback). My linkbutton is inside a datalist. I've tried to use the !IsPostback clause before binding my datalist but it still fails. – Guillermo Gomez May 02 '11 at 18:49
  • 4
    "My linkbutton is inside a datalist" is the key statement. Is the datalist being rebound on the postback? If so, the original LinkButton is wiped out and new LinkButton is created. Its functionally and aesthetically identical, but .NET know the difference and kicks up this error. – chprpipr May 02 '11 at 20:12
3

If they're clicking before a page has a chance to fully render then the __EVENTVALIDATION fields aren't going to have been completely written - thus your error.

Now this was fixed in 3.5 SP1/3.0 SP2, and is configurable in web.config;

<configuration>
    <system.web>
        <pages renderAllHiddenFieldsAtTopOfForm="true"></pages>
    </system.web>
</configuration>

The default is true - so what version of .NET are you running? You could always disable the buttons client side until the page has finished loading.

blowdart
  • 55,577
  • 12
  • 114
  • 149
1

This error was appearing intermittently for me, on a very large page.

I discovered that if a button was clicked before the page had completed loading, it would give this error.

Waiting for the page to load completely before clicking the button, I didn't get the error.

Alex McMillan
  • 17,096
  • 12
  • 55
  • 88
0

Use this in page.asx in the page tag EnableEventValidation="false"

Muhammad Essa
  • 142
  • 1
  • 10
  • 3
    Using EnableEventValidation="false" is bad because it lessens security. – Dirk Strauss Oct 14 '15 at 15:23
  • I am agreed that it lessens security but I find the solution for that any suggestion that can resolve the issue.... @Dirk Strauss – Muhammad Essa Oct 16 '15 at 04:46
  • Has this issue/fix changed recently? My page had been working fine for years but now am getting this error. Full error: https://gist.github.com/cemerson/12b4b44acd5f58fc6574a4efaf9c46ba – Christopher Apr 24 '23 at 10:01
0

I've found that html forms can cause this problem in WebForms if you don't remove them all from a template

hngr18
  • 817
  • 10
  • 13