Retrieving controls from a page object

I'm new to the .Net platform, and I'm stuck in extracting controls from ASPX pages since two days.

I am trying to get all controls from all .aspx pages on my website. Therefore, for this, I create a page object from the class name string that I get from my database. I already saved class names of .aspx.cs files in the database

Code in C #:

 Page obj = (Page)Activator.CreateInstance(null, string ClassName).Unwrap(); 

The string "ClassName" is taken from the database.

Now during debugging, I see that there are controls in obj , but I get 0 in controls.count . I think this is because the controls are still not initialized .

Image 1 during debugging Image 1 during debug time :

Image 2 during debugging showing my controls Image 2 during debug time showing my controls

My code is as follows.

 Page obj = (Page)Activator.CreateInstance(null, string ClassName).Unwrap() List<string[]> fieldsNotInDB = GetControlCollections(obj) 

This is my function to get all controls from Page obj

 public List<string[]> GetControlCollections(Page p) { List<string[]> controlList = new List<string[]>(); IterateControls(p.Controls, controlList); return controlList; } public void IterateControls(System.Web.UI.ControlCollection page, List<string[]> controlList) { foreach (System.Web.UI.Control c in page) { if (c.ID != null) { string []s=new string[2]; s[0]=c.ID; s[1]=c.GetType().ToString(); controlList.Add(s); } if (c.HasControls()) { IterateControls(c.Controls, controlList); } } } 

How do I get controls from my obj ?

+4
source share
1 answer

You are missing the fundamental point of asp.net: the page life cycle.

Here you create an instance of your page object and the time at which the collection is initialized.

But on the real asp.net website, when you reach the page, the asp.net pipeline not only creates an instance, but also fires several events attached to the pages (see http://msdn.microsoft.com/en-us/ library / ms178472 (v = vs. 100) .aspx )

In fact, your page controls will not be available until the init event.

+2
source

Source: https://habr.com/ru/post/1481316/


All Articles