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.
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";
}
}
0 comments:
Post a Comment
Note: only a member of this blog may post a comment.