Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 429
  • Last Modified:

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);
    }
0
chuang4630
Asked:
chuang4630
  • 2
  • 2
1 Solution
 
REA_ANDREWCommented:
here is why, you have to define a list item instead, I used your code with list boxes not drop down lists.

    protected void Button2_Click(object sender, EventArgs e)
    {
        ListItem item = ListBox1.SelectedItem;
        ListBox2.Items.Add(item);
        ListBox1.Items.Remove(item);
    }

0
 
REA_ANDREWCommented:
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" /
0
 
chuang4630Author Commented:
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?

0
 
Rajulkumar PatelCommented:
Hi,

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

For Example,

ListBox2.ClearSelection()


Thanks
0
 
chuang4630Author Commented:

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

http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/ASP_DOT_NET/Q_21868037.html
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now