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 重写版本。 在此实现中,复合控件显示一个包含在呈现 HTML 的两个 TextBox 文本控件中的控件。

重要

此示例具有一个接受用户输入的文本框,这是一个潜在的安全威胁。 默认情况下,ASP.NET 网页验证用户输入是否不包含脚本或 HTML 元素。 有关详细信息,请参阅脚本侵入概述

// 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 服务器控件

适用于

另请参阅