I've got two different ways that I've done templates. First is similar to ASP includes, and the second is using inheritence.
For the first method, I created a custom control with all my navigation elementals. Depending on the user's level, I fill the wrapper with different items, like recent edits, admin links, etc. Then on every page, I just registered the wrapper control, and every page had a span called Main-Body which I positioned with CSS to allow room for the side and header to wrap around it. This worked well, but there was a lot of repetitive code.
Now I've started using inheritence to create page templates. It took me a bit of playing around to get working, but now that I have it set up, its a breeze.
I created a PageBase class (PageBase.vb) that adds all the header and footer template information, including Body tags and Form tags, then loads the controls from the content page as it gets built (myForm). I built my previous wrapper control into it (myWrapper control), but you could just as easily create the wrapper items within the PageBase class itself.
Code:
Imports System
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.HtmlControls
Imports System.Web.UI.WebControls
Public Class PageBase
Inherits System.Web.UI.Page
Private myForm As HtmlForm
Private myWrapper as Wrapper
Protected Overrides Sub OnInit(ByVal e As System.EventArgs)
BuildPage()
MyBase.OnInit(e)
End Sub
Protected Sub BuildPage()
Dim newPlaceHolder As PlaceHolder
Dim iCurrControlNum As Integer = 0
Dim i as Integer
myForm = New HtmlForm
myForm.ID = "objForm"
'Wrapper
myForm.Controls.Add(New LiteralControl("<span class='noprint'>"))
iCurrControlNum += 1
myForm.Controls.Addat(iCurrControlNum, LoadControl("~/Resource/Global/Wrapper.ascx"))
iCurrControlNum += 1
myForm.Controls.Add(New LiteralControl("</span>"))
iCurrControlNum += 1
'Pull Content from the requested page
myForm.Controls.Add(New LiteralControl("<span class='main-body'>"))
iCurrControlNum += 1
For i = 0 To Me.Controls.Count - 1
myForm.Controls.AddAt(iCurrControlNum, Me.Controls(0))
iCurrControlNum += 1
Next
myForm.Controls.Add(New LiteralControl("</span>"))
iCurrControlNum += 1
Me.Controls.Clear()
'HEADER
Me.Controls.Add(New LiteralControl( _
"<html> " & vbCrLf & _
" <body ID='PageBody'>" & vbCrLf))
'CONTENT FORM
Me.Controls.Add(myForm)
'FOOTER
Me.Controls.Add(New LiteralControl( _
" </body>" & vbCrLf & _
"</HTML>"))
End Sub
End Class
Now, creating a new page using the template is a snap. All I have to do is make each page inherit the PageBase class instead of System.Web.UI.Page, and it automatically applies the template. The content page needs only the content itself, no form tags, no body tags, no headers.