Control.Parent 属性

定义

获取对页 UI 层次结构中服务器控件的父控件的引用。Gets a reference to the server control's parent control in the page control hierarchy.

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.Bindable(false)]
[System.ComponentModel.Browsable(false)]
public virtual System.Web.UI.Control Parent { get; }
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)设置为 FindControl 方法调用中指定的控件。The following example sets a new Control object on a page, myControl1, to the control specified in a FindControl method call. 如果调用返回了一个控件,则该代码使用 Parent 属性标识包含 myControl1的控件。If 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");
      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

注解

每次请求页面时,将生成该页面上的服务器控件的层次结构。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.

适用于