Move items from one listbox to another listbox in Asp.net

Here I will explain how to move the listitems from one listbox to another listbox . The ListBox control is used to display a list of items to the user that the user can select by clicking. A ListBox control can provide single or multiple selections using the SelectionMode property. This tutorial shows you how to move items from one listbox to other listbox


Design your aspx page like this 


<head runat="server">
    <title>ListBox</title>
    <style type="text/css">
        .style1
        {
            background-color: #00FFCC;
        }
        .style3
        {
            color: #FFFFFF;
            font-weight: 700;
            background-color: #67E01F;
            height: 26px;
        }
        .style4
        {
            font-weight: bold;
            background-color: #00FFCC;
            color: #FFFFCC;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <table align="center">
    <tr>
    <td align="center" colspan="3" class="style1">
        <h2>
        <span class="style4">ListBox </span><span class="style1">&nbsp;</span></h2>
        </td></tr>
    <tr>
    <td>
        <h4>
        <asp:ListBox ID="ListBox1" runat="server" Height="166px" Width="144px" 
            CssClass="style3" SelectionMode="Multiple">
        <asp:listitem value="India">India</asp:listitem>
            <asp:ListItem Value="USA">USA</asp:ListItem>
            <asp:ListItem Value="UK">UK</asp:ListItem>
            <asp:ListItem Value="China">China</asp:ListItem>
            <asp:ListItem Value="France">France</asp:ListItem>
        </asp:ListBox>
        </h4>
    </td>
    <td>
<table>
<tr>
<td>
<asp:Button ID="btn1" runat="server" Text=">" Width="45px" onclick="btn1_Click" 
        CssClass="style3" />
</td>
</tr>
<tr>
<td>
<asp:Button ID="btn2" runat="server" Text=">>" Width="45px" onclick="btn2_Click" 
        CssClass="style3" />
</td>
</tr>
<tr>
<td>
<asp:Button ID="btn3" runat="server" Text="<" Width="45px" onclick="btn3_Click" 
        CssClass="style3" />
</td>
</tr>
<tr>
<td>
<asp:Button ID="btn4" runat="server" Text="<<" Width="45px" onclick="btn4_Click" 
        CssClass="style3" />
</td>
</tr>
</table>
</td>
<td><asp:ListBox ID="ListBox2" runat="server" Height="169px" Width="121px"  
        SelectionMode="Multiple" CssClass="style3">
<asp:listitem Value="Asia">Asia</asp:listitem>
<asp:listitem Value="NA">NA</asp:listitem>
<asp:listitem Value="Europe">Europe</asp:listitem>
    <asp:ListItem Value="Africa">Africa</asp:ListItem>
</asp:ListBox>
</td>
</tr><tr>
<td colspan="3" >
<asp:Label ID="lblmsg" runat="server" ForeColor="Black" style="color: #3333FF" ></asp:Label>
</td>
</tr>
</table></div>
    </form>
</body>
</html>

Code in .cs file

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;

public partial class _Default : System.Web.UI.Page
{
    ArrayList array1 = new ArrayList();
    ArrayList array2 = new ArrayList();
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btn1_Click(object sender, EventArgs e)
    {
        if (ListBox1.SelectedIndex >= 0)
        {
            for (int i = 0; i < ListBox1.Items.Count; i++)
            {
                if (ListBox1.Items[i].Selected)
                {
                    if (!array1.Contains(ListBox1.Items[i]))
                    {
                        array1.Add(ListBox1.Items[i]);
                    }
                }
            }

            for (int i = 0; i < array1.Count; i++)
            {
                if (!ListBox2.Items.Contains(((ListItem)array1[i])))
                {
                    ListBox2.Items.Add(((ListItem)array1[i]));
                }
                ListBox1.Items.Remove((ListItem)array1[i]);
            }

        }
        else
        {

            lblmsg.Text = "Please select atleast one in Listbox1 to move";
        }
    }
    protected void btn2_Click(object sender, EventArgs e)
    {
  
        while (ListBox1.Items.Count != 0)
        {
            for (int i = 0; i < ListBox1.Items.Count; i++)
            {
                ListBox2.Items.Add(ListBox1.Items[i]);
                ListBox1.Items.Remove(ListBox1.Items[i]);
            }
        }
    }
    protected void btn3_Click(object sender, EventArgs e)
    {
        if (ListBox2.SelectedIndex >= 0)
        {
            for (int i = 0; i < ListBox2.Items.Count; i++)
            {
                if (ListBox2.Items[i].Selected)
                {
                    if (!array2.Contains(ListBox2.Items[i]))
                    {
                        array2.Add(ListBox2.Items[i]);
                    }
                }
            }
            for (int i = 0; i < array2.Count; i++)
            {
                if (!ListBox1.Items.Contains(((ListItem)array2[i])))
                {
                    ListBox1.Items.Add(((ListItem)array2[i]));
                }
                ListBox2.Items.Remove(((ListItem)array2[i]));
            }
            
        }
        else
        {
         
            lblmsg.Text = "Please select atleast one in Listbox2 to move";
        }
    }
    protected void btn4_Click(object sender, EventArgs e)
    {
        while (ListBox2.Items.Count != 0)
        {
            for (int i = 0; i < ListBox2.Items.Count; i++)
            {
                ListBox1.Items.Add(ListBox2.Items[i]);
                ListBox2.Items.Remove(ListBox2.Items[i]);
            }
        }
    }
}

Comments

Popular posts from this blog

Download Visakhapatnam Tirupati AC Double Decker Express Act