创建一个服务器端控件,该控件映射到 <table> HTML 元素并允许您创建表。
<table EnableViewState="False|True" Id="string" Visible="False|True" OnDataBinding="OnDataBinding event handler" OnDisposed="OnDisposed event handler" OnInit="OnInit event handler" OnLoad="OnLoad event handler" OnPreRender="OnPreRender event handler" OnUnload="OnUnload event handler" runat="server" > <tr> <td></td> </tr> </table> |
备注
使用
),这些行存储在表的
若要创建表,首先应在页上的窗体中声明一个 HtmlTable 控件。然后,将 HtmlTableRow 对象放置在 HtmlTable 控件的开始和结束标记之间(对于表中所需的每一行放置一个对象)。定义表中的行之后,声明位于每个 HtmlTableRow 对象的开始和结束标记之间的 HtmlTableCell对象以创建该行的单元格。
![]() |
---|
请确保每一行和每一列中的单元格数目正确,否则该表可能不会按预期的方式显示。一般情况下,各行的单元格数应相同。同样,各列的单元格数也应相同。如果要合并单元格,则各行的宽度应相同,各列的高度也应相同。 |
HtmlTable 控件允许您自定义表的外观。通过设置
示例
下面的示例基于用户在两个
Visual Basic? | ![]() |
---|---|
<%@ Page Language="VB" AutoEventWireup="True" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head> <title>HtmlTable Control</title> <script runat="server"> Sub Page_Load(sender As Object, e As EventArgs) Dim row As Integer = 0 ' Generate rows and cells. Dim numrows As Integer = Convert.ToInt32(Select1.Value) Dim numcells As Integer = Convert.ToInt32(Select2.Value) Dim j As Integer For j = 0 To numrows - 1 Dim r As New HtmlTableRow() ' Set bgcolor on alternating rows. If row Mod 2 = 1 Then r.BgColor = "Gainsboro" End If row += 1 Dim i As Integer For i = 0 To numcells - 1 Dim c As New HtmlTableCell() c.Controls.Add(New LiteralControl("row " & j.ToString() & _ ", cell " & i.ToString())) r.Cells.Add(c) Next i Table1.Rows.Add(r) Next j End Sub </script> </head> <body> <h3>HtmlTable Example</h3> <form id="Form1" runat="server"> <p> <table id="Table1" CellPadding=5 CellSpacing=0 Border="1" BorderColor="black" runat="server" /> <p> Table rows: <select id="Select1" runat="server"> <option Value="1">1</option> <option Value="2">2</option> <option Value="3">3</option> <option Value="4">4</option> <option Value="5">5</option> </select> <br> Table cells: <select id="Select2" runat="server"> <option Value="1">1</option> <option Value="2">2</option> <option Value="3">3</option> <option Value="4">4</option> <option Value="5">5</option> </select> <input id="Submit1" type="submit" value="Generate Table" runat="server"> </form> </body> </html> |
C#? | ![]() |
---|---|
<%@ Page Language="C#" AutoEventWireup="True" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head> <title>HtmlTable Control</title> <script runat="server"> void Page_Load(Object sender, EventArgs e) { int row = 0; // Generate rows and cells. int numrows = Convert.ToInt32(Select1.Value); int numcells = Convert.ToInt32(Select2.Value); for (int j=0; j<numrows; j++) { HtmlTableRow r = new HtmlTableRow(); // Set bgcolor on alternating rows. if (row%2 == 1) r.BgColor="Gainsboro"; row++; for (int i=0; i<numcells; i++) { HtmlTableCell c = new HtmlTableCell(); c.Controls.Add(new LiteralControl("row " + j.ToString() + ", cell " + i.ToString())); r.Cells.Add(c); } Table1.Rows.Add(r); } } </script> </head> <body> <h3>HtmlTable Example</h3> <form id="Form1" runat="server"> <p> <table id="Table1" CellPadding=5 CellSpacing=0 Border="1" BorderColor="black" runat="server" /> <p> Table rows: <select id="Select1" runat="server"> <option Value="1">1</option> <option Value="2">2</option> <option Value="3">3</option> <option Value="4">4</option> <option Value="5">5</option> </select> <br> Table cells: <select id="Select2" runat="server"> <option Value="1">1</option> <option Value="2">2</option> <option Value="3">3</option> <option Value="4">4</option> <option Value="5">5</option> </select> <input id="Submit1" type="submit" value="Generate Table" runat="server"> </form> </body> </html> |
请参见
参考
HtmlTableCell 服务器控件声明性语法HtmlTableRow 服务器控件声明性语法
HtmlForm 服务器控件声明性语法
其他资源
HTML 服务器控件