Control.CreateChildControls 方法

定義

由 ASP.NET 網頁架構呼叫,通知使用組合實作的伺服器控制項來建立所包含的任何子控制項,以準備回傳或呈現。

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

範例

下列範例示範方法的 CreateChildControls 覆寫版本。 在此實作中,複合控制項會顯示以兩個 TextBox 常值控制項括住的控制項來呈現 HTML。

重要

這個範例有一個可接受使用者輸入的文字方塊,這可能會造成安全性威脅。 根據預設,ASP.NET Web 網頁會驗證使用者輸入未包含指令碼或 HTML 項目。 如需詳細資訊,請參閱 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

備註

當您開發複合或樣板化伺服器控制項時,必須覆寫這個方法。 覆寫 方法的 CreateChildControls 控制項應該實 INamingContainer 作 介面,以避免命名衝突。

如需詳細資訊,請參閱Web 服務器控制項範本開發自訂 ASP.NET 伺服器控制項

適用於

另請參閱