Bex Bex - 5 months ago 29
ASP.NET (C#) Question Can I force every page to inherit from a base page? Also should some of this logic be in my master page?

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
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

  1. 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?

  2. 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?


After reading this, i found this: link to web config changes Should be perfect for ya.