Add asp.net dropdownlist item from JavaScript causes page postback error

こ雲淡風輕ζ 提交于 2019-12-04 06:24:23

问题


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



来源:https://stackoverflow.com/questions/5024154/add-asp-net-dropdownlist-item-from-javascript-causes-page-postback-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!