juststeve
asked on
Extending the Page class
At http://aspnet.4guysfromrolla.com/articles/041305-1.aspx is an article explaining how/why to extend the Page class to bring a core group of properties and methods to all pages within an existing web app. I'm looking to clarify & verify my understanding of the article.
To Verify:
Each page of my web app hits a db so each page has a connection object. Currently each page declares:
Public connStr As String = ConfigurationSettings.AppS
Most pages have a drop down box declared via:
Protected WithEvents ddModule As System.Web.UI.WebControls.
that is created by a Function:
Function CreateModules() As ICollection
ddModule.Items.Add(New ListItem("First Item", "table1"))
ddModule.Items.Add(New ListItem("Second Item", "table2"))
ddModule.Items.Add(New ListItem("Third Item", "table3"))
End Function
The page_Load event on each page checks a session var to see if an alternative connection string should be used.
If Session("uID") = 10162 Then
connStr = ConfigurationSettings.AppS
End If
All these constructs would be better placed in a class that extends ASP.NET's Page class, yes?
Now to clarify the implementation:
The article includes 2 methods of adding this class to the project.
>>
If you are using Visual Studio .NET, the base class can be added to your ASP.NET project in one of two ways. The simplest option is to add a Class File to your ASP.NET project. To do this, right-click on the folder you want to add your project in the VS.NET Solution Explorer and choose to Add and then Add Class.
A better approach, in my opinion, is to add a new Class Library project to the Solution. To do this, right-click on the Solution in the Solution Explorer, select Add and choose New Project. Add a New Project of type Class Library in your language of choice. You can then add your base class to this new project. Finally, be sure to add a Reference from the Web application to the Class Library project by right-clicking on the Web application's References folder, selecting Add Reference. From the Add References dialog box, select the Projects tab and then add the Class Library project you just created.
<<
I'm not well versed in controlling/configuring VS and would like the above spelled out in a bit more detail.
tx
--steve...
ASKER
thx...under what conditions would the second approach be 'better' as suggested by the article? Situations where the custom class itself would be re-used across a number of different web apps?
I've posted a number of elements i'll be inserting in MyBasePage:
Some properties and a method from the declaration section of the class:
Public connStr As String = ConfigurationSettings.AppS ettings("c onnStr")
Protected WithEvents ddModule As System.Web.UI.WebControls. DropDownLi st
Function CreateModules() As ICollection
ddModule.Items.Add(New ListItem("First Item", "table1"))
ddModule.Items.Add(New ListItem("Second Item", "table2"))
ddModule.Items.Add(New ListItem("Third Item", "table3"))
End Function
and a bit of code that runs at Page_Load. Does my snippet formerly in the Page_Load belong in hte OnInit?
I've posted a number of elements i'll be inserting in MyBasePage:
Some properties and a method from the declaration section of the class:
Public connStr As String = ConfigurationSettings.AppS
Protected WithEvents ddModule As System.Web.UI.WebControls.
Function CreateModules() As ICollection
ddModule.Items.Add(New ListItem("First Item", "table1"))
ddModule.Items.Add(New ListItem("Second Item", "table2"))
ddModule.Items.Add(New ListItem("Third Item", "table3"))
End Function
and a bit of code that runs at Page_Load. Does my snippet formerly in the Page_Load belong in hte OnInit?
basepage class
======
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.HtmlControls
Imports System.Collections
Public Class MyBasePage
Inherits System.Web.UI.Page
Private m_form As HtmlForm
Private m_title As Literal
Protected Overridable Sub Page_Init(ByVal sender As Object, ByVal e As EventArgs)
Dim controls As Control() = GetControls()
Me.Controls.Clear()
m_form = New HtmlForm
m_form.ID = "Form1"
m_title = New Literal
m_title.ID = "TitleLiteral"
Me.Controls.Add(Me.ParseCo ntrol("<ht ml><head>< title>"))
Me.Controls.Add(m_title)
Me.Controls.Add(Me.ParseCo ntrol("</t itle></hea d><body>") )
Me.Controls.Add(m_form)
Me.Controls.Add(Me.ParseCo ntrol("</b ody></html >"))
'move controls back to form
SetControls(controls)
End Sub
Private Function GetControls() As Control()
Dim list As New ArrayList
For Each ctl As Control In Me.Controls
list.Add(ctl)
Next
Return DirectCast(list.ToArray(Ge tType(Cont rol)), Control())
End Function
Private Sub SetControls(ByVal controls As Control())
For Each item As Control In controls
Me.Form.Controls.Add(item)
Next
End Sub
Protected ReadOnly Property Form() As HtmlForm
Get
Return m_form
End Get
End Property
Protected Property Title() As String
Get
Return m_title.Text
End Get
Set(ByVal value As String)
m_title.Text = value
End Set
End Property
End Class
aspx page
===
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm5.aspx. vb" Inherits="b1xml2.ExpertExc hange.VB.W ebForm5"%>
<asp:TextBox id="UserNameBox" runat="server"></asp:TextB ox>
<asp:Button id="GoButton" runat="server" Text="Go"></asp:Button>
code-behind
========
Public Class WebForm5
Inherits MyBasePage
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.Debugg erStepThro ugh()> Private Sub InitializeComponent()
End Sub
Protected WithEvents UserNameBox As System.Web.UI.WebControls. TextBox
Protected WithEvents GoButton As System.Web.UI.WebControls. Button
'NOTE: The following placeholder declaration is required by the Web Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclara tion As System.Object
Protected Overrides Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
MyBase.Page_Init(sender, e)
End Sub
#End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
If Not IsPostBack Then
MyBase.Title = "Hello World"
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GoButton.Click
Response.Write("<div>UserN ameBox Text Is " & Me.UserNameBox.Text & "</div>")
End Sub
End Class
html output
========
<html><head><title>Hello World</title></head><body> <form name="Form1" method="post" action="WebForm5.aspx?yjy" id="Form1">
<input type="hidden" name="__VIEWSTATE" value="dDw4MTAxNzk5NDg7dDw 7bDxpPDE+O z47bDx0PHA 8bDxUZXh0O z47bDxIZWx sbyBXb3JsZ Ds+Pjs7Pjs +Pjs+GMmpV G8Z/HK+J9D K2wHFCCAnQ Iw=" />
<input name="UserNameBox" type="text" id="UserNameBox" />
<input type="submit" name="GoButton" value="Go" id="GoButton" />
</form></body></html>
======
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.HtmlControls
Imports System.Collections
Public Class MyBasePage
Inherits System.Web.UI.Page
Private m_form As HtmlForm
Private m_title As Literal
Protected Overridable Sub Page_Init(ByVal sender As Object, ByVal e As EventArgs)
Dim controls As Control() = GetControls()
Me.Controls.Clear()
m_form = New HtmlForm
m_form.ID = "Form1"
m_title = New Literal
m_title.ID = "TitleLiteral"
Me.Controls.Add(Me.ParseCo
Me.Controls.Add(m_title)
Me.Controls.Add(Me.ParseCo
Me.Controls.Add(m_form)
Me.Controls.Add(Me.ParseCo
'move controls back to form
SetControls(controls)
End Sub
Private Function GetControls() As Control()
Dim list As New ArrayList
For Each ctl As Control In Me.Controls
list.Add(ctl)
Next
Return DirectCast(list.ToArray(Ge
End Function
Private Sub SetControls(ByVal controls As Control())
For Each item As Control In controls
Me.Form.Controls.Add(item)
Next
End Sub
Protected ReadOnly Property Form() As HtmlForm
Get
Return m_form
End Get
End Property
Protected Property Title() As String
Get
Return m_title.Text
End Get
Set(ByVal value As String)
m_title.Text = value
End Set
End Property
End Class
aspx page
===
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm5.aspx.
<asp:TextBox id="UserNameBox" runat="server"></asp:TextB
<asp:Button id="GoButton" runat="server" Text="Go"></asp:Button>
code-behind
========
Public Class WebForm5
Inherits MyBasePage
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.Debugg
End Sub
Protected WithEvents UserNameBox As System.Web.UI.WebControls.
Protected WithEvents GoButton As System.Web.UI.WebControls.
'NOTE: The following placeholder declaration is required by the Web Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclara
Protected Overrides Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
MyBase.Page_Init(sender, e)
End Sub
#End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
If Not IsPostBack Then
MyBase.Title = "Hello World"
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GoButton.Click
Response.Write("<div>UserN
End Sub
End Class
html output
========
<html><head><title>Hello World</title></head><body>
<input type="hidden" name="__VIEWSTATE" value="dDw4MTAxNzk5NDg7dDw
<input name="UserNameBox" type="text" id="UserNameBox" />
<input type="submit" name="GoButton" value="Go" id="GoButton" />
</form></body></html>
Yes, the second approach would be best if you require custom classes for multiple solutions. You would just include the project reference in your solution to use with any project.
Where as the class file .vb file needs to be included itself in any other projects requiring the base class.
regards,
KS
Where as the class file .vb file needs to be included itself in any other projects requiring the base class.
regards,
KS
ASKER
thankx saleek...any thoughts about where to put the snippet that's currently in each page's Page_Load?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Create a new .vb file (class file) by right-clicking your project in the right hand pane and selected add new and then choose class file (.vb extension).
In that file you would add something like:
Public Class MyBasePage
Inherits System.Web.UI.Page
Public MyBasePage()
Protected Overrides Sub OnInit(ByVal e As EventArgs)
MyBase.OnInit(e)
''''''''''''''''''''''''''
Insert your extension to page here
''''''''''''''''''''''''''
End Sub
End Class
Then in every page which you want to inherit this page, you would write:
Public Class SomePage
Inherits MyBasePage
''''''''''''''''''''''''''
Your code
''''''''''''''''''''''''''
End Class