Control.EnsureChildControls Control.EnsureChildControls Control.EnsureChildControls Control.EnsureChildControls Method

定义

确定服务器控件是否包含子控件。Determines whether the server control contains child controls. 如果不包含,则创建子控件。If it does not, it creates child controls.

protected:
 virtual void EnsureChildControls();
protected virtual void EnsureChildControls ();
abstract member EnsureChildControls : unit -> unit
override this.EnsureChildControls : unit -> unit
Protected Overridable Sub EnsureChildControls ()

示例

下面的示例使用EnsureChildControls方法确保当前服务器控件具有子控件。The following example uses the EnsureChildControls method to ensure that the current server control has child controls. 然后,它获取或设置Text当前服务器控件的TextBox ControlCollection对象中的子 Web 控件的属性。It then gets or sets a Text property for a child TextBox Web control in the current server control's ControlCollection object.

重要

此示例具有一个接受用户输入的文本框,这是一个潜在的安全威胁。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.

// Ensure the current control has children,
// then get or set the Text property.
 public int Value {
    get {
        this.EnsureChildControls();
        return Int32.Parse(((TextBox)Controls[1]).Text);
    }
    set {
        this.EnsureChildControls();
        ((TextBox)Controls[1]).Text = value.ToString();
    }
 }

' Ensure the current control has children,
' then get or set the Text property.

Public Property Value() As Integer
   Get
      Me.EnsureChildControls()
      Return Int32.Parse(CType(Controls(1), TextBox).Text)
   End Get
   Set
      Me.EnsureChildControls()
      CType(Controls(1), TextBox).Text = value.ToString()
   End Set
End Property


注解

此方法首先检查ChildControlsCreated属性的当前值。This method first checks the current value of the ChildControlsCreated property. 如果此值为false CreateChildControls ,则调用方法。If this value is false, the CreateChildControls method is called.

EnsureChildControls方法通常用在复合控件中,复合控件是使用子控件作为部分或全部功能的控件。The EnsureChildControls method is typically used in composite controls, which are controls that use child controls for some or all their functionality. 调用EnsureChildControls方法是为了确保已创建子控件并准备好处理输入、执行数据绑定或执行其他任务。The EnsureChildControls method is called in order to make sure that child controls have been created and are ready to process input, to perform data binding, or to perform other tasks.

GridView控件是复合控件的一个示例。The GridView control is an example of a composite control. Table创建TableRow GridView 、 、Label、和控件TextBox等子控件,这些控件用于呈现生成的 HTML 表。 TableCellIt creates child controls such as Table, TableRow, TableCell, Label, and TextBox controls, which are used to render the HTML table that the GridView generates.

在大多数情况下,自定义服务器控件开发人员不必重写此方法。In most cases, custom server control developers do not have to override this method. 如果确实要重写此方法,请使用类似于默认行为的方式。If you do override this method, use it in a way similar to the default behavior.

适用于

另请参阅