Goal:
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.
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. http://www.dreamincode.net/forums/topic/222439-list-of-all-controls-in-project-vbnet/
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: Get List of Controls on each WebForm in an Assembly
General Idea:
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.
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.
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: How to get all classes within namespace?
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.
What I have Tried:
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.
I also tried instantiating the webform class through the Activator.CreateInstance I tried following the example at this page: Instantiate an object with a runtime-determined type
Example:
Page o = (Page)System.Activator.CreateInstance(Type.GetType(pageName)); foreach (Control c in o.Controls) lblMessages.Text = lblMessages.Text + c.ClientID.ToString() + "<BR>";
This was also not successful. It did compile but I would get a control count of zero and of course no controls.
The final method, which I am trying to avoid is a manual method of collecting the controls on the pages in my application (which should be static once in production) and then still having the permissions page and the administrator assign the permissions to the role at that point.
Finally:
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.