asp.net.ph

Skip Navigation Links

Introduction to the Table Control

Controls You Can Use on Web Forms   ASP.NET Standard Controls   Table, TableRow, and TableCell Controls


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

Comparing the Table 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 a <table> element from the HTML tab of the Toolbox.
  • HtmlTable control — This is a <table> HTML element that has been converted to a control by adding the runat="server" attribute. You can program this control in server code.
  • Table Web Server control — 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’s 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 to Other ASP.NET List Controls

Some of the functions you might accomplish with a Table Web server control can also be accomplished with the list Web server controls: the Repeater, DataList, and DataGrid controls. All of these controls render ( or have the option to render ) as HTML tables. For a discussion of the differences among these three list controls, see Controls for Displaying Lists On Web Pages.

The Table control differs from the list controls in the following areas:

  • 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 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 of 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 TextBox control ) to the cell.

See Also

Adding Rows and Cells Dynamically to a Table Control   Displaying Database Information Using a Table Control   Web Forms Data Binding   Web Server Controls Templates



© 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