Click here to Skip to main content
15,894,539 members
Articles / Web Development / ASP.NET

Page Template Framework for ASP.NET 1.1

Rate me:
Please Sign up or sign in to vote.
4.64/5 (63 votes)
16 Nov 20048 min read 298.2K   5.3K   168  
The Page Template Framework for ASP.NET 1.1 provides a configurable solution for creating page templates in a Web application. Using this framework, page templates are stored in an XML file, and can be dynamically configured without recompiling the Web application.
Imports System
Imports System.Web.Caching
Imports System.Collections
Imports System.IO
Imports System.Web
Imports System.Xml
Imports System.Xml.Serialization
Imports Boardworks.Web.UI.PageFramework.Config

Public Class PageBase
    Inherits System.Web.UI.Page

    ' Default Constructor
    Public Sub New()

    End Sub


    ' Override the OnInit method of the Page class, and implement
    ' the controls listed in the Page.config file
    ' @e: The event arguments
    Protected Overrides Sub OnInit(ByVal e As EventArgs)

        ' Obtain the path to the Page.config file for the current application
        Dim pageConfigPath As String = Server.MapPath(PageConfig.PageConfigFilePath)

        ' Check to see if the Page.config file exists
        If (File.Exists(pageConfigPath)) Then

            ' Check to see if the Page.config file has been loaded into Cache
            If (Cache.Item(PageConfig.PageConfigCacheKey) Is Nothing) Then

                ' Create a CacheDependency on the Page.config file
                Dim dependency As New System.Web.Caching.CacheDependency(pageConfigPath)

                ' Load the Page.config file and insert the PageConfig into Cache
                Cache.Insert(PageConfig.PageConfigCacheKey, PageConfig.Load(pageConfigPath), dependency)

            End If

            ' Load the Page.config file into a PageConfig object
            Dim pc As PageConfig = CType(Cache.Item(PageConfig.PageConfigCacheKey), PageConfig)

            ' Get a reference to the current Page from the PageConfig
            Dim page As page = pc.FindPage(Request)

            ' Locate the PageTemplate for the current Page object
            Dim template As PageTemplate
            If (page Is Nothing) Then
                template = pc.FindDefaultTemplate()
            Else
                template = pc.FindTemplate(page.TemplateName)
            End If


            ' Check to ensure a valid TemplateName attribute was specified
            If (Not template Is Nothing) Then

                ' Declare a Control into which a template will be loaded
                Dim control As System.Web.UI.Control = Nothing

                ' Iterate over the Controls in the template to locate the Controls with BeforeContent placement
                For idx As Int32 = 0 To template.Controls.Count - 1

                    ' Check to see if the current Control has BeforeContent placement
                    Dim currentControl As Boardworks.Web.UI.PageFramework.Config.Control = CType(template.Controls.Item(idx), Boardworks.Web.UI.PageFramework.Config.Control)
                    If (currentControl.ControlPlacement = ControlPlacement.BeforeContent) Then

                        ' Load the specified Control from the Path specified in the PageConfig
                        control = Me.LoadControl(currentControl.Path)

                        ' Check to see if the Control requires a unique name
                        If (Not currentControl.UniqueName Is Nothing) Then

                            ' Ensure that the value is not an Empty String
                            If (currentControl.UniqueName <> String.Empty) Then

                                ' Set the ID property of the Control to the UniqueName attribute
                                control.ID = currentControl.UniqueName

                            End If

                        End If

                        ' Add the Contorl to the current Page
                        Me.Controls.AddAt(idx, control)

                    End If

                Next

                ' Initialize the Page content
                MyBase.OnInit(e)

                ' Iterate over the Controls in the template to locate the Controls with AfterContent placement
                Dim ctrl As Boardworks.Web.UI.PageFramework.Config.Control
                For Each ctrl In template.Controls

                    ' Check to see if the current Control has AfterContent placement
                    If (ctrl.ControlPlacement = ControlPlacement.AfterContent) Then

                        ' Load the specified Control from the Path specified in the PageConfig
                        control = Me.LoadControl(ctrl.Path)

                        ' Check to see if the Control requires a unique name
                        If (Not ctrl.UniqueName Is Nothing) Then

                            ' Ensure that the value is not an Empty String
                            If (ctrl.UniqueName <> String.Empty) Then

                                ' Set the ID property of the Control to the UniqueName attribute
                                control.ID = ctrl.UniqueName

                            End If

                        End If

                        ' Load the specified Control and add it to the current Page
                        Me.Controls.Add(control)
                    End If

                Next ctrl

            Else

                ' Check to ensure that the Page as listed in the PageConfig
                If (Not page Is Nothing) Then

                    ' The page has an invalid TemplateName attribute, throw an exception
                    Throw New Exception(String.Format("Invalid TemplateName for Page ""{0}"".  There is no template named ""{1}"".", page.Path, page.TemplateName))

                Else

                    ' The pages was not listed in the PageConfig, so Initialize the Page content
                    MyBase.OnInit(e)

                End If

            End If

        Else

            ' Initialize the Page content
            MyBase.OnInit(e)

        End If

    End Sub

End Class

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
Scott Van Vliet is a Principal Consultant with Neudesic based in Irvine, CA. He has been designing and developing technology solutions for the past 8 years, and has worked with Microsoft .NET technologies for over 3 years. Scott is currently developing solutions with ASP.NET 2.0, C# 2.0, Windows Presentation Foundation (codename "Avalon"), SQL Server 2000/2005, Reporting Services and SharePoint.

Scott welcomes feedback and can be reached through his Weblog at http://weblogs.asp.net/skillet/.

Comments and Discussions