如何使用javascript清除asp.net复选框列表中的所有项目并添加新项目

发布于 2025-01-01 14:23:35 字数 358 浏览 2 评论 0原文

我有一个复选框列表,就像

<asp:CheckBoxList id="CheckBoxList1" runat="server"  >
 <asp:listitem Value="1">Item 1</asp:listitem>
  <asp:listitem Value="2">Item 2</asp:listitem>
</asp:CheckBoxList>`.

我需要从复选框列表中清除所有项目(例如项目 1 和项目 2),并使用 javascript 在复选框列表中添加新值(例如“项目 3”)。现在有人可以解决这个问题吗?

谢谢...

I have one checkbox list like

<asp:CheckBoxList id="CheckBoxList1" runat="server"  >
 <asp:listitem Value="1">Item 1</asp:listitem>
  <asp:listitem Value="2">Item 2</asp:listitem>
</asp:CheckBoxList>`.

I need to clear all the items such as Item 1 and Item 2 from checkboxlist and add the new value such as "Item 3" in the checkbox list using javascript. Can anyone k now the solution for that problem?

Thank you...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

仅供记录: 是一个服务器端控件。如果您删除这些项目并使用 Javascript(客户端)添加一个新项目,然后您回发到服务器,您将丢失在客户端完成的所有操作。

我的建议是:回发到服务器并使用 CheckBoxList 服务器端标准事件处理项目的删除和添加操作。会简单很多!

Just for the record: <asp:CheckBoxList ...> is a server-side control. If you remove the items and add a new one using Javascript (client-side) and after that you do a post-back to the server, you'll lose everything that was done on the client side.

My advice is: do a post-back to the server and handle the items' remove and add operations using the CheckBoxList server-side standard events. It'll be much simpler!

獨角戲 2025-01-08 14:23:35

删除不是问题,但如果添加新值然后执行回发,您将开始从 ASP.NET 收到错误。如果它看到从静态源(下拉列表、复选框列表、单选按钮列表等)发回的信息,而该信息不是所发送的提供的选项之一,那么它会假设某些恶意用户正在尝试滥用该应用程序,并且它会执行以下操作:吓坏了(打破页面)。

如果您不想进行整个页面回发来执行此更改,那么您可以将复选框列表包装在 UpdatePanel 中(以及触发列表更改所需的任何内容,如果适用),这样就不会出现全页回发只是为了更改项目。这意味着编写服务器端代码来更改列表中的项目。

The removing isn't a problem, but if you add new values and then perform a postback you'll start getting errors from ASP.NET. If it sees information posted back from a static source (drop down list, check box list, radio button list, etc.) that wasn't one of the provided options sent then it assumes some malicious user is attempting to abuse the application and it freaks out (breaking the page).

If you don't want to do an entire page postback to perform this change then you can wrap the checkbox list in an UpdatePanel (along with whatever you need to trigger the changes to the list, if applicable) so that there isn't a full page postback just to change the items. This will mean writing server side code to change the items in the list.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文