asp.net.ph

ControlCollection.Add Method

System.Web.UI Namespace   ControlCollection Class


Adds a specified control to a ControlCollection.

[ VB ]
Public Sub Add ( _
   ByVal child As Control _
) 

[ C# ]
public virtual void Add (
   Control child
);

[ C++ ]
public: virtual void Add (
   Control* child
);

[ JScript ]
public function Add (
   child : Control
) : void;

Parameters

child
The Control to add to the collection.

Exceptions


Exception Type Condition
ArgumentNullException Thrown if the child parameter does not specifiy a control.
HttpException Thrown if the ControlCollection object is read-only.

Remarks

Use this method to add the specified Control to the end of a ControlCollection. The control can be an instance of any ASP.NET server control, a literal control, or a custom server control that you create.

To insert a Control at a specific location in the collection, use the AddAt method.

Example

The following example uses the Add method to programmatically add a series of template items at run time, the number of which are taken from the server control's view state, to a custom templated control.

// 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 );
      }
   }
}
  C# VB JScript

See Also

ControlCollection Members   Controls.AddAt   Controls.Remove   Controls.RemoveAt   Controls.Clear Skip Navigation Links




Home
Suggested Reading


Previous page Back to top Next page

© 2000-2010 Rey Nuñez All rights reserved.

If you have any question, comment or suggestion
about this site, please send us a note

You can help support asp.net.ph