DropDownList control is used to give a single select option to the user 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.
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 dropdown 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:
Red
Blue
Green
2nd DropDown box
Item Text 0
Item Text 1
Item Text 2
Item Text 3
3rd DropDown box
Name 0
Name 1
Name 2
Name 3
Name 4
Name 5
Name 6
Name 7
Name 8
Name 9
This DropDown will fire OnSelectedIndexChanged event when you select other item.
4th DropDown box
Name 0
Name 1
Name 2
Name 3
Name 4
Name 5
Name 6
Name 7
Name 8
Name 9
Note: When dropdown box item changes, Values of dropdowns will appear here in the selected color of 1st dropdown box.
// 1st DropDown box
<asp:DropDownList ID="DropDownList1" 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:DropDownList>
// 2nd DropDown box
<asp:DropDownList ID="DropDownList1" runat="Server" />
// 3rd DropDown box
<asp:DropDownList ID="DropDownList1" runat="server" DataTextField="Name" DataValueField="ID" />
// 4th DropDown box
<asp:DropDownList ID="DropDownList1" runat="server" DataTextField="Name" DataValueField="ID" OnSelectedIndexChanged="GivePostBackResult" AutoPostBack="True" />