Controls You Can Use on Web Forms ASP.NET Standard Controls Table, TableRow, and TableCell Controls
It’s common to add rows and cells to a Table Web server control at run time. Rows are objects of type TableRow. The Row property of the Table control supports a collection of TableRow objects. To add a row to the table, you add a TableRow object to this collection.
Similarly, the TableRow object has a Cells property that supports a collection of objects of type TableCell. You can add cells to a row by manipulating this collection.
- To add a row, create a new object of type TableRow:
Dim tRow as New TableRow
- To add cells to the row, create one or more objects of type TableCell:
Dim tCell as New TableCell
- Add content to the new cell. You can do this in several ways, as shown in the following table.
To add |
Do this |
Static text |
Set the cell’s Text property. |
Controls |
Declare an instance of the control and add it to the cell’s Controls collection. |
Text and controls |
Declare the text by creating an instance of the LiteralControl class. Add it to the cell’s Controls collection as you would other controls. |
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 dynamically-generated controls whose values you want to preserve. For details, see Introduction to Web Forms State Management.
The following example shows how you can add rows and cells to a Table control. The number of rows and columns is determined by what the user enters into two text boxes. Each cell displays the row and cell number as static text.
The following example is similar to the previous, but displays static text and a Hyperlink control in each cell. The Hyperlink control navigates to a mocked-up URL, passing a mock product ID. Because the sample mixes static text and controls, the static text is implemented as a LiteralControl object, which is added to the cell’s Controls collection just like the Hyperlink control is.
Introduction to the Table Control Displaying Database Information Using a Table Control