Share via


HtmlTableRowCollection.GetEnumerator 方法

定义

返回一个实现了 IEnumerator 的对象,其中包含 HtmlTableRow 集合内的所有 HtmlTableRowCollection 对象。

public:
 virtual System::Collections::IEnumerator ^ GetEnumerator();
public System.Collections.IEnumerator GetEnumerator ();
abstract member GetEnumerator : unit -> System.Collections.IEnumerator
override this.GetEnumerator : unit -> System.Collections.IEnumerator
Public Function GetEnumerator () As IEnumerator

返回

一个实现了 IEnumerator 的对象,其中包含 HtmlTableRow 内的所有 HtmlTableRowCollection 对象。

实现

示例

下面的代码示例演示如何使用 GetEnumerator 方法创建 System.Collections.IEnumerator 对象。 System.Collections.IEnumerator然后循环访问 对象以显示集合的内容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 Button_Click(Object sender, EventArgs e)
  {

    HtmlTableRow current_row;

    // Create an IEnumerator enumerator.
    IEnumerator myEnum = Table1.Rows.GetEnumerator();

    Span1.InnerText = "The items in the rows of the table are: ";

    // Iterate through the IEnumerator and display its contents.
    while (myEnum.MoveNext())
    {

      current_row = (HtmlTableRow)myEnum.Current;
      Span1.InnerText = Span1.InnerText + " " + current_row.Cells[0].InnerText +
                        " " + current_row.Cells[1].InnerText;

    }

  }

</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" runat="server" 
       style="border-width: 1; border-color: Black">

         <tr>
            <td>
               Cell 1
            </td>
            <td>
               Cell 2
            </td>
         </tr>
         <tr>
            <td>
               Cell 3
            </td>
            <td>
               Cell 4
            </td>
         </tr>

      </table>

      <br /><br />
  
      <input type="button" 
             value="Display row contents in the table"
             onserverclick="Button_Click" 
             runat="server"/>

      <br /><br />

      <span id="Span1"
            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 Button_Click(ByVal sender As Object, ByVal e As EventArgs)
 
    Dim current_row As HtmlTableRow

    ' Create an IEnumerator enumerator.
    Dim myEnum As IEnumerator = Table1.Rows.GetEnumerator()

    Span1.InnerText = "The items in the rows of the table are: "

    ' Iterate through the IEnumerator and display its contents.
    While myEnum.MoveNext()
         
      current_row = CType(myEnum.Current, HtmlTableRow)
      Span1.InnerText = Span1.InnerText & " " & current_row.Cells(0).InnerText & _
                        " " & current_row.Cells(1).InnerText

    End While

  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" runat="server" 
       style="border-width: 1; border-color: Black">

         <tr>
            <td>
               Cell 1
            </td>
            <td>
               Cell 2
            </td>
         </tr>
         <tr>
            <td>
               Cell 3
            </td>
            <td>
               Cell 4
            </td>
         </tr>

      </table>

      <br /><br />
  
      <input type="button" 
             value="Display row contents in the table"
             onserverclick="Button_Click" 
             runat="server"/>

      <br /><br />

      <span id="Span1"
            runat="server"/>

   </form>

</body>
</html>

注解

使用此方法创建一个 System.Collections.IEnumerator实现的对象,该对象可以循环访问以获取集合中的每个 HtmlTableRowCollection 项。

IEnumerator.Current使用 属性获取集合中的当前元素。

IEnumerator.MoveNext使用 方法移动到集合中的下一项。

IEnumerator.Reset使用 方法将枚举器移动到初始位置。

注意

必须在创建System.Collections.IEnumerator实现的对象后或使用 方法将枚举器移动到集合中的第一项之后IEnumerator.Reset调用 IEnumerator.MoveNext 方法。 否则,由 属性表示的 IEnumerator.Current 项未定义。

适用于

另请参阅