HtmlTableCellCollection.GetEnumerator 方法

定义

返回一个实现了 IEnumerator 的对象,其中包含 HtmlTableCell 集合内的所有 HtmlTableCellCollection 对象。Returns a IEnumerator-implemented object that contains all HtmlTableCell objects in the HtmlTableCellCollection collection.

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 的对象,其中包含 HtmlTableCell 中的所有 HtmlTableCellCollection 对象。A IEnumerator-implemented object that contains all HtmlTableCell objects in the HtmlTableCellCollection.

实现

示例

下面的代码示例演示如何使用GetEnumerator方法创建实现了System.Collections.IEnumerator的对象。The following code example demonstrates how to use the GetEnumerator method to create a System.Collections.IEnumerator-implemented object. 然后循环访问该对象以显示HtmlTableCellCollection集合的内容。The object is then iterated through to display the contents of the HtmlTableCellCollection collection.

<%@ 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)
  {

    HtmlTableCell current_cell;

    // Create an IEnumerator instance.
    IEnumerator myEnum = Table1.Rows[0].Cells.GetEnumerator();

    Span1.InnerText = "The items in the first row are: ";

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

      current_cell = (HtmlTableCell)myEnum.Current;
      Span1.InnerText = Span1.InnerText + " " + current_cell.InnerText;

    }

  }

</script>

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

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

      <h3>HtmlTableCellCollection Example</h3>

      <table id="Table1" 
             style="border-width:1; border-color:Black"
             runat="server">

         <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 the contents of the first row"
             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_cell As HtmlTableCell

    ' Create an IEnumerator instance.
    Dim myEnum As IEnumerator = Table1.Rows(0).Cells.GetEnumerator()

    Span1.InnerText = "The items in the first row are: "

    ' Iterate through the IEnumerator and display its contents.
    While myEnum.MoveNext()
         
      current_cell = CType(myEnum.Current, HtmlTableCell)
      Span1.InnerText = Span1.InnerText & " " & current_cell.InnerText

    End While

  End Sub

</script>

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

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

      <h3>HtmlTableCellCollection Example</h3>

      <table id="Table1" 
             style="border-width:1; border-color:Black"
             runat="server">

         <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 the contents of the first row"
             onserverclick="Button_Click" 
             runat="server"/>

      <br /><br />

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

   </form>

</body>
</html>

注解

使用此方法创建一个实现System.Collections.IEnumerator了的对象, 该对象可循环访问以检索HtmlTableCellCollection集合中每个项的内容。Use this method to create a System.Collections.IEnumerator-implemented object that can be iterated through to retrieve the contents of each item in the HtmlTableCellCollection collection.

IEnumerator.Current使用属性可获取集合中当前指向的项。Use the IEnumerator.Current property to get the item currently pointed to in the collection.

IEnumerator.MoveNext使用方法移动到集合中的下一项。Use the IEnumerator.MoveNext method to move to the next item in the collection.

IEnumerator.Reset使用方法可将枚举数移至初始位置。Use the IEnumerator.Reset method to move the enumerator to the initial position.

备注

在创建实现的对象之后或使用IEnumerator.Reset方法将枚举器移动到集合中的第一项之后, 必须调用方法。IEnumerator.MoveNext System.Collections.IEnumeratorThe IEnumerator.MoveNext method must be called after creating the System.Collections.IEnumerator-implemented object, or after using the IEnumerator.Reset method to move the enumerator to the first item in the collection. 否则, 由IEnumerator.Current属性表示的项是不确定的。Otherwise, the item represented by the IEnumerator.Current property is undefined.

适用于

另请参阅