Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Get Information of Programatically Created Asp.Net page Using BuildManager class

0.00/5 (No votes)
23 Sep 2009 1  
Few days back I came across a problem on asp.net site wherein a user wanted to get information about controls of a Page from a different page.

This articles was originally at wiki.asp.net but has now been given a new home on CodeProject. Editing rights for this article has been set at Bronze or above, so please go in and edit and update this article to keep it fresh and relevant.

Few days back I came across a problem on asp.net site wherein a user wanted to get information about controls of a Page from a different page. Problem with this approach is that server loads the requested page in memory. So at one time you can be only in one page say Page1. We can create object of Page2 from Page1 but can't get information about controls since controls gets initialized only when Page_Init fires and since we are just creating object of Page there is no life cycle involved with this.

Now just to get you started .Net provides lot of classes that help in dynamic compilation of pages and can be very useful if you want to cache your pages for faster access and better user experience. You should know that asp.net pages are compiled when requested by user and cached for later use.

The class that we are going to discuss and use is BuildManager class.Lets count number of panel controls in Page2 from Page1.

We will use reflection to create instance dynamically which is why we need type information of page. This can be achieved using

  // get the compiled type of referenced path  
      string pagePath = "~/Page2.aspx";  
      Type type = BuildManager.GetCompiledType(pagePath);

Once we have type we can go ahead and create instance of Page2 using

  Page myPage = (Page)Activator.CreateInstance(type);  

The above code will create page instance but if you  try to access Controls collection of Page2, you will find it as 0, this is because you have just called constructor to create instance. There is no page life cycle involved which is necessary for populating pages with controls.To execute Page life cycle we will call ProcessRequest method on Page Class as below.

    myPage.ProcessRequest(HttpContext.Current);   

Now you can access controls collection and do what you want.

******************************************************************************************************************8

Here is the complete code to count panels in Page

 


      //Initialize count to 0

      int count = 0;  
 
      // get the compiled type of referenced path  
      string pagePath = "~/Page2.aspx";  
      Type type = BuildManager.GetCompiledType(pagePath);  
 
      // if type is null, could not determine page type  
      if (type == null)  
          throw new ApplicationException("Page " + pagePath + " not found");  
 
      
      Page myPage = (Page)Activator.CreateInstance(type);  
      myPage.ProcessRequest(HttpContext.Current);  
        
      foreach (Control control in myPage.Controls)  
      {  
          if (control is Panel)  
              ++count;  
          if (control.HasControls())  
              CountControls(control, ref count);  
      }  
 

 private static void CountControls(System.Web.UI.Control control, ref int count)  
    {  
        foreach (System.Web.UI.Control childControl in control.Controls)  
        {  
            System.Web.UI.WebControls.WebControl webControl = childControl as System.Web.UI.WebControls.WebControl;  
            if (webControl != null)  
            {  
                if (webControl is System.Web.UI.WebControls.Panel)  
                    ++count;  
                if (webControl.HasControls())  
                    CountControls(webControl, ref count);  
            }  
        }  
    }  
 

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here