http://www.codeproject.com/aspnet/mcenhancedlistboxControl.asp
Introduction
An Article About The Basics of Listbox Control and The Operations That Can Be Done Using Simplified Code.
Background
MSDN Library Web Server Controls
USING THE CODE
About Listbox Control
The Controls That Are used to list the data in asp.net is:
CheckBoxList Dropdownlist Listbox RadiobuttonList
We are about to concentrate on listbox control.
The Namespace Hierarchy
System.Object
System.Web.ui.Control
System.Web.ui.WebControls.WebControl
System.Web.ui.WebControls.listControl
Important Properties and Methods
About Listbox Class
Properties:
DataSourceA data source that provides data for populating the list control.DataMemberA string that specifies a particular table in the DataSource.DataTextFieldA data that specifies the field of the data source that provides the text content of the list items.DataValueFieldA data that specifies the field of the data source that provides the value of each list item.SelectionModeThe ListSelectionMode enumeration represents the selection mode of the ListBox control that determines whether a user can select multiple items or just a single item.SelectedItemIt represents the selected item with the lowest index in the list Control.selectedIndexit represents the lowest order index of the selected items in the list.selectedValueit represents The value of the successd item in the list control.
About ListItemcollection Class
The ListItemcollection Class Represents a Collection of ListItem Objects. The ListItem Objects Represent The items Displayed in List controls.
Properties:
ListItemcollection.countit Gets the Number of ListItem Objects in The Collection.listItemCollection.Itemit Gets a ListItem AT The Specified Index in The Collection.Methods:
Add AddRange Remove Removeat Clear INSERT
About ListItem Class
A ListItem Control Repens An Individual Data Item Withnin A Data-Bound List Control
Eg.,
Properties:
ListItem.SelectedIt Gets or sets a value indicating whether the item is selected.ListItem.TextThe text displayed in a list control for the item represented by the Listitem.ListItem.ValueThe value associated in a list control for the item represented by the ListItem.
Functions
The AddRemoveAll function inserts all items from the Source Listbox to the Target Listbox and removes all items in the Source Listbox. It is a generalised function and you can specify the source and target Listbox according to your need.
Private Void AddRemovell (ListBox Asource, Listbox Atarget)
{
Try
{
Foreach (ListItem Item IN Asource.Items)
Atarget.Items.Add (item);
Asource.Items.clear ();
}
Catch (Exception ExpException)
{
Response.write (Expexception.Message);
}
}
The AddRemoveItem function inserts the selected item from the Source Listbox to the Target Listbox and removes the item in the Source Listbox. It is also a generalised function and you can specify the source and target Listbox according to your need.
Private Void AddRemovetem (ListBox Asource, Listbox Atarget)
{
ListItemcollection Liccollection;
Try
{
Liccollection = new ListItemCollection ();
For (int idcount = 0; intcount { IF (asource.Items [intcount] .selected == true) liccollection.add (asource.items [intcount]); } For (int INTCOUNT = 0; intcount { Asource.Items.Remove (liccollection [intcount]); Atarget.Items.Add (liccollection [intcount]); } } Catch (Exception ExpException) { Response.write (Expexception.Message); } Finally { Liccollection = NULL; } } The MoveUp function moves the selected item to a position above. When you want to store the items from the ListBox by rearranging the selected items you can just loop the items and save it. The data of the selected item. Then it removes the selected item From its current position and inserts it a position above. The loop goes Only Till The SELECTED ITEM SO The Other Items Will Not Be looped. Hence Improving the Performance. Private Void Private Void MoveUp (Listbox Lstbox) { Int IIndex, Icount, Ioffset, Iinsertat, IINDEXSELECTEDMARKER = -1; String Litemdata, Litemval; Try { // Get the country of items in the list control iCount = Lstbox.Items.count; // set the base loop index and the increment / decrement value based // on the direction the item Are Being Moved (Up or Down). IINDEX = 0; IOFFSET = -1; // loop through all of the items in the list. While (IIndex { // Check if this item is successd. IF (Lstbox.selectedIndex> 0) { // Get The Item Data for this ITEM LITEMVAL = LSTBOX.SELECTEDITEM.VALUE.TOSTRING (); Litemdata = LSTBOX.SelectedItem.text.toString (); IINDEXSELECTEDMARKER = LSTBOX.SELECTEDINDEX; // don't move selected items paste Other successted items IF (-1! = neyDexselectedMarker) { For (int IINDEX2 = 0; IIndex2 { // Find the index of this item in enabled listif (LITEMVAL == Lstbox.Items [IIndex2] .value.toString ()) { // Remove the item from its current position Lstbox.Items.Removeat (IIndex2); // reinsert the item in the array one space higher // Than ITS Previous Position IINSERTAT = (IINDEX2 IOFFSET) <0? 0: IINDEX2 IOFFSET; ListItem Li = New ListItem (Litemdata, Litemval); Lstbox.Items.Insert (IINSERTAT, Li); Break; } } } } // if this item wasn't successd save the index so we can check // it Later So We don't move paste the any successd item. Else IF (-1 == neyDexselectedMarker) { INDEXSELECTEDMARKER = IIndex; Break; } IINDEX = IINDEX 1; } IF (IIndexSelectedMarker == 0) Lstbox.selectedIndex = IIndexSelectedMarker; Else Lstbox.selectedIndex = INDEXSELECTEDMARKER-1; } Catch (Exception ExpException) { Response.write (Expexception.Message); } } The MoveDown Function Moves The SELECTED ITEM TO A POSITION BELOW. The Data of The SELECTED ITEM. THEN IT Removes The Selected Item from It a position below. Private void MoveDown (Listbox Lstbox) { Try { Int IIndex, Icount, Ioffset, Insertt, IIndexSelectedMarker = -1; String Litemdata; String Litemval; // Get the country of items in the list control iCount = Lstbox.Items.count; // set the base loop index and the increment / decrement value based ON // The Direction The Item Are Being Moved (Up Or Down). IINDEX = ICOUNT - 1; IOFFSET = 1; // loop through all of the items in the list. While (IIndex> = 0) { // Check if this item is successd. IF (Lstbox.selectedIndex> = 0) { // Get The Item Data for this ITEM Litemdata = Lstbox.SelectedItem.Text.toString (); LITEMVAL = Lstbox.selectedItem.Value.toString (); IINDEXSELECTEDMARKER = LSTBOX.SELECTEDINDEX; // don't move selected items paste Other successted items IF (-1! = neyDexselectedMarker) { For (IND IIndex2 = 0; IIndex2 { // Find the index of this item in enabled list IF (Litemval == Lstbox.Items [IIndex2] .value.toString ()) { // Remove the item from its current position Lstbox.Items.Removeat (IIndex2); // Reinsert the item in the array one space Lower // Than ITS Previous Position IINSERTAT = (IINDEX2 IOFFSET) <0? 0: IINDEX2 IOFFSET; ListItem Li = New ListItem (Litemdata, Litemval); Lstbox.Items.Insert (IINSERTAT, Li); Break; } } } } IINDEX = IIndex - 1; } IF (IIndexSelectedMarker == Lstbox.Items.count-1) Lstbox.selectedIndex = IIndexSelectedMarker; Else Lstbox.selectedIndex = IIndexSelectedMarker 1; } Catch (Exception ExpException) { Response.write (Expexception.Message); } } Points of interest The Article can be extended with databinding features. The Databinding can be done from the Database with the Datasource, DataTextField and Datavaluefield properties set. For simplification for beginners I have added the items in design time. History Version 1.0, September 2004. About Mankayarkarasi Working As a Software Programmer For Past 2 Years In .NET Technology. Has Experience In Visual C # .NET, VB.NET, ASP.NET, ADO.NET, XML Web Services, ASP, JavaScript, MS-Access And SQL Server 2000. Click Here to View Mankayarkarasi's Online Profile. Other Popular ASP.NET ARTICLES: