The Old Sewing Factory

The Old Sewing Factory

11 Sep 2009

Tracking Down ArgumentException "Invalid postback or callback argument."

The longer you use webforms the more likely it is that you will see this:

Exception type: System.ArgumentException
Exception message: 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.

If you’re lucky enough to have it happen on a dev machine and you know which control is causing the problem you’re most of the way to tracking down the error. Just read this, this and this.

If like me, you can’t track it down, all you’re getting is vague error reports and users who can’t remember what they did (or curiously didn’t even see the error) then you need to know which control actually caused the error.

This is what I did:

Imports System.Net.Mail

Namespace Code.Controls

    Public Class TrackingLinkButton
        Inherits LinkButton

        Protected Overrides Sub RaisePostBackEvent(ByVal eventArgument As String)

            Catch argExc As ArgumentException
            End Try    
        End Sub    

        Private Sub SendPostbackError(ByVal argExc As ArgumentException)
            Dim subject As String = "ArgumentException: Additional info"
            Dim body As String = String.Format("Exception was caused by the control with ID: {0} and clientId of: {1}", _
                                               Me.ID, Me.ClientID)
            Dim msg As New MailMessage("", "support@somesite,com", _
                                        subject, body)
            Dim client As New SmtpClient()
        End Sub      

    End Class  

End Namespace

And hooked it in with this:

    <add tagType="System.Web.UI.WebControls.LinkButton"

Now all I have to do it sit back and wait for the email to fire and I should be able to track down the error (I hope).

comments powered by Disqus