asp.neteventvalidation

How do I prevent exceptions from half-loaded pages' form submission while using asp.net event validation?


I have a page with some dynamically added buttons. If you click a button before the page has fully loaded, it throws the classic exception:

Invalid postback or callback argument. 
Event validation is enabled using in configuration or 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 am guessing the Viewstate field hasn't loaded on the form yet, the the other bits are being submitted. What's the best way to prevent this error, while maintaining Event Validation?


Solution

  • I answered a similar question here. To quote:

    Essentially, you'll want to get the ViewState to load at the top of the page. In .NET 3.5 SP1 the RenderAllHiddenFieldsAtTopOfForm property was added to the PagesSection configuration.

    Web.config

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

    Interestingly, the default value of this is true. So, in essence, if you are using .NET 3.5 SP1 then the ViewState is automatically being rendered at the top of the form (before the rest of the page is loaded) thus eliminating the ViewState error you are getting.