Please disable your adblock and script blockers to view this page
 

Move items from one checkboxlist to other in asp.net using both C# and VB

Introduction: In this article I am going to share how to move single or all items back and forth between two checkboxlist controls in asp.net using both C# and VB languages.
Move items from one checkboxlist to other in asp.net using both C# and VB

Description: Here I have demonstrated how we can move items from first checkboxlist to second and vice versa by using four button controls. First button for moving selected item from first to second checkboxlist, second for moving all items whether selected or not, third for moving selected item back to first checkboxlist and fourth one is for moving all items back to first checkboxlist 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 id="Head1" 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>Move items from one CheckBoxList to other and vice-versa</legend>

                        <table style="width: 400px">
                            <tr>
                                <td style="background-color:#3b5998; color:#ffffff; vertical-align: top;">Select Participant
                        <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="btnMove" Width="60px" runat="server" Text=">" OnClick="btnMove_Click" /><br />
                                    <asp:Button ID="btnMoveAll" Width="60px" runat="server" Text=">>" OnClick="btnMoveAll_Click" /><br />
                                    <asp:Button ID="btnMoveBack" Width="60px" runat="server" Text="<" OnClick="btnMoveBack_Click" /><br />
                                    <asp:Button ID="btnMoveBackAll" Width="60px" runat="server" Text="<<" OnClick="btnMoveBackAll_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 move items from one checkboxlist to other

    protected void btnMove_Click(object sender, EventArgs e)
    {
        if (cbParticipants.Items.Count > 0)
        {
            //Check if any item selected to move or not
            if (cbParticipants.SelectedIndex >= 0)
            {
                lblMsg.Text = String.Empty;
                //Loop through each item of first checkboxlist
                for (int i = cbParticipants.Items.Count - 1; i >= 0; i--)
                {
                    //check if item selected
                    if (cbParticipants.Items[i].Selected)
                    {
                        //Move the selected item from first checkboxlist to second
                        cbSelectedParticipants.Items.Add(cbParticipants.Items[i]);
                        //uncheck all checked items
                        cbSelectedParticipants.ClearSelection();
                        //Remove moved item from first checkboxlist
                        cbParticipants.Items.Remove(cbParticipants.Items[i]);
                    }
                }
            }
            else
            {
                lblMsg.Text = "Please select at least one participant to move";
            }
        }
        else
        {
            lblMsg.Text = "No participant(s) to move";
        }
    }

    protected void btnMoveAll_Click(object sender, EventArgs e)
    {
        if (cbParticipants.Items.Count > 0)
        {
            lblMsg.Text = String.Empty;
            //Loop through all items of first checkboxlist and move to second checkboxlist
            foreach (ListItem item in cbParticipants.Items)
            {
                cbSelectedParticipants.Items.Add(item);
            }
            //Remove all moved items from first checkboxlist
            cbParticipants.Items.Clear();
        }
        else
        {
            lblMsg.Text = "No participant(s) to move";
        }
    }

    protected void btnMoveBack_Click(object sender, EventArgs e)
    {
        if (cbSelectedParticipants.Items.Count > 0)
        {
            //Check if any item selected to move back or not
            if (cbSelectedParticipants.SelectedIndex >= 0)
            {
                lblMsg.Text = String.Empty;
                //Loop through each item in second checkboxlist
                for (int i = cbSelectedParticipants.Items.Count - 1; i >= 0; i--)
                {
                    //Check if item selected
                    if (cbSelectedParticipants.Items[i].Selected)
                    {
                        //Move the selected item back to first checkboxlist
                        cbParticipants.Items.Add(cbSelectedParticipants.Items[i]);
                        //Uncheck all checked items
                        cbParticipants.ClearSelection();
                        //Remove moved item from second checkboxlist
                        cbSelectedParticipants.Items.Remove(cbSelectedParticipants.Items[i]);
                    }
                }
            }
            else
            {
                lblMsg.Text = "Please select at least one participant to move back";
            }
        }
        else
        {
            lblMsg.Text = "No participant(s) to move back";
        }
    }

    protected void btnMoveBackAll_Click(object sender, EventArgs e)
    {
        if (cbSelectedParticipants.Items.Count > 0)
        {
            lblMsg.Text = String.Empty;
            //Loop through all items of second checkboxlist and add in first checkboxlist
            foreach (ListItem item in cbSelectedParticipants.Items)
            {
                cbParticipants.Items.Add(item);
            }
            //Remove all items from second checkbox
            cbSelectedParticipants.Items.Clear();
        }
        else
        {
            lblMsg.Text = "No participant(s) to move back";
        }
    }

Asp.Net VB Code to move items from one checkboxlist to other

    Protected Sub btnMove_Click() Handles btnMove.Click
        If cbParticipants.Items.Count > 0 Then
            'Check if any item selected to move or not
            If cbParticipants.SelectedIndex >= 0 Then
                lblMsg.Text = String.Empty
                'Loop through each item of first checkboxlist
                For i As Integer = cbParticipants.Items.Count - 1 To 0 Step -1
                    'check if item selected
                    If cbParticipants.Items(i).Selected Then
                        'Move the selected item from first checkboxlist to second
                        cbSelectedParticipants.Items.Add(cbParticipants.Items(i))
                        'uncheck all checked items
                        cbSelectedParticipants.ClearSelection()
                        'Remove moved item from first checkboxlist
                        cbParticipants.Items.Remove(cbParticipants.Items(i))
                    End If
                Next
            Else
                lblMsg.Text = "Please select at least one participant to move"
            End If
        Else
            lblMsg.Text = "No participant(s) to move"
        End If
    End Sub

    Protected Sub btnMoveAll_Click() Handles btnMoveAll.Click
        If cbParticipants.Items.Count > 0 Then
            lblMsg.Text = String.Empty
            'Loop through all items of first checkboxlist and move to second checkboxlist
            For Each item As ListItem In cbParticipants.Items
                cbSelectedParticipants.Items.Add(item)
            Next
            'Remove all moved items from first checkboxlist
            cbParticipants.Items.Clear()
        Else
            lblMsg.Text = "No participant(s) to move"
        End If
    End Sub

    Protected Sub btnMoveBack_Click() Handles btnMoveBack.Click
        If cbSelectedParticipants.Items.Count > 0 Then
            'Check if any item selected to move back or not
            If cbSelectedParticipants.SelectedIndex >= 0 Then
                lblMsg.Text = String.Empty
                'Loop through each item in second checkboxlist
                For i As Integer = cbSelectedParticipants.Items.Count - 1 To 0 Step -1
                    'Check if item selected
                    If cbSelectedParticipants.Items(i).Selected Then
                        'Move the selected item back to first checkboxlist
                        cbParticipants.Items.Add(cbSelectedParticipants.Items(i))
                        'Uncheck all checked items
                        cbParticipants.ClearSelection()
                        'Remove moved item from second checkboxlist
                        cbSelectedParticipants.Items.Remove(cbSelectedParticipants.Items(i))
                    End If
                Next
            Else
                lblMsg.Text = "Please select at least one participant to move back"
            End If
        Else
            lblMsg.Text = "No participant(s) to move back"
        End If
    End Sub

    Protected Sub btnMoveBackAll_Click() Handles btnMoveBackAll.Click
        If cbSelectedParticipants.Items.Count > 0 Then
            lblMsg.Text = String.Empty
            'Loop through all items of second checkboxlist and add in first checkboxlist
            For Each item As ListItem In cbSelectedParticipants.Items
                cbParticipants.Items.Add(item)
            Next
            'Remove all items from second checkbox
            cbSelectedParticipants.Items.Clear()
        Else
            lblMsg.Text = "No participant(s) to move back"
        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.. ConversionConversion EmoticonEmoticon