Page view counter

Looping Through Controls in Master Pages

Last post 07-25-2007 5:24 PM by mihirpathak. 1 replies.

Sort Posts:

  • Looping Through Controls in Master Pages

    07-25-2007, 4:52 PM
    • Loading...
    • armst13
    • Joined on 06-15-2007, 3:23 PM
    • Posts 58
    • Points 38

    Is there anyway to find the "main control" on a form and loop through them all, when using master pages, without having to do a massive nested loop such as the following:

    //foreach(Control c1 in Page.Controls)

    //{

    // if(c1.GetType().ToString() == "ASP.site_master")

    // {

    // foreach(Control c2 in c1.Controls)

    // {

    // if (c2.GetType().ToString() == "System.Web.UI.HtmlControls.HtmlForm")

    // {

    // foreach (Control c3 in c2.Controls)

    // {

    // if (c3.GetType().ToString() == "System.Web.UI.WebControls.ContentPlaceHolder")

    // {

    // foreach (Control c4 in c3.Controls)

    I am trying to do something like the following, using various methods, but all of these have failed.  It is more clean and would seem to be more efficient than nested if statements.  But these always come back "NULL" everytime.  Or do I have to do the nested method.  It would seem there should be a way, but I can't find a way. 

    Control c7 = this.FindControl("ASP.site_master"); Control c5 = Page.FindControl("System.Web.UI.HtmlControls.HtmlForm");

    Control c6 = this.FindControl("ContentPlaceHolder1");

    Thanks

    Andy

  • Re: Looping Through Controls in Master Pages

    07-25-2007, 5:24 PM
    Answer
    • Loading...
    • mihirpathak
    • Joined on 10-18-2002, 12:16 AM
    • Posts 199
    • Points 1,134

    Hi,

     If you have Label inside your master control you can access it:

    Label lbl = (Label)Master.FindControl("Label1");

    so you can try this option and see its help you

    foreach(Control c1 in Master.Controls) {}

    MP

    I never desire to converse with a man who has written more than he has
    read. -Samuel Johnson, lexicographer (1709-1784)
Page 1 of 1 (2 items)