TableCell 类

表示 Table 控件中的单元格。

**命名空间:**System.Web.UI.WebControls
**程序集:**System.Web(在 system.web.dll 中)

语法

声明
<BindableAttribute(False)> _
Public Class TableCell
    Inherits WebControl
用法
Dim instance As TableCell
[BindableAttribute(false)] 
public class TableCell : WebControl
[BindableAttribute(false)] 
public ref class TableCell : public WebControl
/** @attribute BindableAttribute(false) */ 
public class TableCell extends WebControl
BindableAttribute(false) 
public class TableCell extends WebControl

备注

TableCell 类表示 Table 控件中的单元格。可以使用 Text 属性指定或确定单元格的内容。

使用 TableCell 类可以控制单元格内容的显示方式。通过设置 HorizontalAlignVerticalAlign 属性来指定单元格内容的对齐方式。可以使用 Wrap 属性指定单元格内容是否在单元格内换行。

也可以指定在 Table 控件表示的表中由一个单元格所占的行数和列数。RowSpanColumnSpan 属性分别控制使用的行数和列数。

警告

此控件可用来显示用户输入,而该输入可能包含恶意的客户端脚本。在应用程序中显示从客户端发送来的任何信息之前,请检查它们是否包含可执行脚本、SQL 语句或其他代码。ASP.NET 提供输入请求验证功能以阻止用户输入中的脚本和 HTML。还提供验证服务器控件以判断用户输入。有关更多信息,请参见 保证标准控件的安全如何:通过对字符串应用 HTML 编码在 Web 应用程序中防止脚本侵入在 ASP.NET 网页中验证用户输入

主题 位置
如何:在 Calendar Web 服务器控件中自定义个别日 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:向 Table Web 服务器控件动态添加行和单元格 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:在 Calendar Web 服务器控件中自定义个别日 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:向 Table Web 服务器控件动态添加行和单元格 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:在 Calendar Web 服务器控件中自定义个别日 生成 ASP .NET Web 应用程序
如何:向 Table Web 服务器控件动态添加行和单元格 生成 ASP .NET Web 应用程序

示例

下面的代码示例演示如何创建表,如何以编程方式向表添加元素,以及如何在网页上显示表。注意 TableCell 控件的实例化方式以及其属性值的设置方式。

提示

下面的代码示例使用单文件代码模型,当它直接复制到代码隐藏文件时可能不能正常工作。此代码示例必须被复制到具有 .aspx 扩展名的空文本文件中。有关 Web 窗体代码模型的更多信息,请参见 ASP.NET 网页代码模型

<%@ Page language="VB" AutoEventWireup="true" %>
<%@ Import Namespace="System.Drawing" %>

<html>
    <head>
        <script runat="server">
            Private Sub Page_Load(sender As Object, e As System.EventArgs)
                ' Create a TableItemStyle object that can be
                ' set as the default style for all cells
                ' in the table.
                Dim tableStyle As New TableItemStyle()
                tableStyle.HorizontalAlign = HorizontalAlign.Center
                tableStyle.VerticalAlign = VerticalAlign.Middle
                tableStyle.Width = Unit.Pixel(100)
                ' Create more rows for the table.
                Dim i As Integer
                For i = 2 To 9
                    Dim tempRow As New TableRow()
                    Dim j As Integer
                    For j = 0 To 2
                        Dim tempCell As New TableCell()
                        tempCell.Text = "(" & i & "," & j & ")"
                        tempRow.Cells.Add(tempCell)
                    Next j
                    Table1.Rows.Add(tempRow)
                Next i

                ' Apply the TableItemStyle to all rows in the table.
                Dim r As TableRow
                For Each r In  Table1.Rows
                    Dim c As TableCell
                    For Each c In  r.Cells
                        c.ApplyStyle(tableStyle)
                    Next c 
                Next r

                ' Create a header for the table.
                Dim header As 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.
                Dim headerRow As New TableRow()
                headerRow.Cells.Add(header)

                ' Add the header row to the table.
                Table1.Rows.AddAt(0, headerRow) 
            End Sub
        </script>
    </head>

    <body>
        <form runat="server">
            <h1>TableCell Example</h1>
            <asp:table id="Table1" runat="server" CellPadding="3" CellSpacing="3">
                <asp:TableRow>
                    <asp:TableCell Text="(0,0)"></asp:TableCell>
                    <asp:TableCell Text="(0,1)"></asp:TableCell>
                    <asp:TableCell Text="(0,2)"></asp:TableCell>
                </asp:TableRow>
                <asp:TableRow>
                    <asp:TableCell Text="(1,0)"></asp:TableCell>
                    <asp:TableCell Text="(1,1)"></asp:TableCell>
                    <asp:TableCell Text="(1,2)"></asp:TableCell>
                </asp:TableRow>
            </asp:table>
        </form>
    </body>
</html>
<%@ Page language="C#" AutoEventWireup="true" %>
<%@ Import Namespace="System.Drawing" %>

<html>
    <head>
        <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 i = 2; i < 10; i++)
                {
                    TableRow tempRow = new TableRow();
                    for (int j = 0; j < 3; j++)
                    {
                        TableCell tempCell = new TableCell();
                        tempCell.Text = "(" + i + "," + j + ")";
                        tempRow.Cells.Add(tempCell);
                    }
                    Table1.Rows.Add(tempRow);
                }

                // Apply the TableItemStyle to all rows in the table.
                foreach (TableRow r in Table1.Rows)
                    foreach (TableCell c in r.Cells)
                        c.ApplyStyle(tableStyle);

                // 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();
                headerRow.Cells.Add(header);

                // Add the header row to the table.
                Table1.Rows.AddAt(0, headerRow);  
            }
        </script>
    </head>

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

.NET Framework 安全性

继承层次结构

System.Object
   System.Web.UI.Control
     System.Web.UI.WebControls.WebControl
      System.Web.UI.WebControls.TableCell
         System.Web.UI.WebControls.DataControlFieldCell
         System.Web.UI.WebControls.TableHeaderCell

线程安全

此类型的任何公共静态(Visual Basic 中的 Shared)成员都是线程安全的,但不保证所有实例成员都是线程安全的。

平台

Windows 98、Windows 2000 SP4、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

请参见

参考

TableCell 成员
System.Web.UI.WebControls 命名空间
Table 类
Text
HorizontalAlign
VerticalAlign
Wrap
RowSpan
ColumnSpan

其他资源

Table、TableRow 和 TableCell Web 服务器控件
保证标准控件的安全
如何:通过对字符串应用 HTML 编码在 Web 应用程序中防止脚本侵入
在 ASP.NET 网页中验证用户输入