1
votes

I have two listboxes which are populated at runtime in the web page Page_Load event. I am using Page.IsPostBack to make sure the list is only populated when it's not postback.

listbox A has about 150 items and listbox B is empty. I have two buttons, Add and Remove. I select Items in listbox A and press the Add button and those items are moved to Listbox B.

I'm having problem selecting these items in Listbox B. If I set Autopostback to TRUE, I cannot select any Items. When I select one Item, postback happens and my selection is cleared as if no item has been selected, even when I set Enableviewstate to TRUE.

If I set Autopostback to FALSE, I can select Items without any problem, but when I check the Selected property of these Items on server side they are all FALSE.

no matter what autopostback is set to, I can see the populated items on server side, only the selected property is always false.

All listbox items and their values are unique.

any suggestions about solving this problem?

Here is the code I'm using to populate listbox b in Add button's click event:

    For i = 0 To (ListboxA.Items.Count - 1)
        If ListboxA.Items.Item(i).Selected = True Then
            ListboxB.Items.Add(ListboxA.Items.Item(i))
        End If
    Next
    For i = 0 To (ListboxB.Items.Count - 1)
        If ListboxA.Items.Contains(ListboxB.Items.Item(i)) Then
            ListboxA.Items.Remove(ListboxB.Items.Item(i))
        End If
        ListboxB.Items.Item(i).Selected = False
    Next

and the listboxes :

    <asp:ListBox ID="ListboxA" runat="server" SelectionMode="Multiple" AutoPostBack="True"></asp:ListBox>
    <asp:ListBox ID="ListboxB" runat="server" SelectionMode="Multiple" AutoPostBack="True"></asp:ListBox>
1
@YardenST I have edited my post to include the codeRahmatizadeh

1 Answers

0
votes

Selection must be going because you are doing listbox.databind() on every postback. do it only if its not postback and your selection will remain even after postbacks.