ASPAlliance.com: The #1 ASP.NET Community
The ASPSmith
Search
D: | Domains | Authors.aspalliance.com | Stevesmith | Articles | ASP.NET DropDownList Web Control, Part 1
ASP.NET DropDownList Web Control, Part 1

By Steven Smith

[Example]

Anybody with much experience in ASP has written an include file function for populating the contents of a SELECT dropdown list with entries from a database. Some of the more advanced users have even gone so far as to add caching to their function to reduce database hits for these rarely changing menu options. Naturally, these same techniques are going to be used with ASP.NET, but with a new programming object model. ASP.NET provides a web control, the DropDownList, which can be used for the same purposes as our custom built SELECT tag from Classic ASP.

Since this is as much a tutorial as it is a reference, let's go ahead and look at how we use a DropDownList on our ASP.NET page:

  <asp:DropDownList id="stateList1" runat="server" />

This simple declaration will create an empty DropDownList. I've named it 'stateList' because we're going to populate the DropDownList with U.S. state names, such as might be used as part of an address form. Remember that all web controls, including the DropDownList, must be within a FORM RUNAT="SERVER" tag in order to function. Also note that most of the features and techniques described in this article can be applied equally well to the ListBox and other List web controls. The next thing we need to know how to do with this control is to fill one with values. We'll start with the simplest method, which is to build ListItems into the DropDownList declaratively (i.e. using tags rather than writing compiled code). This next example shows how to add three items to our DropDownList, a blank item and two states (of mind).

  <asp:DropDownList id="stateList2" runat="server">
    <asp:ListItem Selected="True" Value="" Text=""/>
    <asp:ListItem Value="Confusion" Text="Confusion"/>
    <asp:ListItem Value="Denial" Text="Denial"/>
  </asp:DropDownList>

Strangly, runat="server" is not required for the ListItems -- they are all considered to be part of the DropDownList, and so it is somewhat redundant to specify it again for each ListItem. The three properties of the ListItem should be fairly self-explanatory. Selected="true" has the same effect as adding the SELECTED keyword to an OPTION tag in an HTML SELECT tag. The Value property maps to the OPTION VALUE property in the HTML world, and the Text property maps to the contents of the OPTION ... /OPTION tag pair (the visible part). When Value and Text should be the same, either one can be specified and both will share this value.

Ok, that's not terribly exciting yet, but it can certainly be useful for menus you know won't be changing too often. For example, it's probably not necessary to use a database or an array for a Gender DropDownList. The next thing we should cover at this point is, how do you get the value back from one of these controls.

Like all web controls, since the DropDownList just renders as a form element (a SELECT tag), its value can be retrieved from the Request object. For example, to retrieve the value of the stateList2 control and output it to the screen, we might use code like this ( for VB replace [] with () ):

  <%=Request["stateList2"]%>

This isn't the most elegant way to get at the values of something, and in fact one of the great improvements of ASP.NET over Classic ASP is in the object model. VB programmers will be familiar with referencing controls and their properties; this is precisely the recommended practice for .NET applications. In this case, there are two properties that we might be interested in referencing -- Text and Value. In Classic ASP, using the Request object, all that we had available was the Value property. So here we have just one small example of a benefit provided immediately by ASP.NET -- easy access to both the Text and Value portions of SELECT lists. These two statements will output the value of a DropDownList following a submit:

	<%=stateList2.SelectedItem.Text%>
	<%=stateList2.SelectedItem.Value%>
You can see everything we've covered so far on our example page.

In Part 2, we will cover the following topics:

  • Programmatically Adding Items to a databound list control
  • Databinding to an Array
  • Databinding to a database result set
  • Adding an empty default item to a databound list control
  • Setting the default item of a databound list control
Other Links




ASP.NET Developer's Cookbook, By Steven Smith, Rob Howard, ASPAlliance.com 

ASP.NET By Example, By Steven Smith 




Steven Smith, MCSE + Internet (4.0)
Last Modified: 6/12/2009 10:58:21 AM
History: 6/12/2009 10:58:21 AM