Control.EnsureChildControls 方法

定義

判斷伺服器控制項是否包含子控制項。 如果不包含,則建立子控制項。

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

範例

下列範例會 EnsureChildControls 使用 方法來確保目前的伺服器控制項具有子控制項。 然後,它會取得或設定 Text 目前伺服器控制項 ControlCollection 物件中子 TextBox Web 控制項的屬性。

重要

這個範例有一個可接受使用者輸入的文字方塊,這可能會造成安全性威脅。 根據預設,ASP.NET Web 網頁會驗證使用者輸入未包含指令碼或 HTML 項目。 如需詳細資訊,請參閱 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 目前值。 如果這個值為 false ,則會 CreateChildControls 呼叫 方法。

方法 EnsureChildControls 通常用於複合控制項,這些控制項是針對部分或所有功能使用子控制項的控制項。 系統會 EnsureChildControls 呼叫 方法,以確保已建立子控制項,並準備好處理輸入、執行資料系結或執行其他工作。

控制項 GridView 是複合控制項的範例。 它會建立子控制項,例如 TableTableRowTableCell Label 、 和 TextBox 控制項,用來呈現 產生的 HTML 資料表 GridView

在大部分情況下,自訂伺服器控制項開發人員不需要覆寫此方法。 如果您覆寫此方法,請使用與預設行為類似的方式。

適用於

另請參閱