Enhanced List Box Control

本文介绍了Asp.Net中列表控件的基础知识及操作,包括控件使用、命名空间层次结构、重要属性和方法等。还介绍了ListItem类,以及多个函数实现列表项的插入、移动等操作,文章可扩展数据绑定功能。

 

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 are :

  1. CheckBoxList
  2. DropDownList
  3. ListBox
  4. 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 ordinal index of the selected items in the list.
SelectedValueIt represents the value of the selected 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:

  1. Add
  2. AddRange
  3. Remove
  4. RemoveAt
  5. Clear
  6. Insert

About ListItem Class

A ListItem control represents an individual data item within a data-bound list control

Eg., <asp:ListItem Value="1" Text="Item 1">Sample Item 1</asp:ListItem>

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 AddRemoveAll(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 AddRemoveItem(ListBox aSource, ListBox aTarget)
 {
  ListItemCollection licCollection;
  try
  {
     licCollection = new ListItemCollection();
  for(int intCount=0;intCount < aSource.Items.Count;intCount++)
     {
        if(aSource.Items[intCount].Selected==true)
     licCollection.Add(aSource.Items[intCount]); 
   }
   for(int intCount=0;intCount < licCollection.Count;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 count 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 < iCount) 
   {
    // Check if this item is selected.
    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 past other selected items
       if(-1 != iIndexSelectedMarker)
       {
         for(int iIndex2 = 0; iIndex2 < iCount; ++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 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 selected save the index so we can check
        // it later so we don't move past the any selected items.
        else if(-1 == iIndexSelectedMarker)
        {
         iIndexSelectedMarker = iIndex;  
       break;
     }
      iIndex = iIndex + 1;
    }
    if(iIndexSelectedMarker==0)
      lstBox.SelectedIndex=iIndexSelectedMarker;
    else
      lstBox.SelectedIndex=iIndexSelectedMarker-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 its current position and inserts it a position below.

private void MoveDown(ListBox lstBox)
 {
   try
    {
      int   iIndex, iCount, iOffset, InsertAt,iIndexSelectedMarker = -1;
    string  lItemData;
     string  lItemval;
          
    // Get the count 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 selected.
    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 past other selected items
           if(-1 != iIndexSelectedMarker)
           {
              for(int iIndex2 = 0; iIndex2 < iCount-1; ++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:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值