Tuesday, September 18, 2012

Moving value from one ListBox to another ListBox using C#.NET

First the ListBox declaration set the SelectionMode=”Multiple”.

For more details, you can visit on below link.
www.code-sample.com/2013/06/add-remove-items-from-one-listbox-to.html

   <tr>       

<td class="call2">

  <asp:ListBox ID=" ListBox1" runat="server" SelectionMode="Multiple"></asp:ListBox>
</td>
<td class="call1">
  <asp:Button ID="btnadd" runat="server" Text=">>" OnClick="btnadd_Click"    />
                    <br />
 <asp:Button ID="btnsubtract" runat="server" Text="<<" OnClick="btnsubtract_Click" />
 </td>
<td class="call2">
  <asp:ListBox ID=" ListBox2" runat="server" SelectionMode="Multiple"></asp:ListBox>
  </td>
    </tr>


For adding items in the list
   protected void btnadd_Click(object sender, EventArgs e)
    {
        for (int i = ListBox1.Items.Count - 1; i >= 0; i--)
        {
            if (ListBox1.Items[i].Selected)
            {
                ListBox2.Items.Add(ListBox1.Items[i]);
                ListBox1.Items.Remove(ListBox1.Items[i]);
            }

        }
    }
 For removing items in the list
    protected void btnsubtract_Click(object sender, EventArgs e)
    {
        for (int i = ListBox2.Items.Count - 1; i >= 0; i--)
        {
            if (listBoxSelectedEmp.Items[i].Selected)
            {
                ListBox1.Items.Add(ListBox2.Items[i]);
                ListBox2.Items.Remove(ListBox2.Items[i]);
            }

        }
    }

1 comment:

Anonymous said...

I have read so mahy posts rеgarding tҺe blogger lovers
exϲept thіs article is tгuly a nice article,
keep it up.

Feell free tоo surf tο my рage ... casino på norsk