Partager via


Table Web Server Control Overview

The Table Web server control allows you to create server-programmable tables on ASP.NET pages. The TableRow and TableCell Web server controls provide a way to display the actual content for the Table control.

Comparing the Table Web Server Control to Other Table Elements

Tables are typically used not only to present tabular information, but as a way to format information on a Web page. There are a number of ways to create tables on your Web Forms page:

  • HTML table. You can add an HTML <table> element. If you are creating a static table (one in which you will not add or change content at run time), you should use an HTML table and not a Table control.

  • HtmlTable control. This is a <table> HTML element that has been converted to an HTML server control by adding the runat=server attribute. You can program this control in server code. For details about HTML server controls, see ASP.NET Web Server Controls Overview.

  • Table. A Web control that allows you to create and manipulate tables (for example, adding table rows and cells) using an object model that is consistent with other Web controls.

In general, you use a Table Web server control when you intend to add rows and cells (columns) to the table in code at run time. Although you can use it as a static table with predefined rows and columns, it is easier in this case to work with the HTML <table> element.

The Table Web server control can be easier to program than the HtmlTable control because it offers an object model with typed properties that is consistent with other Web server controls. (The model is also consistent between the Table, TableRow, and TableCell controls.)

Comparing the Table Web Server Control to Other List Web Server Controls

Some of the functions you might accomplish with a Table Web server control can also be accomplished with the list Web server controls, specifically the Repeater, DataList, and GridView controls. All these controls are rendered (or have the option to be rendered) as HTML tables.

The differences between the list controls and the Table control are these:

  • The list controls are data-bound. The list controls work only against a data source, whereas the Table control can display any combination of HTML text and controls, whether or not they are data-bound.

  • The list controls use templates to specify the layout of their elements. The Table control supports the TableCell child control, which you can fill as you would any HTML <td> element.

Table Web Server Control Object Model

The Table control acts as a parent control for TableRow controls. The table supports a property called Rows that is a collection of TableRow objects. By managing this collection — adding or deleting items in it — you specify the rows for the table. The TableRow control in turn supports a collection called Cells that contains TableCell objects.

The content to be displayed in the table is added to the TableCell control. The cell has a Text property that you set to any HTML text. Alternatively, you can display controls in the cell by creating instances of, and adding controls to, the cell's Controls collection.

The parent Table control supports properties to control the appearance of the entire table, such as Font, BackColor, and ForeColor. The TableRow and TableCell controls support these properties as well, so you can specify the look of individual rows or cells, overriding the parent table appearance.

Binding Data to the Control

Although the Table control (unlike the list Web server controls; see above) is not inherently data-bound, you can use it to display data from a database.

As with all Web server controls, you can bind any property of a Table control to a data source. However, the Table control does not support a property that you use to directly display data. Instead, you typically add TableCell controls to the table. You can then either bind the Text property of individual TableCell controls to data, or you can add data-bound controls (such as a Label or Literal control) to the cell.

See Also

Tasks

How to: Add Rows and Cells Dynamically to a Table Web Server Control