HtmlTableRowCollection.Clear Method

Removes all HtmlTableRow objects from the HtmlTableRowCollection collection.

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

public void Clear ()
public void Clear ()
public function Clear ()

Use this method to remove all HtmlTableRow objects from the HtmlTableRowCollection collection and reset the Count property to 0.

The following code example demonstrates how to use the Clear method to remove all items from the HtmlTableRowCollection collection. New entries are then added to the collection and displayed in the HtmlTable control.

<%@ Page Language="C#" AutoEventWireup="True" %>

<script runat="server">

  void Button_Click(Object sender, EventArgs e)

    // Clear the rows from the table.

    // Create a new table.
    // Iterate through the rows.
    for (int j = 0; j < 5; 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 < 5; 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 " +



   <title>HtmlTableRowCollection Example</title>

   <form runat="server">

      <h3>HtmlTableRowCollection Example</h3>

      <table id="Table1" 

               Cell 1
               Cell 2
               Cell 3
               Cell 4


      <input type="button" 
             value="Create New Table"



Windows 98, Windows 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0