Move items from one CheckBoxList to other one

In this blog we will know how to move items from one CheckBoxList to other one.

 

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title>Untitled Page</title>

</head>

<body>

    <form id="form1" runat="server">

    <div>

   

    </div>

 

    <asp:Panel ID="Panel1" runat="server" Height="186px" Width="178px"

        BorderColor="#993333" BorderStyle="Outset" ForeColor="#CC3300">

        <asp:CheckBoxList ID="CheckBoxList1" runat="server">

        </asp:CheckBoxList>

       

    </asp:Panel>

    <asp:Button ID="Button1" runat="server" Text="&gt;" onclick="Button1_Click"

        Width="50px" />

    <asp:Button ID="Button2" runat="server" Text="&lt;" onclick="Button2_Click"

        Width="50px" />

    <asp:Panel ID="Panel2" runat="server" Height="186px" Width="178px"

        BorderColor="Maroon" BorderStyle="Outset">

        <asp:CheckBoxList ID="CheckBoxList2" runat="server">

        </asp:CheckBoxList>

    </asp:Panel>

    </form>

</body>

</html>

 

 

using System;

using System.Configuration;

using System.Data;

using System.Linq;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.HtmlControls;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Xml.Linq;

using System.Collections;

public partial class _Default : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

        if (!IsPostBack)

        {

            CheckBoxList1.Items.Add("");

            CheckBoxList1.Items.Add("Raj");

            CheckBoxList1.Items.Add("Ravi");

            CheckBoxList1.Items.Add("Rahul");

            CheckBoxList1.Items.Add("Raju");

            CheckBoxList1.Items.Add("Rohit");

            CheckBoxList1.Items.Add("Rajesh");

           

        }

    }

 

    protected void Button1_Click(object sender, EventArgs e)

    {

       

        for (int i = CheckBoxList1.Items.Count - 1; i >= 0; i--)

        {

 

            if (CheckBoxList1.Items[i].Selected)

            {

 

                CheckBoxList2.Items.Add(CheckBoxList1.Items[i]);

                CheckBoxList2.ClearSelection();

 

                CheckBoxList1.Items.Remove(CheckBoxList1.Items[i]);

 

            }

 

        }

    }

    protected void Button2_Click(object sender, EventArgs e)

    {

        for (int i = CheckBoxList2.Items.Count - 1; i >= 0; i--)

        {

 

            if (CheckBoxList2.Items[i].Selected)

            {

 

                CheckBoxList1.Items.Add(CheckBoxList2.Items[i]);

 

                CheckBoxList1.ClearSelection();

 

                CheckBoxList2.Items.Remove(CheckBoxList2.Items[i]);

 

            }

 

        }

    }

}