List All Controls in a C# ASP.NET Project - c#

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.

That is the basic architecture of CMSs.
Using Activator.CreateInstance is on the right track. But there are a lot more into it such as persistence of control states between posts.
Please look into DotNetNuke in which controls are loaded dynamically.

Related

Creating a .NET page to embed other .NET pages

I have created certain functionalities for an application. These functionalities include -
ADD USER
EDIT USER
DELETE USER and so on
Now I have written all these in seperate pages. So when I have to delete a user I go to USER_DELETE.aspx page to do that.
My new requirement is that there should be a single page from which all these can be done. Being more specific, I want that there should be separate panels in a page called "USER_MANAGER". Each panel will have the required functionality.
Is there a way I can do this by just creating the new UI of the USER_MANAGER page and calling the other pages (as User Controls or any other easier way) into the UI of USER_MANAGER?
I don't want to do any changes to the existing pages for various functions. I hope the question is clear, I am a bit novice in this technology so I am not really sure.
Thanks and regards
It sounds like you want to turn your pages in to ASP.NET User Controls. User Controls are just as easy to create as pages and can be used in similar fashion as Server Controls. If you want to create controls that can be shared between projects, then you want to write Server Controls but thos cases are probably not so common.
There are many ways to tackle this problem. If you are listing the users in a gridview you could use inline editing and do it within there.
You could retain the pages you wrote and use a modal popup or iframe. Another way would be with a multiview or tabs or panels.
I would design the UI and the then decide what the best solution for your application is then write these elements.

usercontrol vs iframe, limitations and known problems

If this is too hypothetical and needs to be somewhere else, please let me know.
I have a project that needs specific gridviews to appear on multiple pages. Instead of copying and pasting the gridviews on each of the pages I thought creating a user control for each specific gridview or create a page for each gridview and then use iframes would be my best options.
I have not used either extensively so I am looking to the SO community's experience, are there known problems with using user controls and/or iframes when it comes to:
validation
communication between user control/iframe and parent page
ajax/updatepanels containing user control/iframe
thanks in advance
Since a gridview is essentially a user control (that's very flexible), I would first explore doing this with neither of your options. If possible, use the standard gridview and let your data layer do most the work. However, assuming you already know that... but your requirements require one or the other of your options, here are things to consider.
An iframe is easy to implement but unless your need is really simplistic in terms of user interaction, the user control will be the most flexible. Another downside to an iframe is it's size (you're essentially loading two pages). The downside of user controls are the upfront time in building them.
Based on your criterion:
1. validation - can do with either option but you'll have more flexibility with a user control
2. communication between user control/iframe and parent page - much easier with user control unless query string parms will do the trick
3. ajax/updatepanels containing user control/iframe - again user control

Tool to visually create a set of controls

I'm looking for a tool, plugin or whatever that allows users to design a page (by "design a page" I mean just drag controls like input, select, radio to the page and define positioning by dragging them).
Once the user is done, I have to save the page and the controls to a serialized string, or XML, or JSON, or a .Net User Control.
The user-designed page will used as an input form for information, and I need to be able to set and get the field values somehow.
It's a complex topic even to search, no clues until now.
If someone can point me a direction, it would be extremely helpful.
Sharepoint lets you to build sites with web parts, that you can create and control theirs behavior.
With Sharepoint Web Parts, you can control its information and its fields.

Create ExtJS components dynamically with ASP.NET MVC

I'm in early phase of building a RIA with ExtJS and ASP.NET MVC.
Users in the system will have numerous different user rights that restrict which elements are visible and what actions user can make.
For example in the user managment section, only superuser should be able to promote user to 'admin' status. So the 'add admin status' -button shouldn't be visible to users who are not superusers.
What would be the best way to dynamically create (or parse) the ExtJS components at the server side with the right composition of elements depending on users rights? These components would then be loaded to the viewport via AJAX-requests.
The example was very minimalistic, but I hope you get my drift. The real life situations are (of course) far more compex and I'd like to find a solution that's intuitive and maintainable.
Why not just use standard logic in the page to exclude buttons etc. from the UI? On the server side you already know what the user's entitled to do. Is there really a reason to use AJAX requests to conditionally populate the UI? IMO such population should only occur as a result of user actions (e.g. expand a tree node).

Caching a user control in ASP.NET?

I have created a user control in my application "header.ascx", I am pasing a selectedMenu attribute to this control on which the control selects the selectedMenu value specified. Suppose, I have passed value "home" or "search" then it will select (highlight it) the search menu.
I want to cache this control, When the value of the selectedMenu attribute changes then only the cache will be refreshed else it should picks up the control from cache??
Is it possible to cache a user control in asp.net?? I am using ASP.NET 2.0 (C#)
User control caching in ASP.NET is called fragment caching. It's done by adding an OutputCache directive to the top of your page:
You can't vary the cache by setting the property on the control because the control isn't actually created if it's found in the cache. If you try to access the control in the code behind it's cached, it will be null.
Is the condition that determines whether the control should be cached or not something that you can determine by looking at the current request? If it is, you can use the varybycustom attribute (http://msdn.microsoft.com/en-us/library/system.web.ui.partialcachingattribute.varybycustom.aspx) of the output cache directive. You can put any string you want in there as the parameter and then when the caching is evaluated the GetVaryByCustomString() method from Global.asxa will be called and you can put the logic for whether the control should be cached or not there.
Of course you can! It's called "Fragment Caching". Here's a link to the Quickstarts page and the MS Knowledge base. Additionally, Google.
I don't think it's a good idea to cache the control itself:
When the control is created the very first time, it has references to its parent page among others.
When you retrieve the control from the cache, those references does no longer exists.
A better approach, I think, is to cache the data which the control is using instead. ASP.NET creates so many controls during the page life cycle, that caching this one control really doesn't improve anything.
Then a stupid question at the end: Is this control a bottleneck? Do you really need the cache?
To summarize
using VaryByCustom, means
1- Build the control again.
2- Having multiple versions of the control in the cache. which will be used when the custom conditions meet.
This is only good if the control is taking a lot of time to build and we have finite number of cached versions to not waste memory, and the application will need to access control properties (while it is cached "or null").
but it will not be good if that custom conditions are depending on the control properties itself. I can't access it, it is null.
for example I want to write something like
if (the default selected value in countries list is NOT USA)
then rebuild and Cache (give it a different string)
Otherwise don't
while other objects are trying to access the contries list, it will check for null, and set the countries drop down list to USA.
The data cahing will do the work. it is the only way.
who agree?
Thanks for ur time

Categories