Control.Parent 屬性

定義

在網頁控制階層架構中取得伺服器控制項之父控制項的參考。

C#
[System.ComponentModel.Browsable(false)]
public virtual System.Web.UI.Control Parent { get; }
C#
[System.ComponentModel.Browsable(false)]
[System.ComponentModel.Bindable(false)]
public virtual System.Web.UI.Control Parent { get; }

屬性值

Control

伺服器控制項父控制項的參考。

屬性

範例

下列範例會將頁面上 myControl1 的新 Control 物件設定為方法呼叫中指定的 FindControl 控制項。 如果呼叫傳回控制項,程式碼會使用 Parent 屬性來識別包含 的 myControl1 控制項。 如果父控制項存在,字串 「The parent of the text box is」 會與 ID 父控制項的 屬性串連,並寫入至 Page 。 如果找不到父控制項,則會寫入字串「找不到控制項」。

C#
private void Button1_Click(object sender, EventArgs MyEventArgs)
{
      // Find control on page.
      Control myControl1 = FindControl("TextBox2");
      if(myControl1!=null)
      {
         // Get control's parent.
         Control myControl2 = myControl1.Parent;
         Response.Write("Parent of the text box is : " + myControl2.ID);
      }
      else
      {
         Response.Write("Control not found");
      }
}

備註

每當要求頁面時,就會建置該頁面上的伺服器控制項階層。 這個屬性可讓您判斷該階層中目前伺服器控制項的父控制項,並對其進行程式設計。

適用於

產品 版本
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8