如何返回已被删除的列表项?

发布于 2024-11-15 17:39:09 字数 2019 浏览 1 评论 0原文

我有一个带有多个选项的复选框列表。所选选项将从第二个复选框列表中删除。这在下面的代码中工作得很好。问题是当用户更改复选框列表 1 中的选定选项时,第二个复选框列表仍然删除了原始选项。我怎样才能改变这个?注意:下拉列表可帮助用户继续填写表单。

页.aspx

   <asp:CheckBoxList ID="CheckBoxList1" runat="server" AutoPostBack="true">           <asp:ListItem Value="3">Italian</asp:ListItem>
    <asp:ListItem Value="6">Chinese</asp:ListItem>
    <asp:ListItem Value="7">Japanese</asp:ListItem>
    <asp:ListItem Value="8">Russian</asp:ListItem>
    <asp:ListItem Value="9">Arabic</asp:ListItem>
    <asp:ListItem Value="10">Hebrew</asp:ListItem>
    <asp:ListItem Value="11">Persian</asp:ListItem>
    <asp:ListItem Value="12">Turkish</asp:ListItem>
    </asp:CheckBoxList>

...

       <asp:DropDownList ID="DropDownList1" Width="100px" runat="server"  AutoPostBack="true" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
  <asp:ListItem Value="">Select</asp:ListItem>
  <asp:ListItem Value="Yes">Yes</asp:ListItem>
  <asp:ListItem Value="No">No</asp:ListItem>
   </asp:DropDownList>   

页.aspx.vb

   Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged

    Dim li2 As ListItem
    Dim values As String = ""

    For i As Integer = 0 To CheckBoxList1.Items.Count - 1
        If CheckBoxList1.Items(i).Selected Then
            values += CheckBoxList1.Items(i).Value + ","
        End If
    Next

    values = values.TrimEnd(","c)
    Dim ints As String() = values.ToString.Split(",")
    Dim y As Integer

    If DropDownList1.SelectedValue = "Yes" Then
    For y = 0 To UBound(ints)
            li2 = CheckBoxList2.Items.FindByValue(ints(y))
            If Not IsNothing(li2) Then
                CheckBoxList2.Items.Remove(li2)
            End If
      Next
  end if

  End Sub

I have a checkboxlist whith several options. The option(s) selected is/are removed from a second checkboxlist. This works fine in the below code. The problem is when the user changes the selected option in checkboxlist 1 the second checkboxlist still has the original option removed. How can I change this? Note: the dropdownlist helps the user continue with the form.

page.aspx

   <asp:CheckBoxList ID="CheckBoxList1" runat="server" AutoPostBack="true">           <asp:ListItem Value="3">Italian</asp:ListItem>
    <asp:ListItem Value="6">Chinese</asp:ListItem>
    <asp:ListItem Value="7">Japanese</asp:ListItem>
    <asp:ListItem Value="8">Russian</asp:ListItem>
    <asp:ListItem Value="9">Arabic</asp:ListItem>
    <asp:ListItem Value="10">Hebrew</asp:ListItem>
    <asp:ListItem Value="11">Persian</asp:ListItem>
    <asp:ListItem Value="12">Turkish</asp:ListItem>
    </asp:CheckBoxList>

...

       <asp:DropDownList ID="DropDownList1" Width="100px" runat="server"  AutoPostBack="true" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
  <asp:ListItem Value="">Select</asp:ListItem>
  <asp:ListItem Value="Yes">Yes</asp:ListItem>
  <asp:ListItem Value="No">No</asp:ListItem>
   </asp:DropDownList>   

page.aspx.vb

   Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged

    Dim li2 As ListItem
    Dim values As String = ""

    For i As Integer = 0 To CheckBoxList1.Items.Count - 1
        If CheckBoxList1.Items(i).Selected Then
            values += CheckBoxList1.Items(i).Value + ","
        End If
    Next

    values = values.TrimEnd(","c)
    Dim ints As String() = values.ToString.Split(",")
    Dim y As Integer

    If DropDownList1.SelectedValue = "Yes" Then
    For y = 0 To UBound(ints)
            li2 = CheckBoxList2.Items.FindByValue(ints(y))
            If Not IsNothing(li2) Then
                CheckBoxList2.Items.Remove(li2)
            End If
      Next
  end if

  End Sub

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

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

发布评论

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

评论(2

抱着落日 2024-11-22 17:39:09

最简单的解决方案是在 CheckBoxList2 中设置 EnableViewState="false"

但我担心这会给你带来其他麻烦。如果是这样,您需要在回发期间保留 CheckBoxList2 中的完整项目列表,或者至少保留那些与 CheckBoxList1 共享值的项目(并且可以删除)。如果这些共享项目的标签也相同,您可以使用 CheckBoxList1 在 CheckBoxList2 中重新创建共享项目,然后再删除选中的项目。这样您就不需要保留额外的数据。

但我不会让这件事变得太复杂。清除 CheckBoxList2、检索并添加所有项目以及删除 CheckBoxList1 中选中的项目可能是最好的方法。此外,您可能希望存储并重新应用 CheckBoxList2 中项目的选中状态。

The most simple solution is to set EnableViewState="false" in CheckBoxList2.

But i'm afraid that this may cause you other trouble. If so, you'll need to persist the full list of items in CheckBoxList2 across postbacks, or at least those items that share values with CheckBoxList1 (and can be removed). If the labels of these shared items are the same too, you can use CheckBoxList1 to recreate the shared items in CheckBoxList2, before removing the checked items. This way you wouldn't need to persist additional data.

But I wouldn't make this thing too complex. Clearing the CheckBoxList2, retrieving and adding all items, and removing the ones checked in CheckBoxList1 may be the best way to go. In addition, you may want to store and reapply the selected state of the items in CheckBoxList2.

世态炎凉 2024-11-22 17:39:09

在我看来,您需要保留 checkboxlist2 中项目列表的完整版本,以便您可以在 checkboxlist1 的选择更改时清除它并将所有项目添加到 checkboxlist2 中。

It sounds to me like you need to retain the full version of the list of items in checkboxlist2 so you can clear it and add all the items to checkboxlist2 when the selection of checkboxlist1 changes.

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