1

I have an old web form that is quite extensive with a lot of controls on it. I'd like to make a list of all the controls on it and what types they are, but I was hoping Visual Studio would have something similar to SQL Server's INFORMATION_SCHEMA table or sys.*. Does anyone know if Visual Studio has a similar feature? It doesn't have to be a table - it could be a comma separated list, etc. Web searches have turned up ways to pull controls via code, but I was really wanting something quicker.

EDIT:

So it seems the general consensus is that this type of functionality doesn't exist in Visual Studio and getting those controls must be done programmatically. @servergta 's answer seems to be the easiest alternative with the one modification of moving the initial declaration of the StringBuilder outside of the method to keep it from re-setting every time it calls itself.

Thanks to everyone who gave an answer!

rsangsura
  • 25
  • 6
  • No built in functionality for it. Use javascript to iterate through the webform to get the elements. – rageit May 05 '14 at 19:51
  • No, the exact functionality that you're describing is NOT part of VS out of the box. – SQLMason May 05 '14 at 19:52
  • have you looked at any examples using `Controls` class/method – MethodMan May 05 '14 at 20:00
  • possible duplicate of [C# Get all web controls on page](http://stackoverflow.com/questions/7362482/c-sharp-get-all-web-controls-on-page) – MethodMan May 05 '14 at 20:01
  • 1
    @DJKRAZE - It's not a duplicate of that page since I was asking for some sort of Visual Studio feature, whereas that question was asking for something programmatic. Thank you though. – rsangsura May 05 '14 at 21:44

2 Answers2

0

Not in the general Visual Studio tools. If it's a web-project, and not a web-site, you can inspect the designer.cs file that accompanies the webform, and is relatively uniform and could be inspected by machine/script. However, if you just need a visual list, you can use the class browser in Solution Explorer.

enter image description here

However, the control tree is parsed and generated at run-time, so it won't be a definitive list. (Data bound controls, for example). To get a definitive list, you could, at runtime, use Trace functionality which will give you the control tree outputted to an HTML table.

Richthofen
  • 2,076
  • 19
  • 39
  • I don't seem to be able to see any of that in my server explorer, but all of the controls were hard-coded to the `.aspx` itself, so maybe that's why? In any case, it seems that my search for a Visual Studio feature has been pretty pointless and I'll have to end up with some level of coding to get them. Thanks for the suggestion though! – rsangsura May 05 '14 at 21:41
  • Ahh. I have Visual Studio 2013, which is why. Might be worth an upgrade? – Richthofen May 06 '14 at 13:58
0

Sorry for the other answer, haven't tested it.

Here is a method that I've tested and worked:

private string getctl(Control master)
    {
        StringBuilder sb = new StringBuilder();
        foreach (Control child in master.Controls)
        {
            sb.AppendFormat("| {0} - {1}", child.ClientID.ToString(), child.GetType().ToString());
            if (child.HasControls())
            {
                sb.Append(getctl(child));
            }
        }
        return sb.ToString();
    }

you could run it like:

string controls = getctl(this.Page);
Luiz Eduardo
  • 380
  • 4
  • 13
  • Pretty good, but the best practice would be to use a StringBuilder instead of string concatenation. As a hint, you might want to try `sb.AppendFormat` for your `txt += "|" + ...` line. Do that, and I'll upvote. – John Saunders May 05 '14 at 20:50
  • I see, but this question is too "basic" so I simplified the answer for begginers, StringBuilder is correct way to do this on production environment, but for a one time run code, I think this is not necessary, but I'll edit the post like you said. – Luiz Eduardo May 05 '14 at 20:57
  • That's a nice piece of code. I'll probably end up using it since it seems that the functionality I'm looking for doesn't really exist in Visual Studio, but I would make one correction to your code that I found when testing: Declaring the new `string` or `StringBuilder` inside the method has the effect of re-setting the string to empty every time it calls itself, so it ends up returning only the last parent and its children. However, if you move the declaration outside of the method more globally, it then returns everything. Thanks for the useful answer thought! I appreciate it. – rsangsura May 05 '14 at 21:37
  • sorry, but an error may have occurred, I've tried to run the code again and all controls were listed – Luiz Eduardo May 06 '14 at 00:15