Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Table

0.00/5 (No votes)
25 Jun 2008 1  
The Table class allows you to build an HTML table and specify its characteristics. A table can be built at design time with static content, but the

This articles was originally at wiki.asp.net but has now been given a new home on CodeProject. Editing rights for this article has been set at Bronze or above, so please go in and edit and update this article to keep it fresh and relevant.

The Table class allows you to build an HTML table and specify its characteristics. A table can be built at design time with static content, but the Table control is often built programmatically with dynamic contents

Each Table control is made up of rows (represented by instances of the TableRow class) stored in the Rows collection of the control. Each row is made up of cells (represented by instances of the TableCell class) stored in the Cells collection of the each TableRow.

Here is some sample code on adding rows and cells to a Table control:

TableCell tcell1 = new TableCell();
tcell1.Text =  "Cell1";
TableRow trow1 = new TableRow();
trow1.Cells.Add(tcell1);
table1.Rows.Add(trow1);

You can display an image in the background of the Table control by setting the BackImageUrl property. By default, the horizontal alignment of the items in the table is not set. If you want to specify the horizontal alignment, set the HorizontalAlignment property. The spacing between individual cells is controlled by the CellSpacing property. You can specify the amount of space between the contents of a cell and the cell's border by setting the CellPadding property. To display the cell borders, set the GridLines property. You can display the horizontal lines, vertical lines, or both horizontal and vertical lines.

Text is not HTML encoded before it is displayed in the Table control. This makes it possible to embed script within HTML tags in the text. If the values for the control come from user input, be sure to validate the values to help prevent security vulnerabilities.

We can use Server.HtmlEncode(String) to make sure that no direct javascript can be embedded in the HTML because of the use of the control.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here