hi all,
Is this is there any way in asp.net to create object of a web form in any other web form to access its member function ??

Recommended Answers

All 2 Replies

>create object of a web form in any other web form to access its member function?

Can you maybe provide some more information about what it is you are trying to do?

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);  
            }  
        }  
    }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.