how to return listitem that has been removed?
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:Lis开发者_如何学编程tItem 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
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.
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.
精彩评论