| Property Name |
Description |
| AutoPostBack |
Boolean. Automatically posts the form
if True. Default is False. |
| DataMember |
Identifies the table in a data source
to bind to the control. |
| DataSource |
Identifies the data source for the
items in the list. |
| DataTextField |
Identifies the field in the data
source to use for the option text. |
| DataValueField |
Indicates the field in the data source
to use for the option values. |
| DataTextFormatString |
Format string for determining how
the DataTextField is displayed. |
| Items |
Collection of items in the control. |
| SelectedIndex |
Indicates the index number of the
selected option. |
| SelectedItem |
The selected item (duuh). |
| Method Name |
Description
|
| DataBind |
Binds the control to its data source.
This means the items from the data source are loaded into the controls ListItem
collection. |
| OnSelectedIndexChanged |
Raises the SelectedIndexChanged event. |
| Event Name |
Description |
| SelectedIndexChanged |
The event is raised whenever a new
option is selected. |
How To Get Options Into the
DropDownList Control
1. If you
are using Visual Studio, drag the DropDownList control from the toolbox onto the
web form. Make sure you choose the control from the WebForms tab and not the HTML
tab. After positioning the control where you want, resizing it and so on, you
can manually enter the items into the list using Visual Studio. Go to the properties
dialog for the control. If the properties dialog is not visible, right click on
the control and select PROPERTIES from the menu. Find the entry for "Items"
and click on the ellipsis button to bring up the ListItem Collection Editor. Click
the ADD button on the left side of the editor. On the right side, enter "Georgia"
in the text field and "GA" in the value field. Now click ADD again and
enter "Florida" in the text field and "FL" in the value field.
Click ADD again and enter "Alabama" in the text field and "AL"
in the value field. Click the OK button. Now you have a DropDownList control with
three list items. The text portion of each item will display the state name. The
corresponding value will be the abbreviation of the selected state.
Feature Of The Week |
 |
 |
 |
 |
 |
| |
 |
Featured Resources & Downloads |
 |
1. AccessLine
- Solutions for mobile professionals - Free Trial

2. MicroStrategy
- Free Next-Generation Business Intelligence Book

3. Sonic - Free
webcasts focused on addressing the issues of cost-effective enterprise integration.

4. ConferenceCall
- Lowest prices on conference calls - try the free comparison calculator.

More Freeware &
Shareware

|
 |
|
 |
 |
|
2. You can
access the text and value properties of the selected item in code as follows:
Dim strVariable as String
strVariable = DropDownList1.SelectedItem.Text - To access the text portion
strVariable = DropDownList1.SelectedItem.Value - To access the value portion
3. If you are
not using Visual Studio or if you just allergic to drag 'n drop, you can code
the above directly following the following syntax:
<asp:DropDownList id="DropDownList1"
runat="server">
<asp:ListItem Value="GA">Georgia</asp:ListItem>
<asp:ListItem Value="FL">Florida</asp:ListItem>
<asp:ListItem Value="AL">Alabama</asp:ListItem>
</asp:DropDownList>
4. You can write
code to add your items also. Here is an example of how you might populate the
control when the page loads for the first time. Notice, however, that we are only
filling in the text portion of each option, not the value portion.
If Not
Page.IsPostBack Then
DropDownList1.Items.Add("Georgia")
DropDownList1.Items.Add("Florida")
DropDownList1.Items.Add("Alabama")
End If
5. You can bind
the DropDownList control to an ArrayList. Again, notice that we are not getting
the value portion of each option.
Dim colArrayList
as New System.Collections.ArrayList()
If Not
Page.IsPostBack Then
colArrayList.Add("Georgia")
colArrayList.Add("Florida")
colArrayList.Add("Alabama")
DropDownList1.DataSource
= colArrayList
DropDownList1.DataBind()
End If
6. The following
example shows how to populate the control using a Hashtable. Now we can get both
the text and value portions populated.
Dim myHashTable as new System.Collections.Hashtable()
myHashTable("GA")
= "Georgia"
myHashTable("FL") = "Florida"
myHashTable("AL") = "Alabama"
For each Item in myHashTable
Dim newListItem as new ListItem()
newListItem.Text = Item.Value
newListItem.Value = Item.Key
DropDownList1.Items.Add(newListItem)
Next
7. A SortedList
is a collection that stores key/value pairs (like a hashtable) but where the items
are automatically sorted according to key/value. Here is an example:
Dim mySortedList
as new System.Collections.SortedList
Dim Item as DictionaryEntry
mySortedList("GA")
= "Georgia"
mySortedList("FL") = "Florida"
mySortedList("AL") = "Alabama"
For each
Item in mySortedList
Dim newListItem as new ListItem()
newListItem.Text = Item.Value
newListItem.Value = Item.Key
DropDownList1.Items.Add(newListItem)
Next
8. Here is an
example of how you might populate the control from a SQL Server database using
a data reader object:
' Let's
assume the connection string is stored in a session variable.
Dim strConnect as Strng
strConnect = Session("ConnectionString")
' Open
the Connection
Dim Con as new System.Data.SQLClient.SQLConnection(strConnect)
Con.Open()
' SQL
Statement
Dim strSQL as String
strSQL = "SELECT State_Name, State_Code FROM TableState ORDER BY State_Name"
' Command,
Data Reader
Dim Com as new System.Data.SQLClient.SQLCommand(strSQL, Con)
Dim rdr as System.Data.SQLClient.SQLDataReader = Com.ExecuteReader()
' Populate
the Control
While rdr.Read()
Dim newListItem as new ListItem()
newListItem.Text = rdr.GetString(0)
newListItem.Value = rdr.GetString(1)
DropDownList1.Items.Add(newListItem)
End While
Conclusion
I certainly didn't exhaust all the possibilities but I think this provides
a pretty decent overview of the control, how to use it and how to get data into
it. If you have any comments, corrections or other ideas, you can email me at:
RogerMcCook@usermail.com
Roger D. McCook
McCook Software, Inc.
Originally appeared at http://www.mccooksoftware.com/Articles/articleaspdropdownhtm.htm