Control.CreateControlCollection Metod

Definition

Skapar ett nytt ControlCollection objekt som innehåller de underordnade kontrollerna (både literal och server) för serverkontrollen.

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

Returer

Ett ControlCollection objekt som ska innehålla den aktuella serverkontrollens underordnade serverkontroller.

Exempel

I följande kodexempel åsidosätts CreateControlCollection metoden för att skapa en instans av en CustomControlCollection klass, som ärver från ControlCollection klassen.

// 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

I följande kodexempel används CreateControlCollection metoden i en åsidosättning av en anpassad serverkontroll för CreateChildControls metoden. Den nya samlingen skapas och fylls sedan i med två underordnade kontroller firstControl och 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


Kommentarer

Åsidosätt den här metoden i en anpassad serverkontroll om du har skapat ett samlingsobjekt som härleds från ControlCollection klassen. Du kan sedan instansiera den samlingsklassen i åsidosättningen av den här metoden.

Gäller för

Se även