Export (0) Print
Expand All

HtmlTableCell Class

Represents the <td> and <th> HTML elements in an HtmlTableRow.

For a list of all members of this type, see HtmlTableCell Members.

System.Object
   System.Web.UI.Control
      System.Web.UI.HtmlControls.HtmlControl
         System.Web.UI.HtmlControls.HtmlContainerControl
            System.Web.UI.HtmlControls.HtmlTableCell

[Visual Basic]
Public Class HtmlTableCell
   Inherits HtmlContainerControl
[C#]
public class HtmlTableCell : HtmlContainerControl
[C++]
public __gc class HtmlTableCell : public HtmlContainerControl
[JScript]
public class HtmlTableCell extends HtmlContainerControl

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Remarks

The <td> element represents a data cell in a table, while the <th> element represents a table heading cell. Use the HtmlTableCell class to programmatically control the <td> and <th> HTML elements in an HtmlTableRow.

The HtmlTableCell class allows you to customize the appearance of a cell in a table by specifying the background color, the border color, the height, and the width. These attributes are set using the BgColor, BorderColor, Height, and Width properties.

You can control how the content of a cell is displayed by specifying the vertical and horizontal alignment of the contents. You can also specify whether the content automatically continues on the next line when the end of the cell is reached. The horizontal alignment is controlled by the Align property, while the VAlign property controls the vertical alignment. The NoWrap property indicates whether text wrapping is disabled.

By using the ColSpan or RowSpan property, you can control the number of columns or rows that a cell occupies. For example, you can create a cell that occupies two columns in a table by setting the ColSpan property to 2.

For a list of initial property values for an instance of HtmlTableCell, see the HtmlTableCell constructor.

CAUTION   This control can be used to display user input, which might include malicious client script. Check any information that is sent from a client for executable script, SQL statements, or other code before displaying it in your application. ASP.NET provides an input request validation feature to block script and HTML in user input. Validation server controls are also provided to assess user input. For more information, see Validation Server Controls.

Example

[Visual Basic, C#] The following example demonstrates how to use an HtmlTableCell to modify the contents of a cell in a table. Note that the Rows collection of an HtmlTable control contains a collection of the rows (HtmlTableRow objects) in the table. The Cells collection of an HtmlTableRow is a collection of the cells (HtmlTableCell objects) in a row.

[Visual Basic] 
<%@ Page Language="VB" AutoEventWireup="True" %>

<html>
<head>

   <script runat="server">

      Sub Button_Click(sender As Object, e As EventArgs) 
      
         Dim i As Integer
         Dim j As Integer

         ' Iterate through the rows of the table.
         For i=0 To Table1.Rows.Count - 1

            ' Iterate through the cells of a row.       
            For j=0 To Table1.Rows(i).Cells.Count - 1
            
               ' Change the inner HTML of the cell.
               Table1.Rows(i).Cells(j).InnerHtml = "Row " & i.ToString() & _ 
                                                   ", Column " & j.ToString() 
            Next j

         Next i

      End Sub

   </script>

</head>
<body>

   <form runat="server">

      <h3>HtmlTableCell Example</h3>

      <table id="Table1" 
             Border="1" 
             BorderColor="black" 
             runat="server">

         <tr>
            <td>
               Cell 1
            </td>
            <td>
               Cell 2
            </td>
         </tr>
         <tr>
            <td>
               Cell 3
            </td>
            <td>
               Cell 4
            </td>
         </tr>

      </table>

      <br><br>
  
      <input type="button" 
             value="Change Table Contents"
             OnServerClick = "Button_Click" 
             runat="server"/>

   </form>

</body>
</html>

[C#] 
<%@ Page Language="C#" AutoEventWireup="True" %>

<html>
<head>

   <script runat="server">

      void Button_Click(Object sender, EventArgs e) 
      {

         // Iterate through the rows of the table.
         for (int i=0; i<=Table1.Rows.Count - 1; i++)
         {

            // Iterate through the cells of a row.
            for (int j=0; j<=Table1.Rows[i].Cells.Count - 1; j++)
            {
               // Change the inner HTML of the cell.
               Table1.Rows[i].Cells[j].InnerHtml = "Row " + i.ToString() + 
                                                   ", Column " + j.ToString(); 
            }

         }

      }

   </script>

</head>
<body>

   <form runat="server">

      <h3>HtmlTableCell Example</h3>

      <table id="Table1" 
             Border="1" 
             BorderColor="black" 
             runat="server">

         <tr>
            <td>
               Cell 1
            </td>
            <td>
               Cell 2
            </td>
         </tr>
         <tr>
            <td>
               Cell 3
            </td>
            <td>
               Cell 4
            </td>
         </tr>

      </table>

      <br><br>
  
      <input type="button" 
             value="Change Table Contents"
             OnServerClick = "Button_Click" 
             runat="server"/>

   </form>

</body>
</html>

[C++, JScript] No example is available for C++ or JScript. To view a Visual Basic or C# example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System.Web.UI.HtmlControls

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

Assembly: System.Web (in System.Web.dll)

See Also

HtmlTableCell Members | System.Web.UI.HtmlControls Namespace | HtmlTable | Rows | HtmlTableRow | Cells | Align | BgColor | BorderColor | ColSpan | Height | NoWrap | RowSpan | VAlign | Width

Show:
© 2014 Microsoft