Control.CreateChildControls 方法

定義

由 ASP.NET 網頁架構呼叫,通知使用組合實作的伺服器控制項來建立所包含的任何子控制項,以準備回傳或呈現。Called by the ASP.NET page framework to notify server controls that use composition-based implementation to create any child controls they contain in preparation for posting back or rendering.

protected public:
 virtual void CreateChildControls();
protected internal virtual void CreateChildControls ();
abstract member CreateChildControls : unit -> unit
override this.CreateChildControls : unit -> unit
Protected Friend Overridable Sub CreateChildControls ()

範例

下列範例示範 CreateChildControls 方法的覆寫版本。The following example demonstrates an overridden version of the CreateChildControls method. 在此執行中,複合控制項會顯示一個 TextBox 控制項,其中包含在呈現 HTML 的兩個常值控制項中。In this implementation, the composite control displays a TextBox control enclosed in two literal controls that render HTML.

重要

這個範例有一個可接受使用者輸入的文字方塊,這可能會造成安全性威脅。This example has a text box that accepts user input, which is a potential security threat. 根據預設,ASP.NET Web 網頁會驗證使用者輸入未包含指令碼或 HTML 項目。By default, ASP.NET Web pages validate that user input does not include script or HTML elements. 如需詳細資訊,請參閱 Script Exploits Overview (指令碼攻擊概觀)。For more information, see Script Exploits Overview.

// Override CreateChildControls to create the control tree.
 [System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name="Execution")]
 protected override void CreateChildControls() {

     // Add a LiteralControl to the current ControlCollection.
     this.Controls.Add(new LiteralControl("<h3>Value: "));


     // Create a text box control, set the default Text property, 
     // and add it to the ControlCollection.
     TextBox box = new TextBox();
     box.Text = "0";
     this.Controls.Add(box);

     this.Controls.Add(new LiteralControl("</h3>"));
 }

' Override CreateChildControls to create the control tree.
<System.Security.Permissions.PermissionSetAttribute(System.Security.Permissions.SecurityAction.Demand, Name:="Execution")> _
Protected Overrides Sub CreateChildControls()
   
   ' Add a LiteralControl to the current ControlCollection.
   Me.Controls.Add(New LiteralControl("<h3>Value: "))
   
   
   ' Create a text box control, set the default Text property, 
   ' and add it to the ControlCollection.
   Dim box As New TextBox()
   box.Text = "0"
   Me.Controls.Add(box)
   
   Me.Controls.Add(New LiteralControl("</h3>"))
End Sub

備註

當您開發複合或樣板化伺服器控制項時,必須覆寫此方法。When you develop a composite or templated server control, you must override this method. 覆寫 CreateChildControls 方法的控制項應該執行 INamingContainer 介面,以避免命名衝突。Controls that override the CreateChildControls method should implement the INamingContainer interface to avoid naming conflicts.

如需詳細資訊,請參閱Web 服務器控制項範本開發自訂 ASP.NET 伺服器控制項For more information, see Web Server Controls Templates and Developing Custom ASP.NET Server Controls.

適用於

另請參閱