Controls You Can Use on Web Forms ASP.NET Standard Controls Table, TableRow, and TableCell Controls
While for many applications, it is easier to display data-bound information using the Repeater, DataList, or DataGrid, it is possible to display database information using the Table Web server control.
In the most common scenario for displaying data in a Table control, each row of the table represents a data row from the data source, and each cell of the table displays a single field from a row. Unlike the list controls ( Repeater, DataList, etc. ), the Table control does not have a mechanism for automatically iterating through the rows in a data source. Therefore, you must do this yourself.
For general details about data binding in Web server controls, see Web Forms Data Binding.
- Set the contents of a TableCell control to the data to be displayed. Most often you set the cell’s Text property to an expression that extracts data from a data source. It is also possible to include controls in a TableCell control and bind the controls to a source of data. For details about using controls to display information, see Adding Rows and Cells Dynamically to a Table Control.
The following example illustrates one way to display data in a Table control. The sample shows an event-handling method for a button. The method loops through the DefaultView object of a dataset, creating a TableRow control for each row in the dataset. The code then creates a TableCell control in each table row and sets its Text property to the "au_id" field of the current data row.
public void Button1_Click ( object src, EventArgs e ) {
// Set up a dataview on the dataset.
FillDataSet ( aDataSet1 );
DataBind ( );
DataView dv = aDataSet1.Tables [ 0 ].DefaultView;
// Create a row with one column for each row in the DataView
foreach ( DataRowView dataRow in dv ) {
TableRow tRow = new TableRow ( );
TableCell tCell = new TableCell ( );
tCell.Text = dataRow.Row.ItemArray [ 0 ].ToString ( );
tRow.Cells.Add ( tCell );
Table1.Rows.Add ( tRow );
}
}
Public Sub Button1_Click ( ByVal src As Object, ByVal e As EventArgs )
FillDataSet ( ADataSet1 )
Me.DataBind ( )
Dim dv As DataView
dv = Me.ADataSet1.Tables ( 0 ) .DefaultView
' Create a row with one column for each row in the DataView
Dim datarow As DataRowView
For Each datarow In dv
Dim tRow As New TableRow ( )
Dim tCell As New TableCell ( )
tCell.Text = datarow.Item ( "au_id" ) .ToString ( )
tRow.Cells.Add ( tCell )
Me.Table1.Rows.Add ( tRow )
Next
End Sub |
|
C# |
VB |
NOTE: Controls that you add dynamically to a Web Forms page do not automatically become part of the page’s view state — neither the controls nor their values are saved when a page performs a round trip to the server. You are therefore responsible for saving the state of any dyanamically-generated controls whose values you want to preserve. For details, see Introduction to Web Forms State Management.
Introduction to the Table Control Adding Rows and Cells Dynamically to a Table Control Web Forms Data Binding