Link to home
Start Free TrialLog in
Avatar of chuang4630
chuang4630

asked on

How do you remove the item from the list box control?

I'd like to move the selected item (single selection list box) from     lstAvailableTemplates to lstAssignedTemplates.
When the user press the button "Add", the item is supposed to move from one list to another. However, I can see the new item being added to the destination, but NOT being removed from the lst lstAvailableTemplates (list box control).

Anyone knows why?


protected void btnAdd_Click(object sender, EventArgs e)
    {
        string item = lstAvailableTemplates.SelectedItem.ToString();
        lstAssignedTemplates.Items.Add(item);
        lstAvailableTemplates.Items.Remove(item);
    }
ASKER CERTIFIED SOLUTION
Avatar of REA_ANDREW
REA_ANDREW
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
you used string item, but you should use ListItem, for my example here is the accompanying HTML

        <asp:ListBox ID="ListBox1" runat="server">
        <asp:ListItem Text=1 Value=1></asp:ListItem>
        <asp:ListItem Text=1 Value=1></asp:ListItem>
        <asp:ListItem Text=1 Value=1></asp:ListItem>
        </asp:ListBox>
        <asp:ListBox ID="ListBox2" runat="server">
        <asp:ListItem Text=1 Value=1></asp:ListItem>
        <asp:ListItem Text=1 Value=1></asp:ListItem>
        <asp:ListItem Text=1 Value=1></asp:ListItem>
        </asp:ListBox>
        <asp:Button ID="Button2" runat="server" Text="Button" OnClick="Button2_Click" /
Avatar of chuang4630
chuang4630

ASKER

It works, Thanks. But there is anothere probelm.
Select one item/press button, it works. When I select the second item/press button, it fails: System.Web.HttpException: Cannot have multiple items selected when the SelectionMode is Single.
How do I fix that while keeping the single selection mode?

Hi,

User ClearSelection to clear the selection before select the section item.

For Example,

ListBox2.ClearSelection()


Thanks

mrrajpatel:
Thanks. Could you please post your answer to the following link so I can give you the points? Thanks again.

https://www.experts-exchange.com/questions/21868037/How-to-select-remove-the-item-from-list-box-single-selection-mode.html