Control.CreateChildControls Control.CreateChildControls Control.CreateChildControls Control.CreateChildControls Method

定义

由 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 网页验证用户输入是否不包含脚本或 HTML 元素。By default, ASP.NET Web pages validate that user input does not include script or HTML elements. 有关详细信息,请参阅脚本侵入概述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.

适用于

另请参阅