This topic has not yet been rated - Rate this topic

Control.Parent Property

Gets a reference to the server control's parent control in the page control hierarchy.

Namespace:  System.Web.UI
Assembly:  System.Web (in System.Web.dll)
[BrowsableAttribute(false)]
[BindableAttribute(false)]
public virtual Control Parent { get; }

Property Value

Type: System.Web.UI.Control
A reference to the server control's parent control.

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.

The following example sets a new Control object on a page, myControl1, to the control specified in a FindControl method call. If the call returns a control, the code uses the Parent property to identify the control that contains myControl1. If 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");
      }
}


.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Mistake in Example
The example description states "If no parent control is found, the string "Control not found" is written." but this is not what happens in the example. Instead, if the control of which the parent property should be used is not found, the string "Control not found" is written.

However, I think it would be better to modify the example instead:
// Find control on page.
      Control myControl1 = FindControl("TextBox2");
      if(myControl1!=null)
      {
         // Get control's parent.
         Control myControl2 = myControl1.Parent;
if (myControl2 != null) { Response.Write("Parent of the text box is : " + myControl2.ID); } else { Response.Write("Parent not found"); }
}
else { Response.Write("Control not found"); }