Share via


ControlCollection.Add(Control) Método

Definición

Agrega a la colección el objeto Control especificado.

public:
 virtual void Add(System::Web::UI::Control ^ child);
public virtual void Add (System.Web.UI.Control child);
abstract member Add : System.Web.UI.Control -> unit
override this.Add : System.Web.UI.Control -> unit
Public Overridable Sub Add (child As Control)

Parámetros

child
Control

Control que se agrega a la colección.

Excepciones

El parámetro child no especifica un control.

ControlCollection es de solo lectura.

Ejemplos

En el ejemplo de código siguiente se usa el Add método para agregar una serie de elementos de plantilla, el número de los cuales se toman del estado de vista del control de servidor, a un control con plantilla personalizado.

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

Comentarios

El nuevo control se agrega al final de una matriz de índice ordinal. El control puede ser una instancia de cualquier control de servidor ASP.NET, un control de servidor personalizado que cree o un control literal.

Para agregar un control a la colección en una ubicación de índice específica, use el AddAt método .

Se aplica a

Consulte también