Dynamic Master Pages

Tips submitted by ASP.NET Maker users

Dynamic Master Pages

Postby bmsvic » Mon Oct 04, 2010 12:48 pm

Sometimes you may want to have different masterpages for different user types etc.

This can be done by modifying the template and inserting a procudure like:

Protected Sub Page_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit

Dim UserTypeId As Integer = Convert.ToInt32(Session("UserTypeId"))
Select Case UserTypeId
Case 1, 2, 3
Me.MasterPageFile = "zMasterPage.master"
Case Else
Me.MasterPageFile = "zMasterPage3.master"
End Select

End Sub

This must be a Page_PreInit event since this is the only place that you can change a master page (before the page is rendered).

The file to change in the template is aspxcommon-scripts.aspx.vb or aspxcommon-scripts.aspx.cs.

Towards the bottom of this file is where the Page_Load event is generated for each page. It is logical to put the above procedure just before this Page_Load.

Regards ..... Mark

Return to User Submited Tips (ASP.NET Maker)