Posted by: dotnetmamma | April 4, 2008

Pre-Init handling

Here are some links from OdetoCode.com that talk about the  pre_Init event. 

http://odetocode.com/Blogs/scott/archive/2005/12/09/2604.aspx

http://www.odetocode.com/articles/450.aspx

I was looking for a way for the administrators of my cms to choose a template for their website.  I created a class and put it in the App_Code Folder and used the the example code for using the HttpModule.  It worked beautifully.  Here is the code I used to choose the template.  It the template names  are stored in an SQL Server database. Eventually the administrator will be able to add new templates to the cms.   

Dim value As StringDim connStr As String = ConfigurationManager.ConnectionStrings(“TobetConnectionString”).ConnectionStringDim myConn As New SqlConnection(connStr)Dim strQuery As String = “select top 1 [ID], [Template] From [SiteConfig]“Dim myCommand As New SqlCommand(strQuery, myConn)myConn.Open()Dim myReader As SqlDataReader = myCommand.ExecuteReader()myReader.Read()value = myReader.GetString(1)myReader.Close()

myConn.Close()

Return valueEnd Function Now, however, I am working on the administrative part of the web site, and I want a different master page for my admin pages.  Since the admin is still part of the same application, I am going to try to create a BasePage class that all of the user pages of my application can use, but the admin pages will have the master page hard coded and not use the BasePage class. 

The way I did it is to take the code out of the web.config for the HttpModule, and create the following Base Page Class in the App_Code Directory.

Imports Microsoft.VisualBasic

Imports System

Imports System.Data

Imports System.Collections

Imports System.Configuration

Imports System.Web

Imports System.Web.Security

Imports System.Web.UI

Imports System.Web.UI.Adapters

Imports System.Web.UI.WebControls

Imports System.Web.UI.WebControls.WebParts

Imports System.Web.UI.HtmlControls

Imports System.Data.SqlClient

Public Class BasePageInherits Page

Public Sub Page_PreInit(ByVal sender As Object, ByVal e As EventArgs) Handles Me.PreInitDim template As String

Dim connStr As String = ConfigurationManager.ConnectionStrings(“TobetConnectionString”).ConnectionStringDim myConn As New SqlConnection(connStr)Dim strQuery As String = “select top 1 [ID], [Template] From [SiteConfig]“

Dim myCommand As New SqlCommand(strQuery, myConn)myConn.Open()

Dim myReader As SqlDataReader = myCommand.ExecuteReader()myReader.Read()

template = myReader.GetString(1)

myReader.Close()

myConn.Close()

Me.MasterPageFile = template & “.master”

Me.Theme = templateEnd SubEnd Class

Then in all the pages that I need to use this master page and theme I changed the codebehind file to inherit from BasePage instead of System.Web.UI.Page. 


Leave a response

Your response:

Categories