Freigeben über


EmptyControlCollection-Konstruktor

Initialisiert eine neue Instanz der EmptyControlCollection-Klasse.

Namespace: System.Web.UI
Assembly: System.Web (in system.web.dll)

Syntax

'Declaration
Public Sub New ( _
    owner As Control _
)
'Usage
Dim owner As Control

Dim instance As New EmptyControlCollection(owner)
public EmptyControlCollection (
    Control owner
)
public:
EmptyControlCollection (
    Control^ owner
)
public EmptyControlCollection (
    Control owner
)
public function EmptyControlCollection (
    owner : Control
)

Parameter

  • owner
    Das Control, das diese Auflistung als Auflistung von untergeordneten Steuerelementen besitzt.

Beispiel

Im folgenden Codebeispiel wird versucht, ein Steuerelement mit untergeordneten Elementen zu füllen, wodurch eine Ausnahme ausgelöst wird. Das liegt daran, dass das Containersteuerelement keine untergeordneten Steuerelemente zulässt. Die Befehlszeile zum Erstellen der ausführbaren Datei lautet wie folgt:

vbc /r:System.dll /r:System.Web.dll /t:library
       /out:myWebAppPath/bin/vb_myEmptyControlCollection.dll
       myEmptyControlCollection.vb
csc /t:library /out:myWebAppPath/bin/cs_myEmptyControlCollection.dll
    myEmptyControlCollection.cs
' 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 
/* 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);
      }
   }

}
/* File name: emptyControlCollection.jsl. */
import System.*;
import System.Web.*;
import System.Web.UI.*;
import System.Web.UI.WebControls.*;
import System.Collections.*;

// Defines a simple custom control.
public class MyJSL_EmptyControl extends Control
{
//    /** @attribute System.Security.Permissions.PermissionSet(
//        System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")
//     */
    protected 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);
    } //CreateControlCollection

    /** @attribute System.Security.Permissions.PermissionSet(
        System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")
     */
    protected 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>");
        get_Controls().Add(text);
    } //CreateChildControls
} //MyJSL_EmptyControl

Im folgenden Codebeispiel wird das oben erstellte leere benutzerdefinierte Steuerelement verwendet. Wenn Sie diesen Beispielcode ausführen, wird eine Ausnahme ausgelöst. Beachten Sie, dass die in der Register-Direktive dargestellten Werte die vorherige Befehlszeile widerspiegeln.

<%@ Register TagPrefix="custom" Assembly="vb_myEmptyControlCollection" Namespace="CustomControls" %>
 <html>
  <body>
  <h1>Using an Empty Control</h1>
  <custom:MyVB_EmptyControl id="vbEmptyControlId" runat="server"/>
  </body>
 </html>
<%@ Register TagPrefix="custom" Assembly="cs_myEmptyControlCollection" Namespace="CustomControls" %>
 <html>
  <body>
  <h1>Using an Empty Control </h1>
  <custom:MyCS_EmptyControl id="csEmptyControlId" runat="server"/>
  </body>
 </html>

Plattformen

Windows 98, Windows 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.

Versionsinformationen

.NET Framework

Unterstützt in: 2.0, 1.1, 1.0

Siehe auch

Referenz

EmptyControlCollection-Klasse
EmptyControlCollection-Member
System.Web.UI-Namespace