共用方式為


How to: Populate List Web Server Controls from a Data Source

The information in this topic applies to the following Web server controls:

ListBox

DropDownList

CheckBoxList

RadioButtonList

BulletedList

You can use a list Web server control to display items that are read from a data source. Each item in the control corresponds to an item — such as a row in a data table — in the data source. The control can display one field from the source and can optionally use a second field as the item value.

You can combine list items that you create at design time with list items that are populated from a data source by setting the AppendDataBoundItems property of a list control to true. A typical example is to create a list item with the text "Select an item" as the first item for a DropDownList control. You can then populate the remaining items in the DropDownList control from the data source.

To populate a list Web server control from a data source

  1. Add a data source control to the page, such as the SqlDataSource control or the ObjectDataSource control.

  2. Use the Data Source Configuration Wizard to define the connection and the query, or data-retrieval method, for the data source control.

  3. Right-click the list control and click Show Smart Tag.

  4. In the task menu, click Choose Data Source.

    The Data Source Configuration Wizard is displayed.

  5. In the Select a data source list, click the data source control that you added in Step 1.

  6. In the Select a data field to display list, click the data field that you want to display in the control.

  7. Optionally, in the Select a data field for the value list, click the data field to store as the value of each item.

  8. Optionally, if the control already contains statically defined list items that you want to retain when the list is populated from a data source, set the control's AppendDataBoundItems property to true.

See Also

Tasks

Walkthrough: Creating Master/Detail Web Pages in Visual Studio

Concepts

BulletedList Web Server Control Overview

Reference

CheckBox and CheckBoxList Web Server Controls Overview

DropDownList Web Server Control Overview

ListBox Web Server Control Overview

RadioButton and RadioButtonList Web Server Controls Overview