locked
Application wide variables RRS feed

  • Question

  • User659845707 posted

    I am still very new to ASP.net (1 week) 

    Using VS2005 ASPv2.0 I am using master/content pages.

    I need to show different text in a banner label depending on which web page is visited.

    The label is defined in the master page.

    I initiated an application wide variable in global.asax 

    <%@ Application Language="VB" %>
    
    <script runat="server">
    
        Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
            ' Code that runs on application startup
            Me.Application.Add("nme", "rog")
        End Sub

    The inital value'rog' is of no consequence.

    Then in each content page, in the page load event I do this

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Me.Application("nme") = "HOME"
        End Sub

    the above is for the Home content page. Other web pages will have different texts like "ABOUT" or "CONTACT" etc.

    then finally in the page load event for the master page I put

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
                    Select Case Me.Application("nme").ToString
                Case "HOME"
                    Me.Banner.Text = "putting the pieces together"
                Case "ABOUT"
                    Me.Banner.Text = "Directors"
            End Select
    
        End Sub

    so now I can change the banner text depending on the page visited.

    It seems to be working well in 'local host' mode but my question is

    Is this the correct way to do the banner text in asp.net?

    Will this work in production with scores of visitors to the site?

    Is there a better, simpler way?

    Thanks



     

     

    Thursday, October 8, 2009 9:47 PM

Answers

  • User659845707 posted

    Found a simpler way that eliminates adding an object to the Application collection in global.asax  It also eliminates using Master.FindControl in Me.Load on each content page.

    Simply use the page.title property in the Me.Load event of the master page.

    page.title returns the title of the content page while page.master.ToString returns the title of the master page. So the following code works

    Partial Class PPLmaster
        Inherits System.Web.UI.MasterPage
    
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
                   Select Case UCase(Page.Title)
                Case "PPL HOME"
                    Me.Banner.Text = "putting the pieces together"
                Case "ABOUT US"
                    Me.Banner.Text = "Directors"
            End Select
    
        End Sub
    End Class


    so the banner text can change depending on which page is cached.

    Hope this is a better solution.

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, October 9, 2009 6:42 AM

All replies

  • User1611391320 posted

    Request.Page.URL

    get the page name...

    and pased on if on each page load

     

    lable hl =(Label)((ContentPlaceholder)this.Master.FindControl("cnt")).FindControld("Lbl");

    hl.Text ="   ";

    Thursday, October 8, 2009 11:07 PM
  • User42760572 posted

    Vijay is right thats a good way, also as a note instead of application variables use Shared variables instead, application

    variables are slow to read and write. 

    Friday, October 9, 2009 1:15 AM
  • User659845707 posted

    Found a simpler way that eliminates adding an object to the Application collection in global.asax  It also eliminates using Master.FindControl in Me.Load on each content page.

    Simply use the page.title property in the Me.Load event of the master page.

    page.title returns the title of the content page while page.master.ToString returns the title of the master page. So the following code works

    Partial Class PPLmaster
        Inherits System.Web.UI.MasterPage
    
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
                   Select Case UCase(Page.Title)
                Case "PPL HOME"
                    Me.Banner.Text = "putting the pieces together"
                Case "ABOUT US"
                    Me.Banner.Text = "Directors"
            End Select
    
        End Sub
    End Class


    so the banner text can change depending on which page is cached.

    Hope this is a better solution.

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, October 9, 2009 6:42 AM