Question

I have two list boxes among which one is source and other is destination.

I want to transfer the selected item to the destination list box after the button click event.

I have searched over the internet and found the sample from here. But in my case its not working.

My ASP source file is :

<asp:Table ID="tbvl" Width="100%" runat="server">
    <asp:TableRow>
        <asp:TableCell Width="45%">
            <asp:ListBox ID="lstsource" CssClass="uppercase" runat="server" Width="100%"  Height="140" SelectionMode="Multiple"></asp:ListBox>
        </asp:TableCell>
        <asp:TableCell Width="10%" HorizontalAlign="Center" Height="100%">
            <asp:Table ID="Table1" runat="server" Height="100%">
                  <asp:TableRow>
                        <asp:TableCell Height="25%"><asp:Button ID="btnsd" CssClass="button" runat="server"  Text=">>" Width="40" /></asp:TableCell>
                  </asp:TableRow>
                  <asp:TableRow>
                        <asp:TableCell Height="25%"><asp:Button ID="btnds" runat="server" CssClass="button" Text="<<" Width="40" /></asp:TableCell>
                  </asp:TableRow>
                  <asp:TableRow>
                        <asp:TableCell Height="25%"><asp:Button ID="btnallsd" runat="server" CssClass="button" Text=">" Width="40" /></asp:TableCell>
                  </asp:TableRow>
                  <asp:TableRow>
                        <asp:TableCell Height="25%"><asp:Button ID="btnallds" runat="server" CssClass="button" Text="<" Width="40" /></asp:TableCell>
                  </asp:TableRow>
              </asp:Table>
          </asp:TableCell>
          <asp:TableCell Width="50%">
               <asp:ListBox ID="lstdestination" runat="server" CssClass="uppercase" Width="100%" Height="140" SelectionMode="Multiple"></asp:ListBox>                            
          </asp:TableCell>
       </asp:TableRow>

</asp:Table>

And my Click() event is;

void btnallsd_Click(object sender, EventArgs e)
{
    for (int i = lstsource.Items.Count - 1; i >= 0; i--)
    {
        if (lstsource.Items[i].Selected)
        {
            lstdestination.Items.Add(lstsource.Items[i]);
            lstdestination.ClearSelection();
            lstsource.Items.Remove(lstsource.Items[i]);
        }
    }
}

When I click the button page is only getting refreshed but the items are not added to the destination list box.

What should I do to get proper output.

Please help.

Was it helpful?

Solution

You have to ensure that the ListBoxes aren't databound on postbacks first. Otherwise you would fill them always with their default items and they would be unselected again.

So use the IsPostBack property, e.g.:

protected void Page_Load(Object sendeer, EventArgs e)
{
    if(!IsPostBack)
    {
        DataBindListBoxes();
    }
}

You also have to make the button-click event-handler at least protected and add the event handler to the aspx markup(or programmatically in codebehind):

codebehind:

protected void btnallsd_Click(object sender, EventArgs e){//...}

aspx:

<asp:Button ID="btnallsd" OnClick="btnallsd_Click" ....

To find the selected items you could also use Linq:

protected void btnallsd_Click(object sender, EventArgs e)
{
    var selected = lstsource.Items.Cast<ListItem>()
                   .Where(li => li.Selected);
    while (selected.Any())
    {
        var item = selected.First();
        lstdestination.Items.Add(item);
        lstsource.Items.Remove(item);
    }
    lstdestination.ClearSelection();
}

OTHER TIPS

Um try this:

 private void MoveListBoxItems(ListBox source, ListBox destination)
            {        
                ListBox.SelectedObjectCollection sourceItems = source.SelectedItems;
                foreach (var item in sourceItems)
                {
                    destination.Items.Add(item);
                }
                while (source.SelectedItems.Count > 0)
                {
                    source.Items.Remove(source.SelectedItems[0]);
                }
            }

Use: On the click event of your move from 1 to 2 button:

MoveListBoxItems(listBox1, listBox2);

Try This

protected void moveRight_Click(object sender, EventArgs e)
   {
   for (int i = 0; i < lbFirst.Items.Count; i++)
   {
   if (lbFirst.Items[i].Selected)
   {
   lbSecond.Items.Add(lbFirst.Items[i]);
   lbFirst.Items.Remove(lbFirst.Items[i]);
   }
   }
   }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top