Geeks With Blogs

News

Microsoft MVP


DZone MVB


Moderator at CodeASP.NET


Quiz Master







free counters
Free counters
Added on January 19,2012


Follow Me @vmsdurano

A bit About Me



Disclaimer
The opinions expressed herein are my own personal opinions and does not represent the opinions of my employers. Nor does it represent the opinion of my dog, because I don’t have one.


Vinz' Blog (ProudMonkey) "Code, Beer and Music ~ my way of being a programmer"

Recently I encounter question at the forums.asp.net asking if how to move selected ListItems from one  CheckBoxList to another. So I decided to post the solution that I have provided in that thread so that others can refer this post if they will encounter the same thing.

Here’s the code block below:

ASPX:

<table>
    <tr>
        <td> 
            <asp:CheckBoxList ID="CheckBoxList1" runat="server">
                <asp:ListItem>Player1</asp:ListItem>
                <asp:ListItem>Player2</asp:ListItem>
                <asp:ListItem>Player3</asp:ListItem>
                <asp:ListItem>Player4</asp:ListItem>
                <asp:ListItem>Player5</asp:ListItem>
                <asp:ListItem>Player6</asp:ListItem>
            </asp:CheckBoxList>
        </td>
        <td>
            <asp:Button ID="ButtonAdd" runat="server" Text=">>" 
                        onclick="ButtonAdd_Click" />
            <br />
            <asp:Button ID="ButtonRemove" runat="server" 
                        Text="<<" onclick="ButtonRemove_Click" />
        </td>
        <td>
            <asp:CheckBoxList ID="CheckBoxList2" runat="server">
            </asp:CheckBoxList>
        </td>
    </tr>
</table>

 

RELEVANT CODES:

protected void ButtonAdd_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 ButtonRemove_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]);
            }
        }
}

That's it! I hope someone find this post useful!


Technorati Tags: ,
Posted on Monday, May 25, 2009 2:42 PM ASP.NET , C# | Back to top


Comments on this post: Move ListItems Between Two CheckBoxLists

comments powered by Disqus

Copyright © Vincent Maverick Durano | Powered by: GeeksWithBlogs.net