次の方法で共有


ControlCollection.Clear メソッド

現在のサーバー コントロールの ControlCollection オブジェクトからすべてのコントロールを削除します。

名前空間: System.Web.UI
アセンブリ: System.Web (system.web.dll 内)

構文

'宣言
Public Overridable Sub Clear
'使用
Dim instance As ControlCollection

instance.Clear
public virtual void Clear ()
public:
virtual void Clear ()
public void Clear ()
public function Clear ()
適用できません。

解説

Control.CreateChildControls メソッドおよび DataBind メソッドをオーバーライドする場合は、このメソッドを使用してカスタム コントロールの ControlCollection を空にします。template 宣言された複合コントロール、または template 宣言されたデータ バインド コントロールを開発する場合にこの操作を行います。

使用例

Control.CreateChildControls メソッドをオーバーライドし、Clear メソッドを使用して、以前 ControlCollection オブジェクト内にあった子コントロールをすべて削除する方法を次のコード例に示します。この例の場合は、コントロールの ControlCollection にある古いオブジェクトが誤って表示されないように、この操作を実行する必要があります。

' Override to create repeated items.
Protected Overrides Sub CreateChildControls()
    Dim O As Object = ViewState("NumItems")
    If Not (O Is Nothing)
       ' Clear any existing child controls.
       Controls.Clear()

       Dim I As Integer
       Dim NumItems As Integer = CInt(O)
       For I = 0 To NumItems - 1
          ' Create an item.
          Dim Item As RepeaterItemVB = New RepeaterItemVB(I, Nothing)
          ' Initialize the item from the template.
          ItemTemplate.InstantiateIn(Item)
          ' Add the item to the ControlCollection.
          Controls.Add(Item)
       Next
    End If
End Sub
// Override to create repeated items.
protected override void CreateChildControls() {
    object o = ViewState["NumItems"];
    if (o != null) {
       // Clear any existing child controls.
       Controls.Clear();

       int numItems = (int)o;
       for (int i=0; i < numItems; i++) {
          // Create an item.
          RepeaterItem item = new RepeaterItem(i, null);
          // Initialize the item from the template.
          ItemTemplate.InstantiateIn(item);
          // Add the item to the ControlCollection.
          Controls.Add(item);
       }
    }
}
// Override to create repeated items.
protected void CreateChildControls()
{
    Object o = get_ViewState().get_Item("NumItems");
    if (o != null) {
        // Clear any existing child controls.
        get_Controls().Clear();

        int numItems = Convert.ToInt32(o);
        for (int i = 0; i < numItems; i++) {
            // Create an item.
            RepeaterItem item = new RepeaterItem(i, null);
            // Initialize the item from the template.
            get_ItemTemplate().InstantiateIn(item);
            // Add the item to the ControlCollection.
            get_Controls().Add(item);
        }
    }
} //CreateChildControls

プラットフォーム

Windows 98,Windows Server 2000 SP4,Windows CE,Windows Millennium Edition,Windows Mobile for Pocket PC,Windows Mobile for Smartphone,Windows Server 2003,Windows XP Media Center Edition,Windows XP Professional x64 Edition,Windows XP SP2,Windows XP Starter Edition

Microsoft .NET Framework 3.0 は Windows Vista,Microsoft Windows XP SP2,および Windows Server 2003 SP1 でサポートされています。

バージョン情報

.NET Framework

サポート対象 : 3.0,2.0,1.1,1.0

参照

関連項目

ControlCollection クラス
ControlCollection メンバ
System.Web.UI 名前空間
Control.Controls プロパティ