title: List All Controls in a C# ASP.NET Project
body: <p><strong>Goal:</strong></p>
  
  <p>I am trying to create a permissions page that assigns permissions to roles based on the controls in the webforms throughout the application. I am trying to write the code that would create a string of controls for a given page. The page name would be passed in as a string. </p>
  
  <p>This post below is very similar to what I am looking for although it was written for a windows form application in VB.NET.  I am hoping to find a solution in C# and applicable to webforms.  <a href="http://www.dreamincode.net/forums/topic/222439-list-of-all-controls-in-project-vbnet/" rel="nofollow noreferrer">http://www.dreamincode.net/forums/topic/222439-list-of-all-controls-in-project-vbnet/</a></p>
  
  <p>This next post also seemed to be what I was looking for but the suggestion of find in files doesn’t work as I would like the code to be more generic and extend as I add pages or controls to forms instead of hard-coded:
  <a href="https://stackoverflow.com/questions/7038734/get-list-of-controls-on-each-webform-in-an-assembly">Get List of Controls on each WebForm in an Assembly</a></p>
  
  <p><strong>General Idea:</strong></p>
  
  <p>My idea is to have a user select a role from a drop down list. Then the user would select a page in the application from a drop down list. The SelectedIndexChanged would then query the webform class of the page selected and produce a grid listing the names of the controls on that page and another drop down list of permissions that could be assigned. When the user was done editing the controls and permissions, they would click save and the information would be saved to the database.</p>
  
  <p>When a forms authenticated user loaded the page, the page would query the database for permissions on each of the controls and based on the user’s role and permission would either have readonly, edit, or not visible status assigned to that control. My final goal is for the end user administrators to be able to manage the permissions in the application instead of having a broad set of roles and permissions or having to do custom one-off work based on a customer’s needs. Please note that this would be the only page where I am trying to access controls on other pages.</p>
  
  <p>I am trying to list all controls in an ASP.NET project or namespace since in my case there is only one namespace in my current project.  I have found plenty of examples listing the controls on the current form but have not found any code that can do it site-wide.
  I have also been able to list all the webform classes that I have:
  <a href="https://stackoverflow.com/questions/949246/how-to-get-all-classes-within-namespace">How to get all classes within namespace?</a></p>
  
  <p>But it doesn't seem that you can then go and query that class for the controls that are defined there - if someone could solve this piece, I think that would get me to the next step.</p>
  
  <p>What I have Tried:</p>
  
  <ol>
  <li><p>One was to instantiate the webform itself and then loop through the
  controls all programmatically.  This was not successful at least the
  way that I was doing it. Webform1 wf = new Webform1();  I also tried
  adding a constructor and a method to then go and loop through the
  controls.</p></li>
  <li><p>I also tried instantiating the webform class through the
  Activator.CreateInstance I tried following the example at this page:
  <a href="https://stackoverflow.com/questions/981330/c-instantiate-an-object-with-a-runtime-determined-type">Instantiate an object with a runtime-determined type</a></p>
  
  <p><strong>Example:</strong> </p>
  
  <pre><code>Page  o = (Page)System.Activator.CreateInstance(Type.GetType(pageName));
  foreach (Control c in o.Controls)
      lblMessages.Text = lblMessages.Text + c.ClientID.ToString() + "&lt;BR&gt;";
  </code></pre>
  
  <p>This was also not successful.  It did compile but I would get a control count of zero and of course no controls. </p></li>
  <li><p>The final method, which I am trying to avoid is a manual method of
  collecting the controls on the pages in my application (which
  <strong>should</strong> be static once in production) and then still having the permissions page and the administrator assign the permissions to the
  role at that point.</p></li>
  </ol>
  
  <p><strong>Finally:</strong></p>
  
  <p>Any help pointing me in the right direction of being able to dynamically fine-tune permissions to controls would be much appreciated even if that includes looking at a different way to handling permissions.</p>