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: 
Solution turned out to be going through the controls from the master page, excluding the children of content place holders (as these give you the controls added from the page itself).

```
public static bool IsFromMasterPage(Control control)
{
    if (control.Page.Master != null)
    {
        // Get all controls on the master page, excluding those from ContentPlaceHolders
        List<Control> masterPageControls = FindControlsExcludingPlaceHolderChildren(control.Page.Master);
        bool match = masterPageControls.Contains(control);
        return match;
    }
    return false;
}    

public static List<Control> FindControlsExcludingPlaceHolderChildren(Control parent)
{
    List<Control> controls = new List<Control>();
    foreach (Control control in parent.Controls)
    {
        controls.Add(control);
        if (control.HasControls() && control.GetType() != typeof(ContentPlaceHolder))
        {
            controls.AddRange(FindControlsExcludingPlaceHolderChildren(control));
        }
    }
    return controls;
}

```
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