确定服务器控件是否包含子控件。如果不包含,则创建子控件。
命名空间:System.Web.UI
程序集:System.Web(在 system.web.dll 中)
Protected Overridable Sub EnsureChildControls
protected virtual void EnsureChildControls ()
protected:
virtual void EnsureChildControls ()
protected void EnsureChildControls ()
protected function EnsureChildControls ()
该方法首先检查 ChildControlsCreated 属性的当前值。如果此值为假,则调用 CreateChildControls 方法。
当 ASP.NET 需要确保已创建子控件时,它将调用该方法。大多数情况下,自定义服务器控件的开发人员无需重写此方法。如果确实重写了此方法,请按与其默认行为相似的方式来使用。
下面的示例使用 EnsureChildControls 方法确保当前服务器控件具有子控件。然后它获取或设置当前服务器控件的 ControlCollection 对象中子 TextBoxWeb 控件的 Text 属性。
' 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
// 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.
/** @property
*/
public int get_Value()
{
this.EnsureChildControls();
return Int32.Parse(((TextBox)(get_Controls().get_Item(1))).get_Text());
} //get_Value
/** @property
*/
public void set_Value(int value)
{
this.EnsureChildControls();
((TextBox)get_Controls().get_Item(1)).set_Text(((Int32)value).ToString());
} //set_Value
// 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();
}
Windows 98、Windows 2000 SP4、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition
.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求。
.NET Framework
受以下版本支持:2.0、1.1、1.0