Reputation: 9411
I have a need to add an item to DropDownList client side, after I made some ajax calls.
If I just use jquery's method to add items to relevant select, it is added there, but when I later select this new (client-side-added) item in dropdown and do postback, there is a problem:
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.
How would I correctly modify DropDownList, so new entry both appear on list AND if I make postback with that new item selected, its value would go to server and used further?
Upvotes: 0
Views: 2015
Reputation: 411
There are two different strategies that I can suggest.
Add any new items to the drop down list server side, call a method on the server via a PostBack to add the new item(s) to the DataSet that the list is bound to and then rebind the drop down list.
Bypass the ViewState and get the selected value form the drop down list directly from the post values submitted back to the server as shown below. You would not need to 'round trip' in this case and add the new item(s) server side.
selectedValue = Request.Form[ - drop down list id -].ToString();
Upvotes: 2
Reputation: 6692
You should use ASP UpdatePanel or you're bound to have trouble with the control being different than what the ViewState tells.
Upvotes: 2