asp.net.ph

Skip Navigation Links

Populating a List Control from a Database

Controls You Can Use on Web Forms   ASP.NET Standard Controls   ListBox Control


ASP.NET selectable list controls can display option items that are dynamically obtained from a data source at run time. In this case, the list items correspond to the values of a given field in the data source. The control can display one field from the source and can optionally use a second field as the item value.

NOTE: The information in this topic applies to all list Web server controls: ListBox, DropDownList, RadioButtonList, and CheckBoxList.

To populate a list control from a database

In the below examples, a DropDownList control is used to display a list of product categories from a specified database, enabling a user to easily view the products for a selected category.

This first example shows a DropDownList control that is bound to an AccessDataSource control.

  1. Declare an <asp:dropdownlist> element on the page. For syntax, see DropDownList Control Syntax.
  2. Specify the control’s DataSourceID property.
  3. Set the control’s DataTextField property to the name of the field in the data source whose values will be used as the Text of each list item.
    Select Category: <asp:dropdownlist id="filterType" runat="server"
          datasourceid="ProductType" datatextfield="Type"
          autopostback />
  4. Set the DataValueField to specify the name of the field to be used as the Value of each list item, if it would be different from the DataTextField property.
  5. Optionally set the control’s AutoPostBack property to true to cause an immediate posting to the server, when the user changes a selection from the list.
Using the AccessDataSource Control
Run Sample | View Source

This second example shows a DropDownList control that is bound using the DataSource property, which can be set programmatically just like any Web server control property.

In this example, the data to use as the DataSource for the DropDownList is dynamically generated via a call to an external method named fetchData. This procedure is set in the Page_Load event handler, which basically does the following:

  1. fetch the product types from the database into a table in a DataSet
  2. bind the returned data to the list control.
    protected void Page_Load ( object src, EventArgs e ) {
          if ( !IsPostBack ) {
             string query = "SELECT DISTINCT Type FROM Products";
             mySelect.DataSource = fetchData ( query, "dbtutor" );
             mySelect.DataBind ( );
          }
       }

Next, set the control’s DataTextField and DataValueField as appropriate, in the same manner as in the previous example. The values from these fields will then be used as the Text or Value, respectively, of each list item.

Select Gear: <asp:DropDownList id="mySelect"
   datatextfield="ProductType" runat="server" />
Populating a List Control from a Database
Run Sample | View Source

For related information, see Retrieving data from user specified queries.

See Also

Determining the Selection in a List Control   Responding to Changes in a List Control



© 2025 Reynald Nuñez and asp.net.ph. All rights reserved.

If you have any question, comment or suggestion
about this site, please send us a note