Web服务器控件:Table控件
阅读此文请先查看IT教学网的:ASP.NET入门教程:Web服务器控件,简单讲述了Web服务器控件的使用方法。
定义和用法
Table 控件与 TableCell 控件和 TableRow 控件配合,用于创建表格。声明一个表并允许您以编程方式对其进行操作。
属性
属性 | 描述 | .NET |
---|---|---|
BackImageUrl | 表格的背景图像的 URL。 | 1.0 |
Caption | 表格的标题(caption)。 | 2.0 |
CaptionAlign | caption 文本的对齐方式。 | 2.0 |
CellPadding | 单元格边框与内容的间距。 | 1.0 |
CellSpacing | 单元格间距。 | 1.0 |
GridLines | The gridline format in the table | 1.0 |
HorizontalAlign | 表格在页面中的水平对齐方式。 | 1.0 |
Rows | 表格中的行集合。 | 1.0 |
runat | 规定该控件是服务器控件。必须设置为 "server"。 | 1.0 |
Web 控件标准属性
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:Table
AccessKey="string"
BackColor="color name|#dddddd"
BackImageUrl="uri"
BorderColor="color name|#dddddd"
BorderStyle="NotSet|None|Dotted|Dashed|Solid|Double|Groove|Ridge|
Inset|Outset"
BorderWidth="size"
Caption="string"
CaptionAlign="NotSet|Top|Bottom|Left|Right"
CellPadding="integer"
CellSpacing="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"
GridLines="None|Horizontal|Vertical|Both"
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"
runat="server"
SkinID="string"
Style="string"
TabIndex="integer"
ToolTip="string"
Visible="True|False"
Width="size"
>
<asp:TableFooterRow
AccessKey="string"
BackColor="color name|#dddddd"
BorderColor="color name|#dddddd"
BorderStyle="NotSet|None|Dotted|Dashed|Solid|Double|
Groove|Ridge|Inset|Outset"
BorderWidth="size"
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"
runat="server"
SkinID="string"
Style="string"
TabIndex="integer"
TableSection="TableHeader|TableBody|TableFooter"
ToolTip="string"
VerticalAlign="NotSet|Top|Middle|Bottom"
Visible="True|False"
Width="size"
>
<asp:TableCell
AccessKey="string"
AssociatedHeaderCellID="string"
BackColor="color name|#dddddd"
BorderColor="color name|#dddddd"
BorderStyle="NotSet|None|Dotted|Dashed|
Solid|Double|Groove|Ridge|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"
Style="string"
TabIndex="integer"
Text="string"
ToolTip="string"
VerticalAlign="NotSet|Top|Middle|Bottom"
Visible="True|False"
Width="size"
Wrap="True|False"
/>
<asp:TableHeaderCell
AbbreviatedText="string"
AccessKey="string"
AssociatedHeaderCellID="string"
BackColor="color name|#dddddd"
BorderColor="color name|#dddddd"
BorderStyle="NotSet|None|Dotted|Dashed|
Solid|Double|Groove|Ridge|Inset|
Outset"
BorderWidth="size"
CategoryText="string"
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"
Scope="NotSet|Row|Column"
SkinID="string"
Style="string"
TabIndex="integer"
Text="string"
ToolTip="string"
VerticalAlign="NotSet|Top|Middle|Bottom"
Visible="True|False"
Width="size"
Wrap="True|False"
/>
</asp:TableFooterRow>
<asp:TableHeaderRow
AccessKey="string"
BackColor="color name|#dddddd"
BorderColor="color name|#dddddd"
BorderStyle="NotSet|None|Dotted|Dashed|Solid|Double|
Groove|Ridge|Inset|Outset"
BorderWidth="size"
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"
runat="server"
SkinID="string"
Style="string"
TabIndex="integer"
TableSection="TableHeader|TableBody|TableFooter"
ToolTip="string"
VerticalAlign="NotSet|Top|Middle|Bottom"
Visible="True|False"
Width="size"
>
<asp:TableCell
AccessKey="string"
AssociatedHeaderCellID="string"
BackColor="color name|#dddddd"
BorderColor="color name|#dddddd"
BorderStyle="NotSet|None|Dotted|Dashed|
Solid|Double|Groove|Ridge|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"
Style="string"
TabIndex="integer"
Text="string"
ToolTip="string"
VerticalAlign="NotSet|Top|Middle|Bottom"
Visible="True|False"
Width="size"
Wrap="True|False"
/>
<asp:TableHeaderCell
AbbreviatedText="string"
AccessKey="string"
AssociatedHeaderCellID="string"
BackColor="color name|#dddddd"
BorderColor="color name|#dddddd"
BorderStyle="NotSet|None|Dotted|Dashed|
Solid|Double|Groove|Ridge|Inset|
Outset"
BorderWidth="size"
CategoryText="string"
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"
Scope="NotSet|Row|Column"
SkinID="string"
Style="string"
TabIndex="integer"
Text="string"
ToolTip="string"
VerticalAlign="NotSet|Top|Middle|Bottom"
Visible="True|False"
Width="size"
Wrap="True|False"
/>
</asp:TableHeaderRow>
<asp:TableRow
AccessKey="string"
BackColor="color name|#dddddd"
BorderColor="color name|#dddddd"
BorderStyle="NotSet|None|Dotted|Dashed|Solid|Double|
Groove|Ridge|Inset|Outset"
BorderWidth="size"
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"
runat="server"
SkinID="string"
Style="string"
TabIndex="integer"
TableSection="TableHeader|TableBody|TableFooter"
ToolTip="string"
VerticalAlign="NotSet|Top|Middle|Bottom"
Visible="True|False"
Width="size"
>
<asp:TableCell
AccessKey="string"
AssociatedHeaderCellID="string"
BackColor="color name|#dddddd"
BorderColor="color name|#dddddd"
BorderStyle="NotSet|None|Dotted|Dashed|
Solid|Double|Groove|Ridge|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"
Style="string"
TabIndex="integer"
Text="string"
ToolTip="string"
VerticalAlign="NotSet|Top|Middle|Bottom"
Visible="True|False"
Width="size"
Wrap="True|False"
/>
<asp:TableHeaderCell
AbbreviatedText="string"
AccessKey="string"
AssociatedHeaderCellID="string"
BackColor="color name|#dddddd"
BorderColor="color name|#dddddd"
BorderStyle="NotSet|None|Dotted|Dashed|
Solid|Double|Groove|Ridge|Inset|
Outset"
BorderWidth="size"
CategoryText="string"
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"
Scope="NotSet|Row|Column"
SkinID="string"
Style="string"
TabIndex="integer"
Text="string"
ToolTip="string"
VerticalAlign="NotSet|Top|Middle|Bottom"
Visible="True|False"
Width="size"
Wrap="True|False"
/>
</asp:TableRow>
</asp:Table>
备注:Table 类使您可以生成 HTML 表并指定其特征。可在设计时使用静态内容生成一个表,但 Table 控件通常使用动态内容以编程方式生成。
注意:以编程方式对表行或单元格进行的添加或修改不会在发送到服务器后继续保留。表行和单元格是其自身的控件,而不是 Table 控件的属性。对表行或单元格进行的更改必须在向服务器进行各次发送之后重建。如果需要进行大量修改,则使用 DataList 或 DataGrid 控件来代替 Table 控件。
每个 Table 控件由存储在该控件的 Rows 集合中的行(由 HtmlTableRow 类的实例表示)组成。每一行由存储在每个 TableRow 的 Cells 集合中的单元格(由 HtmlTableCell 类的实例表示)组成。看到本信息说明该文是通过网页教学(itjxue.com)整理发布的,请不要删掉!
通过设置 BackImageUrl 属性,可在 Table 控件的背景中显示一个图像。默认情况下,表中的项的水平对齐方式是未设置的。如果要指定水平对齐方式,请设置 HorizontalAlignment 属性。各个单元格之间的间距由 CellSpacing 属性控制。通过设置 CellPadding 属性,可指定单元格内容与单元格边框之间的空间量。若要显示单元格边框,请设置 GridLines 属性。可显示水平线、垂直线或同时显示这两种线。
警告:文本在 Table 控件中显示之前并非 HTML 编码形式。这使得可以在文本中的 HTML 标记中嵌入脚本。如果控件的值是由用户输入的,请务必要对输入值进行验证以防止出现安全漏洞。
示例
下面的代码示例演示如何在设计时创建一个表。该表包含两个单元格,每个单元格有两行。
注意:下面的代码示例使用单文件代码模型,如果直接复制到代码隐藏文件中,可能无法正常运行。此代码示例必须复制到一个具有 .aspx 扩展名的空文本文件。
Visual Basic
<%@ Page Language="VB" AutoEventWireup="True" %>
<html><body>
<h3>Table example, constructed at design time</h3>
<form runat=server>
<asp:Table id="Table1" runat="server"
CellPadding=10
GridLines="Both"
HorizontalAlign="Center">
<asp:TableRow>
<asp:TableCell>
Row 0, Col 0
</asp:TableCell>
<asp:TableCell>
Row 0, Col 1
</asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell>
Row 1, Col 0
</asp:TableCell>
<asp:TableCell>
Row 1, Col 1
</asp:TableCell>
</asp:TableRow>
</asp:Table>
</form>
</body>
</html>
C#
<%@ Page Language="C#" AutoEventWireup="True" %>
<html><body>
<h3>Table example, constructed at design time</h3>
<form runat=server>
<asp:Table id="Table1" runat="server"
CellPadding=10
GridLines="Both"
HorizontalAlign="Center">
<asp:TableRow>
<asp:TableCell>
Row 0, Col 0
</asp:TableCell>
<asp:TableCell>
Row 0, Col 1
</asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell>
Row 1, Col 0
</asp:TableCell>
<asp:TableCell>
Row 1, Col 1
</asp:TableCell>
</asp:TableRow>
</asp:Table>
</form>
</body>
</html>