Table.Rows 属性

定义

获取 Table 控件中行的集合。

public:
 virtual property System::Web::UI::WebControls::TableRowCollection ^ Rows { System::Web::UI::WebControls::TableRowCollection ^ get(); };
[System.Web.UI.PersistenceMode(System.Web.UI.PersistenceMode.InnerDefaultProperty)]
public virtual System.Web.UI.WebControls.TableRowCollection Rows { get; }
[<System.Web.UI.PersistenceMode(System.Web.UI.PersistenceMode.InnerDefaultProperty)>]
member this.Rows : System.Web.UI.WebControls.TableRowCollection
Public Overridable ReadOnly Property Rows As TableRowCollection

属性值

TableRowCollection,它包含 TableRow 控件的 Table 对象。

属性

示例

下面的示例演示如何使用 Rows 集合以编程方式构造表。 动态创建表包括三个步骤。 首先,创建 TableCell 对象来表示行中的单元格。 通过设置 Text 属性或将控件添加到 的TableCell集合来Control.Controls添加单元格的内容。 接下来,创建 以 TableRow 表示表中的行。 将 TableCell 之前创建的对象添加到 Cells 的 集合中 TableRow。 最后,将 TableRow 添加到 Rows 控件的 Table 集合。 对表中的每一行重复此过程。

注意

下面的代码示例使用单文件代码模型,如果直接复制到代码隐藏文件中,可能无法正常工作。 必须将此代码示例复制到具有 .aspx 扩展名的空文本文件中。 有关Web Forms代码模型的详细信息,请参阅 ASP.NET Web Forms 页代码模型

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

    private void Page_Load(Object sender, EventArgs e)
    {
        // Generate rows and cells.           
        int numrows = 3;
        int numcells = 2;
        for (int j = 0; j < numrows; j++)
        {          
            TableRow r = new TableRow();
            for (int i = 0; i < numcells; i++) {
                TableCell c = new TableCell();
                c.Controls.Add(new LiteralControl("row " 
                    + j.ToString() + ", cell " + i.ToString()));
                r.Cells.Add(c);
            }
            Table1.Rows.Add(r);
        }
    }

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

    <h3>Table Example, constructed programmatically</h3>
    <asp:Table id="Table1" 
        GridLines="Both" 
        HorizontalAlign="Center" 
        Font-Names="Verdana" 
        Font-Size="8pt" 
        CellPadding="15" 
        CellSpacing="0" 
        Runat="server"/>

    </div>
    </form>
</body>
</html>
<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
    Sub Page_Load(sender As Object, e As EventArgs)
        ' Generate rows and cells.           
        Dim numrows As Integer = 3
        Dim numcells As Integer = 2
        Dim j As Integer
        For j = 0 To numrows - 1
            Dim r As New TableRow()
            Dim i As Integer
            For i = 0 To numcells - 1
                Dim c As New TableCell()
                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 'Page_Load
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Programmatic Table Example</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

    <h3>Table Example, constructed programmatically</h3>
    <asp:Table id="Table1" 
        GridLines="Both" 
        HorizontalAlign="Center" 
        Font-Names="Verdana" 
        Font-Size="8pt" 
        CellPadding="15" 
        CellSpacing="0" 
        Runat="server"/>

    </div>
    </form>
</body>
</html>

注解

Rows使用 集合以编程方式管理 TableRow 控件中的 Table 对象。 表示 TableRow 表中的一行。

注意

此属性通常仅在以编程方式生成表时使用。 在设计时,它是通过在控件的开始标记和结束标记之间声明 TableRow 对象来设置的 Table

适用于

另请参阅