Here I will explain how to move items from one listbox to another listbox in asp.net using C#
Description
In one application I got requirement like work with listbox control in asp.net. First we will learn what listbox control purpose is and how we can use in our application.
The ListBox control is used to display a list of items to the user that the user can select by clicking. A ListBox control can provide single or multiple selections using the SelectionMode property. This tutorial shows you how to move items from one listbox to other listbox and we can move items by using single selection of item or multiple selection of items.
Design your aspx page like this
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Listbox Sample</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table align="center">
<tr>
<td align="center" colspan="3">
<b>ListBox Sample</b>
</td>
</tr>
<tr>
<td>
<asp:ListBox ID="ListBox1" runat="server" Height="169px" Width="121px" SelectionMode="Multiple">
<asp:listitem Value="0">Asp.Net</asp:listitem>
<asp:listitem Value="1">C#.Net</asp:listitem>
<asp:listitem Value="2">VB.Net</asp:listitem>
<asp:listitem Value="3">JavaScript</asp:listitem>
<asp:listitem Value="4">Ajax</asp:listitem>
</asp:ListBox>
</td>
<td>
<table>
<tr>
<td>
<asp:Button ID="btn1" runat="server" Text=">" Width="45px" onclick="btn1_Click" />
</td>
</tr>
<tr>
<td>
<asp:Button ID="btn2" runat="server" Text=">>" Width="45px" onclick="btn2_Click" />
</td>
</tr>
<tr>
<td>
<asp:Button ID="btn3" runat="server" Text="<" Width="45px" onclick="btn3_Click" />
</td>
</tr>
<tr>
<td>
<asp:Button ID="btn4" runat="server" Text="<<" Width="45px" onclick="btn4_Click" />
</td>
</tr>
</table>
</td>
<td>
<asp:ListBox ID="ListBox2" runat="server" Height="169px" Width="121px" SelectionMode="Multiple">
<asp:listitem Value="0">SQl Server</asp:listitem>
<asp:listitem Value="1">Sharesoint</asp:listitem>
<asp:listitem Value="2">Jquery</asp:listitem>
</asp:ListBox>
</td>
</tr>
<tr>
<td colspan="3">
<asp:Label ID="lbltxt" runat="server" ForeColor="Red"></asp:Label>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
|
In our asp.net application we are using arraylists to get arraylist in code behind we need to system.Colelction namespace in our application
using System.Collections;
|
System.Collections namespace contains interfaces and classes that define various collections of objects, such as lists, queues, bit arrays, hash tables and dictionaries.
Codebehind write the following code
ArrayList arraylist1 = new ArrayList();
ArrayList arraylist2 = new ArrayList();
protected void Page_Load(object sender, EventArgs e)
{
}
/// <summary>
/// btn1_Click event is used to move single or multiple items from Listbox1 to Listbox2
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btn1_Click(object sender, EventArgs e)
{
lbltxt.Visible = false;
if (ListBox1.SelectedIndex >= 0)
{
for (int i = 0; i < ListBox1.Items.Count; i++)
{
if (ListBox1.Items[i].Selected)
{
if (!arraylist1.Contains(ListBox1.Items[i]))
{
arraylist1.Add(ListBox1.Items[i]);
}
}
}
for (int i = 0; i < arraylist1.Count; i++)
{
if (!ListBox2.Items.Contains(((ListItem)arraylist1[i])))
{
ListBox2.Items.Add(((ListItem)arraylist1[i]));
}
ListBox1.Items.Remove(((ListItem)arraylist1[i]));
}
ListBox2.SelectedIndex = -1;
}
else
{
lbltxt.Visible = true;
lbltxt.Text = "Please select atleast one in Listbox1 to move";
}
}
/// <summary>
/// btn2_Click event is used to move all items from Listbox1 to Listbox2
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btn2_Click(object sender, EventArgs e)
{
lbltxt.Visible = false;
while(ListBox1.Items.Count!=0)
{
for(int i=0;i<ListBox1.Items.Count;i++)
{
ListBox2.Items.Add(ListBox1.Items[i]);
ListBox1.Items.Remove(ListBox1.Items[i]);
}
}
}
/// <summary>
/// btn3_Click event is used to move single or multiple items from Listbox2 to Listbox1
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btn3_Click(object sender, EventArgs e)
{
lbltxt.Visible = false;
if (ListBox2.SelectedIndex >= 0)
{
for (int i = 0; i < ListBox2.Items.Count; i++)
{
if (ListBox2.Items[i].Selected)
{
if (!arraylist2.Contains(ListBox2.Items[i]))
{
arraylist2.Add(ListBox2.Items[i]);
}
}
}
for (int i = 0; i < arraylist2.Count; i++)
{
if (!ListBox1.Items.Contains(((ListItem)arraylist2[i])))
{
ListBox1.Items.Add(((ListItem)arraylist2[i]));
}
ListBox2.Items.Remove(((ListItem)arraylist2[i]));
}
ListBox1.SelectedIndex = -1;
}
else
{
lbltxt.Visible = true;
lbltxt.Text = "Please select atleast one in Listbox2 to move";
}
}
/// <summary>
/// btn4_Click event is used to move all items form Listbox2 to Listbox1
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btn4_Click(object sender, EventArgs e)
{
lbltxt.Visible = false;
while (ListBox2.Items.Count != 0)
{
for (int i = 0; i < ListBox2.Items.Count; i++)
{
ListBox1.Items.Add(ListBox2.Items[i]);
ListBox2.Items.Remove(ListBox2.Items[i]);
}
}
}
}
|
Demo
|
If you enjoyed this post, please support the blog below. It's FREE! Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email. |
|||
|
|||
18 comments :
Please Elobrate All Things in Easy Way
How to store the Listbox2 items in the SQL table ?
Thanks suresh its helped me
Thanks dude for ur documentation
i got nice help from this project
nice tutorial.. but postback on each button click bothers.. how to prevent pagerefresh/postback.
thanks in advance.
why ListBox1.SelectedIndex = -1; is used
http://www.codeproject.com/Tips/570319/ASP-NET-Move-Items-from-One-Listbox-to-Another-Lis
same code same example
@vinod...
Someone copy and pasted my code in codeproject check the date of articles written.
In this tutorial, If you move all listbox1 items to listbox2 and then select all items from listbox2 except one, and then move, it won't move all selected items.
Correct Example as follows:
Let's take two checkboxes , two listboxes and two buttons to move from left to right and vice-versa. I Filled listbox1 with customerName and kept 2nd listbox empty
protected void ckbSelectAllCust_CheckedChanged(object sender, EventArgs e)
{
if (ckbSelectAllCust.Checked == true)
{
foreach (ListItem li in lbxCustomer.Items)
{
li.Selected = true;
}
}
else
{
foreach (ListItem li in lbxCustomer.Items)
{
li.Selected = false;
}
}
}
protected void ckbAllSelectedCust_CheckedChanged(object sender, EventArgs e)
{
if (ckbAllSelectedCust.Checked == true)
{
foreach (ListItem li in lbxSelectedCust.Items)
{
li.Selected = true;
}
}
else
{
foreach (ListItem li in lbxSelectedCust.Items)
{
li.Selected = false;
}
}
}
protected void btnMoveRight_Click(object sender, EventArgs e)
{
List remove = new List();
if (ckbSelectAllCust.Checked == true)
{
foreach (ListItem item in lbxCustomer.Items)
{
lbxSelectedCust.Items.Add(item);
remove.Add(item);
}
foreach (ListItem item in remove)
{
lbxCustomer.Items.Remove(item);
}
}
else
{
foreach (ListItem item in lbxCustomer.Items)
{
if (item.Selected == false) continue;
else
{
lbxSelectedCust.Items.Add(item);
remove.Add(item);
}
}
foreach (ListItem item in remove)
{
lbxCustomer.Items.Remove(item);
}
}
}
protected void btnMoveLeft_Click(object sender, EventArgs e)
{
List remove = new List();
if (ckbAllSelectedCust.Checked == true)
{
foreach (ListItem item in lbxSelectedCust.Items)
{
lbxCustomer.Items.Add(item);
remove.Add(item);
}
foreach (ListItem item in remove)
{
lbxSelectedCust.Items.Remove(item);
}
}
else
{
foreach (ListItem item in lbxSelectedCust.Items)
{
if (item.Selected == false) continue;
else
{
lbxCustomer.Items.Add(item);
remove.Add(item);
}
}
foreach (ListItem item in remove)
{
lbxSelectedCust.Items.Remove(item);
}
}
}
thanks
Thanks Suresh.Great job. the way ,you explain the articles,i like so much.
Hi same functionality i am looking for. with some addition feature such as grouping of items. For example in your code
list items like
Server side
Asp.Net
C#.Net
VB.Net
Server side
JavaScript
Ajax
How can i set this in your code. from above value from database. make it transfer to the next listview need to remail same grouping during transfer,
also one more thing is when i want to shift any item from 2 listbox to first listbox it has goes on the postion where its present during first data bind.
Thanks And Regards,
Omkar.
Your code not work in vb.net.
when i click on
btn2(>>)
btn4(<<)
get error like...
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
Very Nice and useful article Suresh sir u always help me you blog is tooooo much useful .Bundle of thanks
Thank u so much.....
Its Working....
Note: Only a member of this blog may post a comment.