views:

460

answers:

3

I have a certain page and depending on an administrator role a lot of extra validation controls have to be inserted. However I don't want to take the risk that a validator gets turned on for other users.

What I was thinking is to use some form of "Visual inheritance" but I don't know which path to choose. Perhaps have separate .aspx pages which inherit from the same class (which inherits from Page)?

Side note: this has to be done in webforms, not mvc.

Extra information: The problem I'm trying to solve is the separation of roles and the impact on the page without having to duplicate the page and having to maintain any changes in both. There are clients who get to see the vanilla page and admins can see the same information but with added validation controls. However these validation controls should not be seen by the clients. Also some more information can be seen but that could be handled by using rolebased viewing of certain user control.

Anyone has an idea about this?

+2  A: 

Why don't you want to use master pages? Master pages do exactly what you want to do - they can even "inherit" from each other (in a manner of speaking).

I would suggest that you use master pages to do what you want.

ASP.NET master pages allow you to create a consistent layout for the pages in your application. A single master page defines the look and feel and standard behavior that you want for all of the pages (or a group of pages) in your application. You can then create individual content pages that contain the content you want to display. When users request the content pages, they merge with the master page to produce output that combines the layout of the master page with the content from the content page.

Andrew Hare
We were also thinking about that but in that case the problem shifts to the place of the content pages. Or do you mean that the textbox would be on the master page and the validator, if needed, is placed on the content page? We have more than 100 controls on our page and also what about grids/listviews? I think this is a nogo solution.
Nyla Pareska
I don't understand - what problem are you trying to solve? How would you better solve it?
Andrew Hare
I added some extra information in my question to hopefully clarify it a bit more.
Nyla Pareska
A: 

Often such problems can be solved by splitting the page into user controls (.ascx). It can become a bit messy, but gets the job done.

Inheritance of pages is difficult, although possible. What you must realize, is that one .aspx page cannot inherit from another .aspx page. This is because of how the ASP.NET compiler works: your .aspx XML markup is compiled into a class that derives from your code-behind class. Since this only happens at runtime, you cannot have another .aspx page inherit from it, because the class is simply not there when the code-behind is being compiled.

What you can do is to create another class that inherits from Page and make your .aspx code-behind classes inherit from that. That is possible, but note that this class will not have an .aspx XML part - you will have to instantiate all the controls yourself, as well as assign their properties. In most cases this will be quite messy.

Vilx-
+2  A: 

If the problem you are trying to solve is role based viewing of controls and you are using the builtin asp.net membership/roles providers, you could use the LoginView control to manage the visibilty of the admin stuff:

<asp:LoginView runat="server" ID="LoginView">
    <RoleGroups>
        <asp:RoleGroup Roles="Admin">
            <ContentTemplate>
                Special Admin content
                <asp:RequiredFieldValidator></asp:RequiredFieldValidator>

            </ContentTemplate>
        </asp:RoleGroup>
    </RoleGroups>
</asp:LoginView>
HectorMac
Won't this reduce the performance and readability a lot when having 100 of such controls?
Nyla Pareska
Is performance a problem currently? If so, is the bottle neck related to the creation of controls? The LoginView control is pretty lightweight. The logic to hide/show admin content has to live somewhere. The declarative control allows you to express yourself visually as opposed to in the code-behind. It is certainly a little verbose when expressed 100 times. You could skip the loginview control by creating custom server/user control wrappers that expose a role property in addition to the underlying properties of the validator control(s) you are trying tohide/show.
HectorMac
I like that custom control approach. I'll dig a little deeper into this to see if it fits the need.
Nyla Pareska