Add or remove items from one checkboxlist to other in asp.net using C#,VB

Introduction: In this article I am going to share how to add or remove items from one checkboxlist to another in asp.net using both C# and VB languages.
Add or remove items from one checkboxlist to other in asp.net using both C# and VB

Description: While working on project I got the requirement to implement a feature to shortlist the eligible participants from all candidates.  There were many other ways to do so but I decided to fill all the candidates in one checkboxlist and provided the option to select any or all participants from that. Similarly we can remove all or any of the selected participants as shown in demo image above.

Implementation: Let’s create a demo page to demonstrate the concept.

HTML Source Code
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
            <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                <ContentTemplate>
                    <fieldset style="width: 400px;">
                        <legend>Add/Remove Items from One CheckBoxList to other</legend> 
                        <table style="width: 400px">
                            <tr>
<asp:CheckBoxList ID="cbParticipants" runat="server">
                            <asp:ListItem Text="Aryan" Value="1"></asp:ListItem>
                            <asp:ListItem Text="Mayank" Value="2"></asp:ListItem>
                            <asp:ListItem Text="Anjan" Value="3"></asp:ListItem>
                            <asp:ListItem Text="Ankita" Value="4"></asp:ListItem>
                            <asp:ListItem Text="Nikita" Value="5"></asp:ListItem> 
                            <asp:ListItem Text="Abha" Value="6"></asp:ListItem>
                        </asp:CheckBoxList></td>
                                <td style="text-align: center;">
                                    <asp:Button ID="btnAdd" Width="120px" runat="server" Text="Add Selected" OnClick="btnAdd_Click" /><br />
                                    <asp:Button ID="btnAddAll" Width="120px" runat="server" Text="Add All" OnClick="btnAddAll_Click" /><br />
                                    <asp:Button ID="btnRemove" Width="120px" runat="server" Text="Remove Selected" OnClick="btnRemove_Click" /><br />
                                    <asp:Button ID="btnRemoveAll" Width="120px" runat="server" Text="Remove All" OnClick="btnRemoveAll_Click" /></td>
                                <td style="background-color:#3b5998; color:#ffffff;vertical-align: top;">Selected Participants
                        <asp:CheckBoxList ID="cbSelectedParticipants" runat="server">
                        </asp:CheckBoxList>
                                </td>
                            </tr>
                            <tr><td style="text-align:center;" colspan="3"><asp:Label ID="lblMsg"  ForeColor="Red" runat="server"></asp:Label></td></tr>
                        </table>                       
                    </fieldset>
                </ContentTemplate>
            </asp:UpdatePanel>
        </div>
    </form>
</body>
</html>

Asp.Net C# Code to add or remove items from one checkboxlist to other

 protected void btnAdd_Click(object sender, EventArgs e)
    {
        //Check if any participant selected to add or not
        if (cbParticipants.SelectedIndex >= 0)
        {
            lblMsg.Text = String.Empty;
            //Loop through each item of checkboxlist
            foreach (ListItem item in cbParticipants.Items)
            {
                //check if item selected
                if (item.Selected)
                {
                    // Add participant to the selected list if not alreay added
                    if (!IsParticipantExists(item.Value))
                    {
                        cbSelectedParticipants.Items.Add(item);
                    }
                }
            }
            //Uncheck all selected items
            cbParticipants.ClearSelection();
        }
        else
        {
            lblMsg.Text = "Please select at least one participant to add";
        }
    }   

    private bool IsParticipantExists(string val)
    {
        bool exists = false;
        //Loop through each item in selected participant checkboxlist
        foreach (ListItem item in cbSelectedParticipants.Items)
        {
            //Check if item selected already exists in the selected participant checboxlist or not
            if (item.Value == val)
            {
                exists = true;
                break;
            }
        }
        return exists;
    }

    protected void btnAddAll_Click(object sender, EventArgs e)
    {
        lblMsg.Text = String.Empty;
        //Remove all existing items from checkboxlist
        cbSelectedParticipants.Items.Clear();
        //Loop through all items of first checkboxlist and add in other checkboxlist
        foreach (ListItem item in cbParticipants.Items)
        {
            cbParticipants.ClearSelection();
            cbSelectedParticipants.Items.Add(item);
        }       
    }

    protected void btnRemove_Click(object sender, EventArgs e)
    {
// Check if there are items in the checkboxlist or not
        if (cbSelectedParticipants.Items.Count > 0)
        {
            //Check if any participant selected to remove or not
            if (cbSelectedParticipants.SelectedIndex >= 0)
            {
                lblMsg.Text = String.Empty;
                //Loop through each item in checkboxlist
                for (int i = cbSelectedParticipants.Items.Count - 1; i >= 0; i--)
                {
                    //Check if item selected
                    if (cbSelectedParticipants.Items[i].Selected)
                    {
                        //remove the selected item
                        cbSelectedParticipants.Items.RemoveAt(i);
                    }
                }
            }
            else
            {
                lblMsg.Text = "Please select at least one participant to remove";
            }
        }
        else
        {
            lblMsg.Text = "No participant(s) to remove";
        }
    }

    protected void btnRemoveAll_Click(object sender, EventArgs e)
    {
// Check if there are items in the checkboxlist or not
        if (cbSelectedParticipants.Items.Count > 0)
        {
            lblMsg.Text = String.Empty;
            //Remove all items from checkboxlist
            cbSelectedParticipants.Items.Clear();
        }
        else
        {
            lblMsg.Text = "No participant(s) to remove";
        }
    }

Asp.Net VB Code to add or remove items from one checkboxlist to other

     Protected Sub btnAdd_Click() Handles btnAdd.Click
        'Check if any participant selected to add or not
        If cbParticipants.SelectedIndex >= 0 Then
            lblMsg.Text = String.Empty
            'Loop through each item of checkboxlist
            For Each item As ListItem In cbParticipants.Items
                'check if item selected
                If item.Selected Then
                    ' Add participant to the selected list if not alreay added
                    If Not IsParticipantExists(item.Value) Then
                        cbSelectedParticipants.Items.Add(item)
                    End If
                End If
            Next
            'Uncheck all selected items
            cbParticipants.ClearSelection()
        Else
            lblMsg.Text = "Please select at least one participant to add"
        End If
    End Sub

    Private Function IsParticipantExists(val As String) As Boolean
        Dim exists As Boolean = False
        'Loop through each item in selected participant checkboxlist
        For Each item As ListItem In cbSelectedParticipants.Items
            'Check if item selected already exists in the selected participant checboxlist or not
            If item.Value = val Then
                exists = True
                Exit For
            End If
        Next
        Return exists
    End Function

    Protected Sub btnAddAll_Click() Handles btnAddAll.Click
        lblMsg.Text = String.Empty
        'Remove all existing items from checkboxlist
        cbSelectedParticipants.Items.Clear()
        'Loop through all items of first checkboxlist and add in other checkboxlist
        For Each item As ListItem In cbParticipants.Items
            cbParticipants.ClearSelection()
            cbSelectedParticipants.Items.Add(item)
        Next
    End Sub

    Protected Sub btnRemove_Click(sender As Object, e As EventArgs) Handles btnRemove.Click
 'Check if there are items in the checkboxlist or not       
If cbSelectedParticipants.Items.Count > 0 Then
            'Check if any participant selected to remove or not
            If cbSelectedParticipants.SelectedIndex >= 0 Then
                lblMsg.Text = String.Empty
                'Loop through each item in checkboxlist
                For i As Integer = cbSelectedParticipants.Items.Count - 1 To 0 Step -1
                    'Check if item selected
                    If cbSelectedParticipants.Items(i).Selected Then
                        'remove the selected item
                        cbSelectedParticipants.Items.RemoveAt(i)
                    End If
                Next
            Else
                lblMsg.Text = "Please select at least one participant to remove"
            End If
        Else
            lblMsg.Text = "No participant(s) to remove"
        End If
    End Sub

    Protected Sub btnRemoveAll_Click() Handles btnRemoveAll.Click
  'Check if there are items in the checkboxlist or not     
  If cbSelectedParticipants.Items.Count > 0 Then
            lblMsg.Text = String.Empty
            'Remove all items from checkboxlist
            cbSelectedParticipants.Items.Clear()
        Else
            lblMsg.Text = "No participant(s) to remove"
        End If
    End Sub
Now over to you:
"A blog is nothing without reader's feedback and comments. So please provide your valuable feedback so that i can make this blog better and If you like my work; you can appreciate by leaving your comments, hitting Facebook like button, following on Google+, Twitter, Linkedin and Pinterest, stumbling my posts on stumble upon and subscribing for receiving free updates directly to your inbox . Stay tuned and stay connected for more technical updates." 
Previous
Next Post »

If you have any question about any post, Feel free to ask.You can simply drop a comment below post or contact via Contact Us form. Your feedback and suggestions will be highly appreciated. Also try to leave comments from your account not from the anonymous account so that i can respond to you easily..