Control.Parent 屬性


在網頁控制階層架構中取得伺服器控制項之父控制項的參考。Gets a reference to the server control's parent control in the page control hierarchy.

 virtual property System::Web::UI::Control ^ Parent { System::Web::UI::Control ^ get(); };
public virtual System.Web.UI.Control Parent { get; }
public virtual System.Web.UI.Control Parent { get; }
member this.Parent : System.Web.UI.Control
member this.Parent : System.Web.UI.Control
Public Overridable ReadOnly Property Parent As Control



伺服器控制項父控制項的參考。A reference to the server control's parent control.



下列範例會將 Control 頁面上的新物件設定 myControl1 為方法呼叫中所指定的控制項 FindControlThe following example sets a new Control object on a page, myControl1, to the control specified in a FindControl method call. 如果呼叫傳回控制項,則程式碼會使用 Parent 屬性來識別包含的控制項 myControl1If the call returns a control, the code uses the Parent property to identify the control that contains myControl1. 如果父控制項存在,則字串「文字方塊的父系是」會與 ID 父控制項的屬性串連,並寫入至 PageIf the parent control exists, the string "The parent of the text box is" is concatenated with the ID property of the parent control and written to the Page. 如果找不到父控制項,則會寫入字串「找不到控制項」。If no parent control is found, the string "Control not found" is written.

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

Private Sub Button1_Click(sender As Object, MyEventArgs As EventArgs)
' Find control on page.
Dim myControl1 As Control = FindControl("TextBox2")
If (Not myControl1 Is Nothing)
   ' Get control's parent.
   Dim myControl2 As Control = myControl1.Parent
   Response.Write("Parent of the text box is : " & myControl2.ID)
   Response.Write("Control not found.....")
End If
End Sub


每當要求頁面時,就會建立該頁面上的伺服器控制項階層。Whenever a page is requested, a hierarchy of server controls on that page is built. 這個屬性可讓您判斷該階層中目前伺服器控制項的父控制項,並對其進行程式設計。This property allows you to determine the parent control of the current server control in that hierarchy, and to program against it.