共用方式為


Control.Parent 屬性

定義

在頁面控制階層中,會取得伺服器控制項的父控制權的參考。

public:
 virtual property System::Web::UI::Control ^ Parent { System::Web::UI::Control ^ get(); };
[System.ComponentModel.Browsable(false)]
public virtual System.Web.UI.Control Parent { get; }
[System.ComponentModel.Browsable(false)]
[System.ComponentModel.Bindable(false)]
public virtual System.Web.UI.Control Parent { get; }
[<System.ComponentModel.Browsable(false)>]
member this.Parent : System.Web.UI.Control
[<System.ComponentModel.Browsable(false)>]
[<System.ComponentModel.Bindable(false)>]
member this.Parent : System.Web.UI.Control
Public Overridable ReadOnly Property Parent As Control

屬性值

這是對伺服器控制的父控的參考。

屬性

範例

以下範例將頁面 上的新 Control 物件 myControl1,設定為方法呼叫中 FindControl 指定的控制項。 若呼叫回傳控制項,程式碼會利用該 Parent 屬性來識別包含 myControl1的控制項。 若父控制項存在,字串「文字框的父是」會與 ID 父控制項的屬性串接,並寫入 Page。 若找不到父控制項,則寫入字串「Control not found」。

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");
      }
}

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)
Else
   Response.Write("Control not found.....")
End If
End Sub

備註

每當頁面被請求時,該頁面會建立伺服器控制的階層結構。 這個特性讓你能判斷該階層中目前伺服器控制的父控制,並針對它進行程式設計。

適用於