EmptyControlCollection Class (System.Web.UI)

Switch View :
ScriptFree
.NET Framework Class Library
EmptyControlCollection Class

Provides standard support for a ControlCollection collection that is always empty.

Inheritance Hierarchy

System.Object
  System.Web.UI.ControlCollection
    System.Web.UI.EmptyControlCollection

Namespace:  System.Web.UI
Assembly:  System.Web (in System.Web.dll)
Syntax

Visual Basic
Public Class EmptyControlCollection _
	Inherits ControlCollection
C#
public class EmptyControlCollection : ControlCollection
Visual C++
public ref class EmptyControlCollection : public ControlCollection
F#
type EmptyControlCollection =  
    class
        inherit ControlCollection
    end

The EmptyControlCollection type exposes the following members.

Constructors

  Name Description
Public method EmptyControlCollection Initializes a new instance of the EmptyControlCollection class.
Top
Properties

  Name Description
Public property Count Gets the number of server controls in the ControlCollection object for the specified ASP.NET server control. (Inherited from ControlCollection.)
Public property IsReadOnly Gets a value indicating whether the ControlCollection object is read-only. (Inherited from ControlCollection.)
Public property IsSynchronized Gets a value indicating whether the ControlCollection object is synchronized. (Inherited from ControlCollection.)
Public property Item Gets a reference to the server control at the specified index location in the ControlCollection object. (Inherited from ControlCollection.)
Protected property Owner Gets the ASP.NET server control to which the ControlCollection object belongs. (Inherited from ControlCollection.)
Public property SyncRoot Gets an object that can be used to synchronize access to the collection of controls. (Inherited from ControlCollection.)
Top
Methods

  Name Description
Public method Add Denies the addition of the specified Control object to the collection. (Overrides ControlCollection.Add(Control).)
Public method AddAt Denies the addition of the specified Control object to the collection, at the specified index position. (Overrides ControlCollection.AddAt(Int32, Control).)
Public method Clear Removes all controls from the current server control's ControlCollection object. (Inherited from ControlCollection.)
Public method Contains Determines whether the specified server control is in the parent server control's ControlCollection object. (Inherited from ControlCollection.)
Public method CopyTo Copies the child controls stored in the ControlCollection object to an System.Array object, beginning at the specified index location in the System.Array. (Inherited from ControlCollection.)
Public method Equals(Object) Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected method Finalize Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public method GetEnumerator Retrieves an enumerator that can iterate through the ControlCollection object. (Inherited from ControlCollection.)
Public method GetHashCode Serves as a hash function for a particular type. (Inherited from Object.)
Public method GetType Gets the Type of the current instance. (Inherited from Object.)
Public method IndexOf Retrieves the index of a specified Control object in the collection. (Inherited from ControlCollection.)
Protected method MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public method Remove Removes the specified server control from the parent server control's ControlCollection object. (Inherited from ControlCollection.)
Public method RemoveAt Removes a child control, at the specified index location, from the ControlCollection object. (Inherited from ControlCollection.)
Public method ToString Returns a string that represents the current object. (Inherited from Object.)
Top
Remarks

This class is used when you want to define a custom control that does not allow child controls.

Examples

Visual Basic


' File name: emptyControlCollection.vb.

Imports System
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Collections


Namespace CustomControls 

  Public Class MyVB_EmptyControl 
    Inherits Control

    <System.Security.Permissions.PermissionSetAttribute(System.Security.Permissions.SecurityAction.Demand, Name:="FullTrust")> _
    Protected Overrides Function CreateControlCollection() As ControlCollection
    ' Function Name: CreateControlCollection.
    ' Denies the creation of any child control by creating an empty collection.
    ' Generates an exception if an attempt to create a child control is made.
      Return New EmptyControlCollection(Me)
    End Function 

    <System.Security.Permissions.PermissionSetAttribute(System.Security.Permissions.SecurityAction.Demand, Name:="FullTrust")> _  
    Protected Overrides Sub CreateChildControls()
    ' Sub Name: CreateChildControls.
    ' Populates the child control collection (Controls). 
    ' Note: This function will cause an exception because the control does not allow 
    ' child controls.
      Dim text As LiteralControl
      text = New LiteralControl("<h5>Composite Controls</h5>")
      Controls.Add(text)
    End Sub 
  End Class 

End Namespace 



C#


/* File name: emptyControlCollection.cs. */

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;

namespace CustomControls
{

  // Defines a simple custom control.
  public class MyCS_EmptyControl : Control
  {
    [System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name="FullTrust")] 
    protected override ControlCollection CreateControlCollection() 
    /*
     * Function Name: CreateControlCollection.
     * Denies the creation of any child control by creating an empty collection.
     * Generates an exception if an attempt to create a child control is made.
     */
     {
       return new EmptyControlCollection(this);
     }

     [System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name="FullTrust")] 
     protected override void CreateChildControls()
     /*
      * Function Name: CreateChildControls.
      * Populates the child control collection (Controls). 
      * Note: This function will cause an exception because the control does not allow 
      * child controls.
      */
      {
        // Create a literal control to contain the header and add it to the collection.
        LiteralControl text;
        text = new LiteralControl("<h5>Composite Controls</h5>");
        Controls.Add(text);
      }
   }

}


Version Information

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0
Platforms

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
See Also

Reference