Freigeben über


TableCell Web Server Control

Represents a cell in a Table control and allows you to manipulate it programmatically.

<asp:TableCell id="TableCell1"
     ColumnSpan="colcount"
     RowSpan="rowcount"
     HorizontalAlign="Center|Justify|Left|NotSet|Right"
     VerticalAlign="Bottom|Middle|NotSet|Top"
     Wrap="True|False"
     runat="server">
Cell text
</asp:TableCell>

Remarks

An instance of the TableCell class represents a cell in a Table control. The cells of each row are stored in the Cells collection of the TableRow representing the row. You can manipulate the contents of a cell by using the Text property.

This class allows you to control how the contents of the cell are displayed. Setting the HorizontalAlign and VerticalAlign properties specifies the horizontal and vertical alignment of the contents in the cell, respectively. You can use the Wrap property to specify whether the contents of the cell automatically continue on the next line when the end of the cell is reached.

You can also specify how many rows or columns a cell occupies in the Table control. The RowSpan and ColumnSpan properties control how many rows and columns are used, respectively.

CAUTION   Text is not HTML encoded before it is displayed in the TableCell 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 TableCell Web server control's properties and events, see the TableCell Class documentation.

Example

The following example demonstrates how to use a TableCell to represent a cell to a Table control.

<%@ 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>TableCell 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><TableCell 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 | TableCell Class