This documentation is archived and is not being maintained.

Table Web Server Control

Declares a table and allows you to manipulate it programmatically.

<asp:Table id="Table1"
     BackImageUrl="url"
     CellSpacing="cellspacing"
     CellPadding="cellpadding"
     GridLines="None|Horizontal|Vertical|Both"
     HorizontalAlign="Center|Justify|Left|NotSet|Right"
     runat="server">

   <asp:TableRow>

      <asp:TableCell>
         Cell text
      </asp:TableCell>

   </asp:TableRow>

</asp:Table>

Remarks

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.

Note   Programmatic additions or modifications to a table row or cell will not persist across posts to the server. Table rows and cells are controls of their own, not properties of the Table control. Changes to table rows or cells must be reconstructed after each post to the server. If substantial modifications are expected, use the DataList or DataGrid controls instead of the Table control.

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.

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.

CAUTION   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.

For detailed information on the Table Web server control's properties and events, see the Table Class documentation.

Example

The following example demonstrates how to dynamically create a Table control in the Web page.

<%@ Page Language="VB" AutoEventWireup="True" %>
<html>
<head>
   <script runat="server">
      Sub Page_Load(sender As Object, e As EventArgs)
         ' Generate rows and cells.           
         Dim numrows As Integer = 3
         Dim numcells As Integer = 2
         Dim j As Integer
         For j = 0 To numrows - 1
            Dim r As New TableRow()
            Dim i As Integer
            For i = 0 To numcells - 1
               Dim c As New TableCell()
               c.Controls.Add(New LiteralControl("row " & j.ToString() & _
                              ", cell " & i.ToString()))
               r.Cells.Add(c)
            Next i
            Table1.Rows.Add(r)
         Next j
      End Sub 'Page_Load
   </script>
</head>
<body>
   <form runat="server">
      <h3>Table Example</h3>

      <asp:Table id="Table1" 
           Font-Name="Verdana" 
           Font-Size="8pt" 
           CellPadding="5" 
           CellSpacing="0"           
           BorderWidth="1" 
           Gridlines="Both" 
           runat="server"/>
   </form>
</body>
</html>
[C#]
<%@ Page Language="C#" AutoEventWireup="True" %>
<html>
<head>
   <script runat="server">
      void Page_Load(Object sender, EventArgs e) 
      {
         // Generate rows and cells.           
         int numrows = 3;
         int numcells = 2;
         for (int j=0; j<numrows; j++) 
         {          
            TableRow r = new TableRow();
            for (int i=0; i<numcells; i++) 
            {
               TableCell c = new TableCell();
               c.Controls.Add(new LiteralControl("row " + j.ToString() +
                              ", cell " + i.ToString()));
               r.Cells.Add(c);
            }
            Table1.Rows.Add(r);
         }
      }
   </script>
</head>
<body>
   <form runat=server>
      <h3>Table Example</h3>
      <asp:Table id="Table1" 
           GridLines="Both" 
           HorizontalAlign="Center" 
           Font-Name="Verdana" 
           Font-Size="8pt" 
           CellPadding="15" 
           CellSpacing="0" 
           runat="server"/>
   </form>
</body>
</html>

See Also

Web Server Controls | Table Class

Show: