ASP.Net ListBox Control

Updated on 19 Jul 2012,
Published on 08 Mar 2010

The ASP.Net ListBox control provides the functionality to display a list of items in the form of menu that allows single or multiple items selection. You can select the multiple list items of ListBox control by changing their selected state. The selected items of ListBox return the Boolean type result as true or false that determine the selected state of an individual list item or multiple list items based on the C# code logic applied on it. If the list item is in selected state then it returns true and if it is in un-selected state then the associated list item returns false. You can use the ASP.Net ListBox web server control to get the user input as multiple items selected under a specific group. A simple example of ListBox control is displaying a list of items to display country, state or cities that can be used to select multiple locations to search any desired object based on filter criteria specified by using multiple selection of items of ListBox controls.

ListBox Control Examples:

You can see the live samples and examples of ListBox Control from the following links:

The ASP.Net ListBox control consists of following properties that are commonly used to get or set the values or user input. There are two sets of properties below; the first set includes the properties that enable you to get or set the values of list items of ListBox and the second set includes the properties that enable you to customize its appearance: 

AppendDataBoundItems: It allows you to add the list items of ListBox in a static way along with the data-bound list items retrieved from any data source. It is a Boolean type property that accepts value as true or false for allowing or disallowing the append feature of list items.

AutoPostBack: It is also a Boolean type property that enables the control to send the web page to PostBack when user clicks the list item of listbox to select or un-select it.

DataMember: It accepts the name of one of the data table stored in the data source, if there is a collection of data tables in it. For example, while using SQL queries or stored procedures you can retrieve data in the form of multiple data tables that can be stored into the DataSet class object. This DataSet object can be used to bind the data to different types of Data-bound controls on the ASP.Net web page. The DataMember property enables you to specify the name of the table that you want to bind with any particular data-bound control.

DataSource: This property allows you to specify the name of the data source dynamically.

DataSourceID: It accepts the ID of the DataSource control used to retrieve the data from the database.

DataTextField: It accepts the name of the Field/Column of the data table stored in the datasource object that you want to display as the text content of the list item.

DataTextFormatString: It allows you to format the text property of the list item. For example you can use {0:C} to format the numeric value as currency. There are different types of formats such as {0:X}, {0:E}, {0:N}, {0:C}. You can learn about these formats from this tutorial: ASP.Net C# String Format Function

DataValueField: It accepts the name of the field of the data table stored in the datasource object that you want to set as the value attribute of each list item. This value part does not appear as text content.

SelectionMode: It allows you to set the selection mode of ListBox control to enable it for single or multiple selections of items. It accepts the following two types of predefined values to set the selection mode:

1. Single

2. Multiple

Other than above properties of ASP.Net ListBox control that allow you to populate it with list items, it also contains few CSS based style properties that allow you to customize the appearance of control and its fonts. The ListBox control supports BackColor, CssClass, Font style properties, ForeColor, Height and Width property. The Font style properties include Bold, Italics, Name, Underline, Size, and StrikeOut like properties that enable you to change the appearance of text displayed as list items of ListBox.

Output:

Get the output for above discussed code from the following link:

ListBox Control Sample

Continue to the next tutorial: ASP.Net ListBox AutoPostBack Property to learn the use of AutoPostBack property that submits the web page when user selects an item of Listbox control.

0 Responses to "ASP.Net ListBox Control"
Leave a Comment
* required
* required
* will not be published
* optional
* hint: http://www.example.com
  • Subscribe via Email