TableCellCollection.Clear 方法

定义

TableCell 中移除所有 TableCellCollection 对象。

public:
 virtual void Clear();
public void Clear ();
abstract member Clear : unit -> unit
override this.Clear : unit -> unit
Public Sub Clear ()

实现

示例

以下示例演示如何使用 Clear 方法从表的中心行中删除所有 TableCell 对象,并将其替换为新 TableCell 对象。

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

    int numrows = 5;
    int numcells = 6;
    int counter = 1;
 
    private void Page_Load(Object sender, EventArgs e) 
    {
        // Create a table.
        for (int rowNum = 0; rowNum < numrows; rowNum++) 
        {          
            TableRow rw = new TableRow();
            for (int cellNum=0; cellNum<numcells; cellNum++) 
            {
                TableCell cel = new TableCell();
                cel.Text=counter.ToString();
                counter++;
                rw.Cells.Add(cel);
            }
            Table1.Rows.Add(rw);
        }
    }

    private void Button_Click(Object sender, EventArgs e)
    {
        Table1.Rows[2].Cells.Clear();
        for (int cellNum = 0; cellNum < numcells; cellNum++) 
        {
            TableCell cel = new TableCell();
            cel.Text="***";
            Table1.Rows[2].Cells.Add(cel); 
        }
    }

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>TableCellCollection Example</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

    <h3>TableCellCollection Example</h3>
    <asp:Table id="Table1" runat="server" />
 
    <asp:Button id="Button1"
         Text="Replace Row 3 With ***"
         OnClick="Button_Click"
         runat="server" />

    </div>
    </form>
</body>
</html>
<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

    Dim numrows As Integer = 5
    Dim numcells As Integer = 6
    Dim counter As Integer = 1

    Sub Page_Load(sender As Object, e As EventArgs)
        ' Create a table.
        Dim rowNum As Integer
        For rowNum = 0 To numrows - 1
            Dim rw As New TableRow()
            Dim cellNum As Integer
            For cellNum = 0 To numcells - 1
                Dim cel As New TableCell()
                cel.Text = counter.ToString()
                counter += 1
                rw.Cells.Add(cel)
            Next
            Table1.Rows.Add(rw)
        Next
    End Sub         

    Sub Button_Click(sender As Object, e As EventArgs)
        Table1.Rows(2).Cells.Clear()
        Dim cellNum As Integer
        For cellNum = 0 To numcells - 1
            Dim cel As New TableCell()
            cel.Text = "***"
            Table1.Rows(2).Cells.Add(cel)
        Next
    End Sub

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>TableCellCollection Example</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

    <h3>TableCellCollection Example</h3>
    <asp:Table id="Table1" runat="server" />
    <p>&nbsp;</p>
    <asp:Button id="Button1"
         Text="Replace Row 3 With ***"
         OnClick="Button_Click"
         runat="server" />

    </div>
    </form>
</body>
</html>

注解

使用此方法从 中删除所有 TableCell 对象,TableCellCollectionCount并将 属性设置为 0。

注意

此方法从 中删除 TableCell 对象,但不会从 Table 控件中删除 TableRowTableCellCollection 如果要从表中完全删除行, TableRow 请务必同时删除 。

适用于

另请参阅