简体   繁体   English

在客户端C#/ ASP.NET的DropDownList上添加一个项目

[英]Add an item on DropDownList at client, C# / ASP.NET

I have a need to add an item to DropDownList client side, after I made some ajax calls. 我进行了一些ajax调用后,需要向DropDownList客户端添加一个项目。

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: 如果我只是使用jquery的方法将项目添加到相关的select中,则会将其添加到其中,但是当我稍后在下拉列表中选择此新的(添加客户端添加的)项目并进行回发时,就会出现问题:

Invalid postback or callback argument. 无效的回发或回调参数。 Event validation is enabled using in configuration or <%@ Page EnableEventValidation="true" %> in a page. 使用配置或页面中的<%@页面EnableEventValidation =“ true”%>启用事件验证。 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. 如果数据有效且预期,请使用ClientScriptManager.RegisterForEventValidation方法以注册回发或回调数据以进行验证。

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? 我将如何正确修改DropDownList,使新条目都出现在列表中,并且如果我选择了该新项进行回发,其值将进入服务器并进一步使用?

您应该使用ASP UpdatePanel,否则控件与ViewState所显示的控件肯定会出现问题。

There are two different strategies that I can suggest. 我可以建议两种不同的策略。

  1. 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. 将任何新项添加到下拉列表服务器端,通过PostBack调用服务器上的方法以将新项添加到列表绑定到的数据集,然后重新绑定下拉列表。

  2. 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. 绕过ViewState并从下拉列表中直接从提交回服务器的发布值中获取所选值,如下所示。 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(); selectedValue = Request.Form [-下拉列表ID-]。ToString();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM