Export (0) Print
Expand All
Expand Minimize

TableCell Web Server Control Declarative Syntax

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

    BackColor="color name|#dddddd"
    BorderColor="color name|#dddddd"
    ForeColor="color name|#dddddd"
    OnDataBinding="DataBinding event handler"
    OnDisposed="Disposed event handler"
    OnInit="Init event handler"
    OnLoad="Load event handler"
    OnPreRender="PreRender event handler"
    OnUnload="Unload event handler"

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 noteCaution:

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.

The following code example demonstrates how to create a table, add elements to the table programmatically, and display the table on the Web page. Note how TableCell controls are instantiated and their property values are set.

<%@ page language="C#" %>
<%@ Import Namespace="System.Drawing" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"  

<script runat="server">
    private void Page_Load(object sender, System.EventArgs e)
        // Create a TableItemStyle object that can be 
        // set as the default style for all cells 
        // in the table.
        TableItemStyle tableStyle = new TableItemStyle();
        tableStyle.HorizontalAlign = HorizontalAlign.Center;
        tableStyle.VerticalAlign = VerticalAlign.Middle;
        tableStyle.Width = Unit.Pixel(100);

        // Create more rows for the table. 
        for (int rowNum = 2; rowNum < 10; rowNum++)
            TableRow tempRow = new TableRow();
            for (int cellNum = 0; cellNum < 3; cellNum++)
                TableCell tempCell = new TableCell();
                tempCell.Text = 
                    String.Format("({0},{1})", rowNum, cellNum);

        // Apply the TableItemStyle to all rows in the table. 
        foreach (TableRow rw in Table1.Rows)
            foreach (TableCell cel in rw.Cells)

        // Create a header for the table.
        TableHeaderCell header = new TableHeaderCell();
        header.RowSpan = 1;
        header.ColumnSpan = 3;
        header.Text = "Table of (x,y) Values";
        header.Font.Bold = true;
        header.BackColor = Color.Gray;
        header.HorizontalAlign = HorizontalAlign.Center;
        header.VerticalAlign = VerticalAlign.Middle;

        // Add the header to a new row.
        TableRow headerRow = new TableRow();

        // Add the header row to the table.
        Table1.Rows.AddAt(0, headerRow);  

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>TableCell Example</title>
    <form id="form1" runat="server">

    <h1>TableCell Example</h1>
    <asp:table id="Table1" runat="server" 
        CellPadding="3" CellSpacing="3"
            <asp:TableCell Text="(0,0)" />
            <asp:TableCell Text="(0,1)" />
            <asp:TableCell Text="(0,2)" />
            <asp:TableCell Text="(1,0)" />
            <asp:TableCell Text="(1,1)" />
            <asp:TableCell Text="(1,2)" />


Community Additions

© 2014 Microsoft