Add asp.net dropdownlist item from JavaScript caus

2020-05-07 09:11发布

问题:

The design is:

all elements are in an updatepanel.

click on a list of <tr> to fire a JavaScript function to add new <option> to the asp dropdownlist.

But, when I click on other buttons to callback to the server, I get an error message:

Uncaught Sys.WebForms.PageRequestManagerServerErrorException: Sys.WebForms.PageRequestManagerServerErrorException: 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.

I understand the problem. I need to let server side know the change of the dropdownlist before any other postback.

Set validation as false won't work in this case, the dropdown will lose the new item anyway.

I did some researches, and the possible solutions:

1)Using ViewState? Request Form? could anyone give an example?

2)Add dropdownlist item in server side. But I want to make the whole tr row clickable to fire the event. Could I click on tr and fire eg. its first child linkbutton or a linkbutton in somewhere else.

3)I've tried Jquery.ajax and webmethod , it can fire server event but since all method are static (shared) , it doesn't allow me to edit the downdownlist instance.

Any kinds of solution will be helpful!!

Regards,

回答1:

You should trigger a server side event handler to update the list from javascript as described in this article. http://codeclimber.net.nz/archive/2007/06/26/how-to-refresh-an-updatepanel-from-javascript.aspx