Move items from one CheckBoxList to another in ASP.Net

Mohammed Imtiyaz Jan 9, 2015

This tutorial will explain you how to move list items from one CheckBoxList to another CheckBoxList in ASP.Net using C#

ASP.Net

<asp:Label ID="lblMsg" runat="server"></asp:Label>
<table class="table table-bordered">
    <thead>
        <tr>
            <th>CheckBosList 1</th>
            <th></th>
            <th>CheckBoxList 2</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>
                <asp:CheckBoxList ID="CheckBoxList1" runat="server" CssClass="checkbox-inline"></asp:CheckBoxList>
            </td>
            <td class="text-center">
                <button type="submit" id="btnLTR" class="button" runat="server" onserverclick="btnLTR_ServerClick" data-loading-text="<i class='fa fa-refresh fa-spin' style='font-size:16px; color:#fff;'></i>">
                    <i class="fa fa-chevron-right" style="color: #fff;"></i>
                </button>
                <br />
                <button type="submit" id="btnRTL" class="button" runat="server" onserverclick="btnRTL_ServerClick" data-loading-text="<i class='fa fa-refresh fa-spin' style='font-size:16px; color:#fff;'></i>">
                    <i class="fa fa-chevron-left" style="color: #fff;"></i>
                </button>
            </td>
            <td>
                <asp:CheckBoxList ID="CheckBoxList2" runat="server" CssClass="checkbox-inline"></asp:CheckBoxList>
            </td>
        </tr>
    </tbody>
</table>

C#

using System;
using System.Web.UI.WebControls;

using System.Collections;  // For ArrayList

public partial class VC : System.Web.UI.Page
{
    ArrayList arraylist1 = new ArrayList();
    ArrayList arraylist2 = new ArrayList();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            CheckBoxList1.Items.Add("ASP.Net");
            CheckBoxList1.Items.Add("C#");
            CheckBoxList1.Items.Add("Vb.net");
            CheckBoxList1.Items.Add("jQuery");
            CheckBoxList1.Items.Add("Javascript");
            CheckBoxList1.Items.Add("SQL Server");
            CheckBoxList1.Items.Add("Ajax");
            CheckBoxList1.Items.Add("Html");
            CheckBoxList1.Items.Add("Css");
        }
    }
    protected void btnLTR_ServerClick(object sender, EventArgs e)
    {
        try
        {
            if (CheckBoxList1.SelectedIndex >= 0)
            {
                lblMsg.Text = String.Empty;
                for (int i = 0; i < CheckBoxList1.Items.Count; i++)
                {
                    if (CheckBoxList1.Items[i].Selected)
                        if (!arraylist1.Contains(CheckBoxList1.Items[i]))
                            arraylist1.Add(CheckBoxList1.Items[i]);

                }
                for (int i = 0; i < arraylist1.Count; i++)
                {
                    if (!CheckBoxList2.Items.Contains(((ListItem)arraylist1[i])))
                        CheckBoxList2.Items.Add(((ListItem)arraylist1[i]));
                    CheckBoxList1.Items.Remove(((ListItem)arraylist1[i]));
                }
                CheckBoxList2.SelectedIndex = -1;
            }
            else
            {
                lblMsg.Text = "Please select atleast one item from CheckBoxList 1";
                if (CheckBoxList1.Items.Count == 0)
                    lblMsg.Text = "CheckBoxList 1 is empty";
            }
        }
        catch { }
    }

    protected void btnRTL_ServerClick(object sender, EventArgs e)
    {
        try
        {
            if (CheckBoxList2.SelectedIndex >= 0)
            {
                lblMsg.Text = String.Empty;
                for (int i = 0; i < CheckBoxList2.Items.Count; i++)
                {
                    if (CheckBoxList2.Items[i].Selected)
                        if (!arraylist2.Contains(CheckBoxList2.Items[i]))
                            arraylist2.Add(CheckBoxList2.Items[i]);
                }
                for (int i = 0; i < arraylist2.Count; i++)
                {
                    if (!CheckBoxList1.Items.Contains(((ListItem)arraylist2[i])))
                        CheckBoxList1.Items.Add(((ListItem)arraylist2[i]));
                    CheckBoxList2.Items.Remove(((ListItem)arraylist2[i]));
                }
                CheckBoxList1.SelectedIndex = -1;
            }
            else
            {
                lblMsg.Text = "Please select atleast one item from CheckBoxList 2";
                if (CheckBoxList2.Items.Count == 0)
                    lblMsg.Text = "CheckBoxList 2 is empty";
            }
        }
        catch { }
    }
}