语言

HtmlTableRowCollection 类

定义

表示控件行HtmlTableRow的对象集合HtmlTable。 此类不能被继承。

public ref class HtmlTableRowCollection sealed : System::Collections::ICollection
public sealed class HtmlTableRowCollection : System.Collections.ICollection
type HtmlTableRowCollection = class
    interface ICollection
    interface IEnumerable
Public NotInheritable Class HtmlTableRowCollection
Implements ICollection
继承
HtmlTableRowCollection
实现

示例

下面的代码示例演示如何通过将行添加到HtmlTable集合来动态生成控件的内容HtmlTableRowCollection。 请注意, Rows 表的属性是 HtmlTableRowCollection 对象。

<%@ 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">
<script runat="server">

   void Page_Load(Object sender, EventArgs e)
   {

     // Get the number of rows and columns selected by the user.
     int numrows = Convert.ToInt32(Select1.Value);
     int numcells = Convert.ToInt32(Select2.Value);

     // Iterate through the rows.
     for (int j = 0; j < numrows; j++)
     {

       // Create a new row and add it to the Rows collection.
       HtmlTableRow row = new HtmlTableRow();

       // Provide a different background color for alternating rows.
       if (j % 2 == 1)
         row.BgColor = "Gray";

       // Iterate through the cells of a row.
       for (int i = 0; i < numcells; i++)
       {
         // Create a new cell and add it to the Cells collection.
         HtmlTableCell cell = new HtmlTableCell();
         cell.Controls.Add(new LiteralControl("row " +
                           j.ToString() +
                           ", cell " +
                           i.ToString()));
         row.Cells.Add(cell);
       }
       Table1.Rows.Add(row);
     }
   }

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
   <title>HtmlTableRowCollection Example</title>
</head>
<body>

   <form id="form1" runat="server">

      <h3>HtmlTableRowCollection Example</h3>

      <table id="Table1" 
             style="border-width:1; border-color:Black; padding:5"
             cellspacing="0"
             runat="server" />
        
      <hr />

      Select the number of rows and columns to create: <br /><br />

      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>

        

      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>
       
      <br /><br />
  
      <input type="submit" 
             value="Generate Table" 
             runat="server"/>

   </form>

</body>
</html>
<%@ 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">
<script runat="server">

  Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)

    Dim i As Integer
    Dim j As Integer
    Dim row As HtmlTableRow
    Dim cell As HtmlTableCell

    ' Get the number of rows and columns selected by the user.
    Dim numrows As Integer = CInt(Select1.Value)
    Dim numcells As Integer = CInt(Select2.Value)

    ' Iterate through the rows.
    For j = 0 To numrows - 1

      ' Create a new row and add it to the Rows collection.
      row = New HtmlTableRow()

      ' Provide a different background color for alternating rows.
      If (j Mod 2) = 1 Then
        row.BgColor = "Gray"
      End If

      ' Iterate through the cells of a row.
      For i = 0 To numcells - 1
           
        ' Create a new cell and add it to the Cells collection.
        cell = New HtmlTableCell()
        cell.Controls.Add(New LiteralControl("row " & _
                                          j.ToString() & _
                                          ", cell " & _
                                          i.ToString()))
        row.Cells.Add(cell)
            
      Next i

      Table1.Rows.Add(row)
         
    Next j
      
  End Sub

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
   <title>HtmlTableRowCollection Example</title>
</head>
<body>

   <form id="form1" runat="server">

      <h3>HtmlTableRowCollection Example</h3>

      <table id="Table1" 
             style="border-width:1; border-color:Black; padding:5"
             cellspacing="0"
             runat="server" />
        
      <hr />

      Select the number of rows and columns to create: <br /><br />

      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>

        

      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>
       
      <br /><br />
  
      <input type="submit" 
             value="Generate Table" 
             runat="server"/>

   </form>

</body>
</html>

注解

使用HtmlTableRowCollection类以编程方式管理表示控件中的HtmlTableRow行的对象集合HtmlTable。 此类通常用于添加、删除或修改控件中 HtmlTable 行的内容。

注释

控件 HtmlTable 包含一个 Rows 表示对象集合 HtmlTableRow 的属性。 每个 HtmlTableRow 行表示表中的单个行。 HtmlTableRow一个Cells属性表示对象的集合HtmlTableCell。 这些对象反过来表示表的各个单元格。 若要检索单个单元格,请首先从HtmlTableRow控件的集合中获取Rows包含单元格的HtmlTable行的对象。 然后,可以从集合中获取HtmlTableCell对象,该对象代表行CellsHtmlTableRow中的单元格。

属性

名称 说明
Count

获取集合中的HtmlTableRow对象数HtmlTableRowCollection。

IsReadOnly

获取一个值,该值指示集合是否 HtmlTableRowCollection 为只读。

IsSynchronized

获取一个值,该值指示是否同步对 HtmlTableRowCollection 集合的访问(线程安全)。

Item[Int32]

从HtmlTableRow集合中获取HtmlTableRowCollection指定索引处的对象。

SyncRoot

获取可用于同步对 HtmlTableRowCollection 集合的访问的对象。

方法

名称 说明
Add(HtmlTableRow)

将指定的 HtmlTableRow 对象追加到集合的 HtmlTableRowCollection 末尾。

Clear()

从集合中删除所有 HtmlTableRow 对象 HtmlTableRowCollection 。

CopyTo(Array, Int32)

从集合中的 HtmlTableRowCollection 项复制到指定 Array 对象,从数组中的指定索引处开始。

Equals(Object)

确定指定的对象是否等于当前对象。

(继承自 Object)
GetEnumerator()

返回一个 IEnumerator-implemented 对象,该对象包含集合中的所有HtmlTableRowHtmlTableRowCollection对象。

GetHashCode()

用作默认哈希函数。

(继承自 Object)
GetType()

获取当前实例的 Type。

(继承自 Object)
Insert(Int32, HtmlTableRow)

将对象 HtmlTableRow 添加到集合中的指定位置。

MemberwiseClone()

创建当前 Object的浅表副本。

(继承自 Object)
Remove(HtmlTableRow)

从HtmlTableRow集合中删除指定的HtmlTableRowCollection对象。

RemoveAt(Int32)

从 HtmlTableRow 集合中移除位于指定索引处的对象 HtmlTableRowCollection 。

ToString()

返回一个表示当前对象的字符串。

(继承自 Object)

扩展方法

名称 说明
AsParallel(IEnumerable)

启用查询的并行化。

AsQueryable(IEnumerable)

将 IEnumerable 转换为 IQueryable。

Cast<TResult>(IEnumerable)

将 IEnumerable 的元素强制转换为指定类型。

OfType<TResult>(IEnumerable)

根据指定类型筛选 IEnumerable 的元素。

适用于

另请参阅