TableCell 控件與 Table 控件和 TableRow 控件結合,用於創建表格中的單元。
提示:每行的單元均存儲在 TableRow 控件的 Cells 集合中。
AccessKey, Attributes, BackColor, BorderColor, BorderStyle, BorderWidth, CSSClass, Enabled, Font, EnableTheming, ForeColor, Height, IsEnabled, SkinID, Style, TabIndex, ToolTip, Width
AppRelativeTemplateSourceDirectory, BindingContainer, ClientID, Controls, EnableTheming, EnableVIEwState, ID, NamingContainer, Page, Parent, Site, TemplateControl, TemplateSourceDirectory, UniqueID, Visible
<ASP:TableCell
AccessKey="string"
AssociatedHeaderCellID="string"
BackColor="color name|#dddddd"
BorderColor="color name|#dddddd"
BorderPADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; PADDING-TOP: 0px"> Inset|Outset"
BorderWidth="size"
ColumnSpan="integer"
CSSClass="string"
Enabled="True|False"
EnableTheming="True|False"
EnableVIEwState="True|False"
Font-Bold="True|False"
Font-Italic="True|False"
Font-Names="string"
Font-Overline="True|False"
Font-Size="string|Smaller|Larger|XX-Small|X-Small|Small|Medium|
Large|X-Large|XX-Large"
Font-Strikeout="True|False"
Font-Underline="True|False"
ForeColor="color name|#dddddd"
Height="size"
HorizontalAlign="NotSet|Left|Center|Right|Justify"
ID="string"
OnDataBinding="DataBinding event handler"
OnDisposed="Disposed event handler"
OnInit="Init event handler"
OnLoad="Load event handler"
OnPreRender="PreRender event handler"
OnUnload="Unload event handler"
RowSpan="integer"
runat="server"
SkinID="string"
TabIndex="integer"
Text="string"
ToolTip="string"
VerticalAlign="NotSet|Top|Middle|Bottom"
Visible="True|False"
Width="size"
Wrap="True|False"
/></ASP:TableCell>
備注:TableCell 類的一個實例表示 Table 控件中的一個單元格。每一行的單元格都存儲在表示該行的 TableRow 的 Cells 集合中。使用 Text 屬性可操作單元格的內容。
此類使您可以控制單元格內容的顯示方式。設置 HorizontalAlign 和 VerticalAlign 屬性可以分別指定單元格內容的水平和垂直對齊方式。可使用 Wrap 屬性指定當到達單元格的結尾時單元格內容是否自動在下一行繼續。
還可指定單元格在 Table 控件中占用的行數或列數。RowSpan 和 ColumnSpan 屬性分別控制所用的行數和列數。
警告:文本在 TableCell 控件中顯示之前並非 HTML 編碼形式。這使得可以在文本中的 Html 標記中嵌入腳本。如果控件的值是由用戶輸入的,請務必要對輸入值進行驗證以防止出現安全漏洞。
下面的代碼示例演示如何創建一個表、以編程方式向表中添加元素並在網頁上顯示該表。注意 TableCell 控件是如何實例化的,以及如何設置它們的屬性值。
Visual Basic
<%@ 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>
C#
<%@ 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>