Click here to Skip to main content
15,881,870 members
Articles / Web Development / ASP.NET
Article

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

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
11 Oct 2013CPOL2 min read 6K   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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
The ASP.NET Wiki was started by Scott Hanselman in February of 2008. The idea is that folks spend a lot of time trolling the blogs, googlinglive-searching for answers to common "How To" questions. There's piles of fantastic community-created and MSFT-created content out there, but if it's not found by a search engine and the right combination of keywords, it's often lost.

The ASP.NET Wiki articles moved to CodeProject in October 2013 and will live on, loved, protected and updated by the community.
This is a Collaborative Group

754 members

Comments and Discussions

 
-- There are no messages in this forum --