HtmlTableCellCollection.GetEnumerator 方法

定義

傳回 IEnumerator 實作的物件,其中包含 HtmlTableCell 集合中所有的 HtmlTableCellCollection 物件。

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

IEnumerator 實作的物件,包含 HtmlTableCell 中所有的 HtmlTableCellCollection 物件。

實作

範例

下列程式碼範例示範如何使用 GetEnumerator 方法來建立實作 System.Collections.IEnumerator 的物件。 然後,物件會逐一查看以顯示集合的內容 HtmlTableCellCollection

<%@ 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 每個專案的內容。

IEnumerator.Current使用 屬性可取得集合中目前指向的專案。

IEnumerator.MoveNext使用 方法可移至集合中的下一個專案。

使用 方法, IEnumerator.Reset 將列舉值移至初始位置。

注意

IEnumerator.MoveNext在建立實作 System.Collections.IEnumerator 的物件之後,或使用 方法將列舉值移至集合中的第一個專案之後 IEnumerator.Reset ,必須呼叫 方法。 否則,屬性所 IEnumerator.Current 代表的專案是未定義的。

適用於

另請參閱