asp.net.ph

Skip Navigation Links

Data Binding Multiple Record Controls


Multi-record controls — such as the Repeater, DataList, DataGrid, ListBox, CheckBoxList, and RadioButtonList controls — are bound to a data source that contains multiple data items. For example, you can bind a DataGrid control to a table in a dataset and the control can directly display the data. Or you can bind a DataList or Repeater control to a table in a dataset and the control can make it available to child controls.

You can also bind an HtmlSelect control to multiple records. The procedure is different than it is for the Web server controls listed above. For details, see Data Binding HTML Server Controls.

NOTE: This procedure discusses setting properties that bind the control to an object containing multiple data items. You can also use data binding to set the value of single-value properties in these controls. For example, you can set the control’s Width or BackColor property using single-value data binding. For details, see Data Binding Single-Value Controls at Design Time.

To bind a multi-record control

  1. Set the control’s DataSource property to the object containing the data items you want to bind to.

    The container object must implement the IEnumerable interface; this includes objects such as:

  2. If the data source is a dataset, set the DataMember property to the name of the table to bind to. ( If this property is not set, by default the first table in the dataset is used. )
  3. If you are working with a DataList or DataGrid control, set the DataKeyField property to the name of a column that you can use at run time to identify individual items. If it is available, you usually use the primary key column from the data source.
  4. If you are working with a ListBox, DropDownList, CheckBoxList, RadioButtonList, or HtmlSelect control, set the DataTextField property to the name of the data column to display in the control.
  5. Optionally, set the DataValueField property to specify the name of a column that the control should return as its value when the user selects an item.
  6. Add code to the Web Forms page to call the control’s or the page’s DataBind method.

The following example shows how you can data bind a ListBox control. The example assumes that you have a dataset called DsTitles1 that contains a table called titles. The table contains two columns, the title column that you assign to the ListBox display text and the title_id column that you assign as the control’s value.

private void Page_Load ( object src, EventArgs e ) {
   myAdapter.Fill ( DsTitles1 );
   if ( ! this.IsPostBack ) {
      myListBox.DataSource = DsTitles1;
      myListBox.DataMember = "titles";
      myListBox.DataTextField = "title";
      myListBox.DataValueField = "title_id";
      myListBox.DataBind ( );
   }
}
  C# VB

The following example shows how to bind a ListBox control to the data returned when you use a DataCommand object to execute a SQL statement. The command object’s ExecuteReader method returns a data reader object ( in this case, of type SqlDataReader ), which you can directly bind a ListBox control to.

private void Page_Load ( object src, EventArgs e ) {
   if ( ! this.IsPostBack ) {
      myConn.Open ( );
      myCommandCommandText = "Select au_id, au_lname from authors";
      myListBox.DataSource = myCommandExecuteReader ( );
      myListBox.DataTextField = "au_lname";
      myListBox.DataValueField = "au_id";
      myListBox.DataBind ( );
      myConn.Close ( );
   }
}
  C# VB
See Also



© 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