HtmlTableRowCollection.GetEnumerator Method ()

 

Returns an System.Collections.IEnumerator-implemented object that contains all HtmlTableRow objects in the HtmlTableRowCollection collection.

Namespace:   System.Web.UI.HtmlControls
Assembly:  System.Web (in System.Web.dll)

public IEnumerator GetEnumerator()

Return Value

Type: System.Collections.IEnumerator

An System.Collections.IEnumerator-implemented object that contains all HtmlTableRow objects in the HtmlTableRowCollection.

Use this method to create an System.Collections.IEnumerator-implemented object that can be iterated through to get each item in the HtmlTableRowCollection collection.

Use the IEnumerator.Current property to get the current element in the collection.

Use the IEnumerator.MoveNext method to move to the next item in the collection.

Use the IEnumerator.Reset method to move the enumerator to the initial position.

System_CAPS_noteNote

You must call the IEnumerator.MoveNext method after creating an System.Collections.IEnumerator-implemented object, or after using the IEnumerator.Reset method to move the enumerator to the first item in the collection. Otherwise, the item represented by the IEnumerator.Current property is undefined.

The following code example demonstrates how to use the GetEnumerator method to create an System.Collections.IEnumerator object. The System.Collections.IEnumerator object is then iterated through to display the contents of the HtmlTableRowCollection 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)
  {

    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>

.NET Framework
Available since 1.1
Return to top
Show: