I have a page with some dynamically added buttons. If you click the button before the page has fully loaded, it throws a classic exception:
Invalid postback or callback argument. Event validation is enabled using in configuration or in a page. For For security reasons, this function checks that the arguments for the postback or callback of the event come from which it originally displayed. If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method to register the postback data or callback for validation.
Invalid postback or callback argument. Event validation is enabled using in configuration or in a page. For
For security reasons, this function checks that the arguments for the postback or callback of the event come from which it originally displayed. If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method to register the postback data or callback for validation.
I assume that the Viewstate field has not yet been loaded into the form, and the remaining bits are sent. What is the best way to prevent this error while maintaining event validation?
I answered a similar question here . Quote:
Essentially, you'll want to load ViewState 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 is true. So, essentially, if you use .NET 3.5 SP1, then the ViewState is automatically displayed at the top of the form (before the rest of the page is loaded), thereby eliminating the ViewState error you get.
, __EVENTVALIDATION, ViewState . __EVENTVALIDATION , , , .
__EVENTVALIDATION
, , , - , - javascript, isLoaded, true . . isLoaded , , , , . , , .
, false , true? , , .
, , ViewState .
ClientScriptManager.RegisterForEventValidation( button.UniqueID ) ? , , , , , , , , .
ClientScriptManager.RegisterForEventValidation( button.UniqueID )
, , __EVENTVALIDATION , . , - Render. , , , , :
javascript -
function CheckForHiddenFields() { var EventValidation = document.getElementById('__EVENTVALIDATION'); if (EventValidation && EventValidation.value && EventValidation.value != '') { return true; } else { return false; } }
asp: Button:
OnClientClick="return CheckForHiddenFields();"