Tuesday, February 3, 2009

Viewstate is lost for dynamically (javascript/AJAX) generated dropdown box

I had a page with the dynamically (AJAX) populated dropdown box, which was failing to persist the state whenever ASP.NET postback was executed. To work around the problem - I created a hidden field, and attached SelectedIndexChange event to the dropdown, which populated hidden field with the selected value from the dropdown...
Simple enough!

Just make sure EnableEventValidation is set to false, otherwise the following error is being thrown:

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.

No comments: