asp.net Can I force every page to inherit from a base page? Also should some of this logic be in my master page?
Posted
by
Bex
on Stack Overflow
See other posts from Stack Overflow
or by Bex
Published on 2011-01-14T09:39:23Z
Indexed on
2011/01/14
9:53 UTC
Read the original article
Hit count: 237
Hi!
I have a web app that has a base page.
Each page needs to inherit from this base page as it contains properties they all need as well as dealing with the login rights.
My base page has some properties, eg: IsRole1, IsRole2, currentUserID, Role1Allowed, Role2Allowed.
On the init of each page I set the properties "Role1Allowed" and "Role2Allowed"
Private Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
Role1Allowed = True
Role2Allowed= False
End Sub
The basepage then decides if the user needs redirecting.
'Sample code so not exactly what is going to be, bug gives the idea
Protected Overridable Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
If Role1Allowed And Not Role1 Then
'Redirect somewhere
End If
End Sub
The page then must override this pageload if they need anything else in it, but making sure they call the base pageload first.
Protected Overrides Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
MyBase.Page_Load(sender, e)
If Not IsPostBack Then
BindGrid()
End If
End Sub
The other properties (IsRole1, IsRole, currentUserID) are also accessible by the page so it can be decided if certain things need doing based on the user.
(I hope this makes sense)
Ok so I have 2 questions
- Should this functionality be in the base page or should it somehow be in the master, and if so how would I get access to all the properties if it was?
- As there are multiple people working on this project and creating pages some are forgetting to inherit from this basepage, or call the base pageload when overriding it. Is there any way to force them to do this?
Thanks for any help.
bex
© Stack Overflow or respective owner