Monday, April 26, 2010

Asp.net List box to ListBox Transfer....

   
The ListBox control enables you 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 show you how to choose multi items and move them to the other Listbox.


The ListBox control enables you 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. The ListBox also provides the MultiColumn property to enable the display of items in columns instead of a straight vertical list of items. This allows the control to display more visible items and prevents the need for the user to scroll to an item. This tutorial show you how to move items of ListBox from one to the other.





First, you will need to import the using System.Collections namespace. The System.Collections namespace contains interfaces and classes that define various collections of objects, such as lists, queues, bit arrays, hash tables and dictionaries.



public void AddBtn_Click(Object Src, EventArgs E)
{

if (lstAsset.SelectedIndex>=0)
{
for (int i = 0; i < lstAsset.Items.Count; i++)
{
if (lstAsset.Items[i].Selected)
{
if (!lasset.Contains(lstAsset.Items[i]))
{
lasset.Add(lstAsset.Items[i]);
}
}
}
for (int i = 0; i < lasset.Count; i++)
{
if (!lstSubordinateAsset.Items.Contains(((ListItem)lasset[i])))
{
lstSubordinateAsset.Items.Add(((ListItem)lasset[i]));
} lstAsset.Items.Remove(((ListItem)lasset[i]));
}
}
}

public void AddAllBtn_Click(Object Src, EventArgs E)
{

while (lstAsset.Items.Count != 0)
{
for (int i = 0; i < lstAsset.Items.Count; i++)
{
if (!lasset.Contains(lstAsset.Items[i]))
{
lasset.Add(lstAsset.Items[i]);
}
}
for (int i = 0; i < lasset.Count; i++)
{
if (!lstSubordinateAsset.Items.Contains(((ListItem)lasset[i])))
{
lstSubordinateAsset.Items.Add(((ListItem)lasset[i]));
}
lstAsset.Items.Remove(((ListItem)lasset[i]));
}
}
}

public void RemoveBtn_Click(Object Src, EventArgs E)
{

if (lstSubordinateAsset.SelectedItem != null)
{
for (int i = 0; i < lstSubordinateAsset.Items.Count; i++)
{
if (lstSubordinateAsset.Items[i].Selected)
{
if (!lsubordinate.Contains(lstSubordinateAsset.Items[i]))
{
lsubordinate.Add(lstSubordinateAsset.Items[i]);
}
}
}
for (int i = 0; i < lsubordinate.Count; i++)
{
if (!lstAsset.Items.Contains(((ListItem)lsubordinate[i])))
{
lstAsset.Items.Add(((ListItem)lsubordinate[i]));
}
lstSubordinateAsset.Items.Remove(((ListItem)lsubordinate[i]));
UpdateList.Add(lsubordinate[i]);
}
}
}

public void RemoveAllBtn_Click(Object Src, EventArgs E)
{

while (lstSubordinateAsset.Items.Count != 0)
{
for (int i = 0; i < lstSubordinateAsset.Items.Count; i++)
{
if (!lsubordinate.Contains(lstSubordinateAsset.Items[i]))
{
lsubordinate.Add(lstSubordinateAsset.Items[i]);
}
}
for (int i = 0; i < lsubordinate.Count; i++)
{
if (!lstAsset.Items.Contains(((ListItem)lsubordinate[i])))
{
lstAsset.Items.Add(((ListItem)lsubordinate[i]));
}
lstSubordinateAsset.Items.Remove(((ListItem)lsubordinate[i]));
UpdateList.Add(lsubordinate[i]);
}
}


 also see this in code project............... thats  will also help all  
http://www.codeproject.com/KB/aspnet/List_Box_Control.aspx

2 comments: