This documentation is archived and is not being maintained.

Control.EnsureChildControls Method

Determines whether the server control contains child controls. If it does not, it creates child controls.

[Visual Basic]
Protected Overridable Sub EnsureChildControls()
[C#]
protected virtual void EnsureChildControls();
[C++]
protected: virtual void EnsureChildControls();
[JScript]
protected function EnsureChildControls();

Remarks

This method first checks the current value of the ChildControlsCreated property. If this value is false, the CreateChildControls method is called.

ASP.NET calls it this method when it needs to make sure that child controls have been created. In most cases, custom server control developers do not need to override this method. If you do override this method, use it in a similar fashion as its default behavior.

Example

The following example uses the EnsureChildControls method to ensure that the current server control has child controls. It then gets or sets a Text property for a child TextBox Web control in the current server control's ControlCollection object.

[Visual Basic] 
' 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



[C#] 
// 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();
    }
 }


[C++] 
// Ensure the current control has children,
// then get or set the Text property.
public:
__property int get_Value() 
{
   this->EnsureChildControls();
   return Int32::Parse(dynamic_cast<TextBox*>(Controls->Item[1])->Text);
}
__property void set_Value(int value)
{
   this->EnsureChildControls();
   dynamic_cast<TextBox*>(Controls->Item[1])->Text = value.ToString();
}

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

 public function set Value(value : int)
 {
        this.EnsureChildControls();
        TextBox(Controls[1]).Text = value.ToString();
 }

Requirements

Platforms: Windows 2000, Windows XP Professional, Windows Server 2003 family

See Also

Control Class | Control Members | System.Web.UI Namespace | Controls | ChildControlsCreated | CreateChildControls

Show: