Partilhar via


Table, TableRow, and TableCell Web Server Control Overview

The Table Web server control enables you to create tables on ASP.NET pages that you can program in server code. The TableRow and TableCell Web server controls provide a way to display the content for the Table control.

This topic contains:

  • Background

  • Code Examples

  • Class Reference

Background

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 ASP.NET pages:

  • HTML table. 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. This is a Web control that enables you to create and manipulate tables (for example, adding table rows and cells) by 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 that 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 named Rows that is a collection of TableRow objects. By adding or deleting items in this collection, you specify the rows for the table. The TableRow control in turn supports a collection named 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 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 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.

Back to top

Code Examples

How to: Add Table Web Server Controls to a Web Forms Page

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

Back to top

Class Reference

The following table lists the classes that relate to the Table control, the TableCell control, and the TableRow control.

Member

Description

Table

The main class for the Table control.

TableCell

The main class for the TableCell control.

TableCellCollection

Encapsulates a collection of TableHeaderCell and TableCell objects that make up a row in a Table control.

TableFooterRow

Represents a footer row in a Table control.

TableHeaderCell

Represents a heading cell within a Table control.

TableHeaderRow

Represents a heading row in a Table control.

TableHeaderScope

Represents the HTML scope attribute for classes that represent header cells in a table.

TableItemStyle

Represents the style properties for an element of a control that renders as a TableRow or TableCell.

TableRow

The main class for the TableRow control.

TableRowCollection

Encapsulates a collection of TableRow objects that represent a single row in a Table control.

TableRowSection

Specifies where a TableRow object is placed in a Table control.

TableSectionStyle

Represents the style for a section of a Table control.

TableStyle

Represents the style for the Table control.

Back to top

See Also

Reference

HtmlTable

HtmlTableRow

HtmlTableCell