0

i'm developping a page to manage rights in an ASP.NET application. I want to know if it exists a way to list all controls of my pages easily.

This method lists all pages in a dropdownlist :

//I take all aspx and ascx pages from my solution
foreach (String item in Directory.GetFiles(Server.MapPath("~")).
                     Where(key => key.EndsWith("aspx") || key.EndsWith("ascx")))
            {
                String[] itemSplit = item.Split('\\');
                listePages.Items.Add(new ListItem(itemSplit[itemSplit.Length - 1], itemSplit[itemSplit.Length - 1]));
            }

And this event is triggered when the user select a page :

        Page g = (Page)Activator.CreateInstance(Type.GetType(pageName));
        foreach (Control c in g.Form.Controls)
        {
            this.listeControls.Items.Add(new ListItem(c.ClientID, c.ClientID));
        }

But this event triggered a NullReferenceException

Thanks for your help.

Anthony VINEE
  • 67
  • 2
  • 10
  • I didn't get you. You want to find all the `.aspx` & `.ascx` files from a directory? Or you are trying to that directly from the solution? – Rahul Singh Oct 05 '15 at 08:32

2 Answers2

0

This is more of an approach then an answer but what I would look at is this:

Each ASPX page gets compiled into a specific .net object; you can see this by looking at the .designer file that gets created; it has a reference to each control on the page.

You could load that object and use reflection to get each of its properties and checking if it is an extension of Control.

That would give you a list for your drop-down. The trick, I guess, is finding the right object to inspect with reflection.

It would certainly be better than trying to parse the ASPX/ASCX with regex.

To provide some help, here's an SO answer regarding how you can find the compiled CodeBehind:

In an ASP.NET website with a codebehind at what point are the .cs files compiled?

Depending on how and where you run your code, you might need to consider that your site might not be running in full trust though.

Community
  • 1
  • 1
Russ Clarke
  • 17,511
  • 4
  • 41
  • 45
  • I agree with you. But first, I would test read aspx file as an XML file but it seems it doesn't work, there are too special characters ... So, I will follow your advice :) Thank you – Anthony VINEE Oct 05 '15 at 09:42
  • It would be nice, but HTML/ASPX is *not* XML, regardless of how much it looks like it! – Russ Clarke Oct 05 '15 at 09:43
0

With the solution of Russ Clarke :

using (StreamReader sr = new StreamReader(System.AppDomain.CurrentDomain.BaseDirectory + pageName + ".designer.cs"))
            {
                String line = "";

                while ((line = sr.ReadLine()) != null)
                {
                    if (line.Contains(';'))
                    {
                        String[] tab = line.Split(' ');
                        String nomSplit = tab[tab.Length - 1].Split(';')[0];

                        this.listeControls.Items.Add(new ListItem(nomSplit, nomSplit));
                    }
                }
            }

Thank you, it's just what I want.

Anthony VINEE
  • 67
  • 2
  • 10