Asp.net : Move selected item or all items from one checkboxlist to another

In this article I am going to explain how to move selected item or all items from one checkboxlist to another checkboxlist in asp.net using C#, VB.net


Implementation:

Add a webform to project and add all required control from toolbox to webform.

HTML Markup:
<table>
        <tr>
            <td>
                <asp:CheckBoxList ID="chkboxlistcountry" runat="server">
                    <asp:ListItem>India</asp:ListItem>
                    <asp:ListItem>Cuba</asp:ListItem>
                    <asp:ListItem>USA</asp:ListItem>
                    <asp:ListItem>China</asp:ListItem>
                </asp:CheckBoxList></td>
            <td>
                <asp:Button ID="btnmoveright" runat="server" Text=">" Width="50px"/>
                <br />
                <asp:Button ID="btnmoverightall" runat="server" Text=">>" Width="50px"/>
                <br />
                <asp:Button ID="btnmoveleft" runat="server" Text="<" Width="50px"/>
                <br />
                <asp:Button ID="btnmoveleftall" runat="server" Text="<<" Width="50px"/></td>
            <td>
                <asp:CheckBoxList ID="chkboxlistcity" runat="server">
                    <asp:ListItem>Delhi</asp:ListItem>
                    <asp:ListItem>Newyork</asp:ListItem>
                    <asp:ListItem>Mumbai</asp:ListItem>
                    <asp:ListItem>Bejing</asp:ListItem>
                </asp:CheckBoxList></td>

        </tr>
    </table>
C# code
int i = 0;
protected void btnmoveright_Click(object sender, EventArgs e)
    {
        try
        {
            chkboxlistcity.Items.Add(chkboxlistcountry.SelectedItem);
            i = chkboxlistcountry.SelectedIndex;
            chkboxlistcountry.Items.RemoveAt(i);
            chkboxlistcity.ClearSelection();
        }
        catch (Exception ex)
        {
        }
    }

    protected void btnmoveleft_Click(object sender, EventArgs e)
    {
        try
        {
            chkboxlistcountry.Items.Add(chkboxlistcity.SelectedItem);
            i = chkboxlistcity.SelectedIndex;
            chkboxlistcity.Items.RemoveAt(i);
            chkboxlistcountry.ClearSelection();
        }
        catch (Exception ex)
        {
        }
    }

    protected void btnmoverightall_Click(object sender, EventArgs e)
    {
        try
        {
            for (i = 0; i <= chkboxlistcountry.Items.Count - 1; i++)
            {
                chkboxlistcity.Items.Add(chkboxlistcountry.Items[i]);
            }
            chkboxlistcountry.Items.Clear();
        }
        catch (Exception ex)
        {
        }      
    }

    protected void btnmoveleftall_Click(object sender, EventArgs e)
    {
        try
        {
            for (i = 0; i <= chkboxlistcity.Items.Count - 1; i++)
            {
                chkboxlistcountry.Items.Add(chkboxlistcity.Items[i]);
            }
            chkboxlistcity.Items.Clear();
        }
        catch (Exception ex)
        {
        }      
    }
VB.net Code:
Dim i As Integer = 0

    Protected Sub btnmoveright_Click(sender As Object, e As EventArgs) Handles btnmoveright.Click
        Try
            chkboxlistcity.Items.Add(chkboxlistcountry.SelectedItem)
            i = chkboxlistcountry.SelectedIndex
            chkboxlistcountry.Items.RemoveAt(i)
            chkboxlistcity.ClearSelection()
        Catch ex As Exception
        End Try
    End Sub

    Protected Sub btnmoveleft_Click(sender As Object, e As EventArgs) Handles btnmoveleft.Click
        Try
            chkboxlistcountry.Items.Add(chkboxlistcity.SelectedItem)
            i = chkboxlistcity.SelectedIndex
            chkboxlistcity.Items.RemoveAt(i)
            chkboxlistcountry.ClearSelection()
        Catch ex As Exception
        End Try
    End Sub

    Protected Sub btnmoverightall_Click(sender As Object, e As EventArgs) Handles btnmoverightall.Click
        Try
            For i = 0 To chkboxlistcountry.Items.Count - 1
                chkboxlistcity.Items.Add(chkboxlistcountry.Items(i))
            Next
            chkboxlistcountry.Items.Clear()
        Catch ex As Exception
        End Try
    End Sub

    Protected Sub btnmoveleftall_Click(sender As Object, e As EventArgs) Handles btnmoveleftall.Click
        Try
            For i = 0 To chkboxlistcity.Items.Count - 1
                chkboxlistcountry.Items.Add(chkboxlistcity.Items(i))
            Next
            chkboxlistcity.Items.Clear()
        Catch ex As Exception
        End Try

    End Sub

Post a Comment

0 Comments