Control.CreateControlCollection Metode

Definisi

Membuat objek baru ControlCollection untuk menahan kontrol anak (baik harfiah maupun server) kontrol server.

protected:
 virtual System::Web::UI::ControlCollection ^ CreateControlCollection();
protected virtual System.Web.UI.ControlCollection CreateControlCollection();
abstract member CreateControlCollection : unit -> System.Web.UI.ControlCollection
override this.CreateControlCollection : unit -> System.Web.UI.ControlCollection
Protected Overridable Function CreateControlCollection () As ControlCollection

Mengembalikan

Objek ControlCollection untuk memuat kontrol server turunan kontrol server saat ini.

Contoh

Contoh kode berikut mengambil alih CreateControlCollection metode untuk membuat instans CustomControlCollection kelas, yang mewarisi dari ControlCollection kelas.

// Override the CreateControlCollection method to 
// write to the Trace object when tracing is enabled
// for the page or application in which this control
// is included.   
protected override ControlCollection CreateControlCollection()
{
    return new CustomControlCollection(this);
}
' Override the CreateControlCollection method to 
' write to the Trace object when tracing is enabled
' for the page or application in which this control
' is included.   
Protected Overrides Function CreateControlCollection() As ControlCollection
    Return New CustomControlCollection(Me)
End Function

Contoh kode berikut menggunakan CreateControlCollection metode dalam penimpaan kontrol server kustom metode CreateChildControls . Koleksi baru dibuat, lalu diisi dengan dua kontrol anak, firstControl dan secondControl.


protected override void CreateChildControls()
{               
   // Creates a new ControlCollection. 
   this.CreateControlCollection();

   // Create child controls.
    ChildControl firstControl = new ChildControl();
   firstControl.Message = "FirstChildControl";

   ChildControl secondControl = new ChildControl();
   secondControl.Message = "SecondChildControl";
   
   Controls.Add(firstControl);
   Controls.Add(secondControl);

   // Prevent child controls from being created again.
   ChildControlsCreated = true;
}

Protected Overrides Sub CreateChildControls()
   ' Creates a new ControlCollection. 
   Me.CreateControlCollection()
   
   ' Create child controls.
   Dim firstControl As New ChildControl()
   firstControl.Message = "FirstChildControl"
   
   Dim secondControl As New ChildControl()
   secondControl.Message = "SecondChildControl"
   
   Controls.Add(firstControl)
   Controls.Add(secondControl)
   
   ' Prevent child controls from being created again.
   ChildControlsCreated = True
End Sub


Keterangan

Ambil alih metode ini dalam kontrol server kustom jika Anda telah membuat objek koleksi yang berasal dari ControlCollection kelas . Anda kemudian dapat membuat instans kelas koleksi tersebut dalam penimpaan metode ini.

Berlaku untuk

Lihat juga