Search
Author
ASP.NET Tutorials
Author
Sheo Narayan
Advertisements


Winners

Win Prizes

Social Presence
Like us on Facebook

Silverlight Tutorials | Report a Bug in the Tutorial
asp:ListBox control
ListBox control is used to give a single or multiple select options to the user from multiple listed items.
 
All properties and its working resembles DropDownList box. However, ListBox has two extra properties called Rows and SelectionMode. ListBox control is used to give a single or multiple select option to the user (based on the property set) from multiple listed items. You can specify its height and width in pixel by setting its height and width but you will not be able give mutliple select option to the user. When it is rendered on the page, it is implemented through <select/> HTML tag. It is also called as Combo box.

Its properties like BackColor, ForeColor etc. are implemented through style properites of <span>. It has less property to decorate in comparison with other controls. There is no property like BorderStyle, BorderWidth. in DropDownList control.

You can add its option items by directly writing into .aspx page directly or dynamically add at run time or bind through database.

Following are some important properties that are very useful.
Rows No. of rows (items) can be set to display in the List.
SelectionMode Single or Multiple. If multiple, it allows user to select multiple items from the list by holding Ctrl or Shift key.
SelectedValue Get the value of the Selected item from the dropdown box.
SelectedIndex Gets or Sets the index of the selected item in the dropdown box.
SelectedItem Gets the selected item from the list.
Items Gets the collection of items from the dropdown box.
DataTextField Name of the data source field to supply the text of the items. (No need to set when you are adding items directly into .aspx page.)
DataValueField Name of the data source field to supply the value of the items. (No need to set when you are adding items directly into .aspx page.)
DataSourceID ID of the datasource component to provide data. (Only used when you have any DataSource component on the page, like SqlDataSource, AccessDataSource etc.)
DataSource The datasource that populates the items in the listbox box. (Generally used when you are dynamically generating the items from Database.)
AutoPostBack true or false. If true, the form is automatically posted back to the server when user changes the dropdown list selection. It will also fire OnSelectedIndexChanged method.
AppendDataBoundItems true or false. If true, the statically added item (added from .aspx page) is maintained when adding items dynamically (from code behind file) or items are cleared.
OnSelectedIndexChanged Method name that fires when user changes the selection of the dropdown box. (Fires only when AutoPostBack=true.)
DEMO : DropDownList Show Source Code
Staically added items (from .aspx page) Added manually through code Added through databound (dynamically)
1st DropDown box: 2nd DropDown box 3rd DropDown box (Multiple selction is possible, hold Ctrl or Shift key and click items.)
This DropDown will fire OnSelectedIndexChanged event when you select other item.
4th DropDown box
Note: When dropdown box item changes, Values of dropdowns will appear here in the selected color of 1st dropdown box.
                        // 1st DropDown box
                        <asp:ListBox ID="ListBox1" runat="server">
                            <asp:ListItem Text="Red" Value="red"></asp:ListItem>
                            <asp:ListItem Text="Blue" Value="blue"></asp:ListItem>
                            <asp:ListItem Text="Green" Value="green"></asp:ListItem>
                        </asp:ListBox>
                        
                        // 2nd DropDown box 
                        <asp:ListBox ID="ListBox1" runat="Server" />
                        
                        // 3rd DropDown box 
                        <asp:ListBox ID="ListBox2" runat="server" DataTextField="Name" DataValueField="ID" Rows="8" SelectionMode="multiple" />
                        
                        // 4th DropDown box 
                        <asp:ListBox ID="ListBox3" runat="server" DataTextField="Name" DataValueField="ID" OnSelectedIndexChanged="GivePostBackResult" AutoPostBack="True" />