Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Extending the Page class

Posted on 2005-04-20
6
Medium Priority
?
214 Views
Last Modified: 2010-04-07

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.AppSettings("connStr")

Most pages have a drop down box declared via:

    Protected WithEvents ddModule As System.Web.UI.WebControls.DropDownList

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.AppSettings("connStrAlt")
        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...
0
Comment
Question by:juststeve
  • 3
  • 2
6 Comments
 
LVL 11

Expert Comment

by:Ramesh Srinivas
ID: 13823966
1st method:

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
0
 

Author Comment

by:juststeve
ID: 13824223
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.AppSettings("connStr")
    Protected WithEvents ddModule As System.Web.UI.WebControls.DropDownList

    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?
0
 
LVL 23

Expert Comment

by:b1xml2
ID: 13824300
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.ParseControl("<html><head><title>"))
        Me.Controls.Add(m_title)
        Me.Controls.Add(Me.ParseControl("</title></head><body>"))
        Me.Controls.Add(m_form)
        Me.Controls.Add(Me.ParseControl("</body></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(GetType(Control)), 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.ExpertExchange.VB.WebForm5"%>
<asp:TextBox id="UserNameBox" runat="server"></asp:TextBox>
<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.DebuggerStepThrough()> 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 designerPlaceholderDeclaration 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>UserNameBox 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="dDw4MTAxNzk5NDg7dDw7bDxpPDE+Oz47bDx0PHA8bDxUZXh0Oz47bDxIZWxsbyBXb3JsZDs+Pjs7Pjs+Pjs+GMmpVG8Z/HK+J9DK2wHFCCAnQIw=" />
<input name="UserNameBox" type="text" id="UserNameBox" />
<input type="submit" name="GoButton" value="Go" id="GoButton" />
</form></body></html>
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 11

Expert Comment

by:Ramesh Srinivas
ID: 13824355
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
0
 

Author Comment

by:juststeve
ID: 13824958
thankx saleek...any thoughts about where to put the snippet that's currently in each page's Page_Load?
0
 
LVL 11

Accepted Solution

by:
Ramesh Srinivas earned 2000 total points
ID: 13825339
Something like this:

Public Class MyBasePage
    Inherits System.Web.UI.Page

    Public MyBasePage()

    Public connStr as String

    Protected Overrides Sub OnInit(ByVal e As EventArgs)
        MyBase.OnInit(e)

       ''''''''''''''''''''''''''''''''''''''''''''''''''''
       If Session("uID") = 10162 Then
            connStr = ConfigurationSettings.AppSettings("connStrAlt")
       End If
       ''''''''''''''''''''''''''''''''''''''''''''''''''''''
    End Sub

End Class
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses
Course of the Month21 days, 1 hour left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question