Question: When iterating through a collection of all controls on a page (from `Page.Controls` and those control's children and their children's children etc), how can you tell if the control came from the Page's Master Page?

The following seems to work, but feels a bit dirty. Is there a better way to get this information?

Update: Sorry, missed some code out earlier.

```
List<Control> allControls = GetAllControls(this.Page)
foreach (Control c in allControls)
{
       bool isFromMaster = c.NamingContainer.TemplateControl.GetType().BaseType.BaseType == typeof(MasterPage);
}

```

Where `GetAllControls` recursively gets all controls on a page

Thanks

Answer: 
Given a reference to a `Control`, you could recursively look at the `Parent` property:

```
bool IsFromMasterPage(Control control)
{
    while(control.Parent != null)
    {
        if (control.Parent is MasterPage) return true;
        control = control.Parent;
    }
    return false;
}

```
The `Page.Controls` **only contain controls from the current page**

If you want to check the MasterPage controls, use:

```
this.Master.Controls

```

On the other hand if you want to find the MasterPage controls on your page:

```
IEnumerable<MasterPage> masterPageControls = this.Page.Controls.OfType<MasterPage>();

```

Although you can have only one MasterPage associated to your page