Search code examples
asp.netvb.netweb-applications

Reuse VB Declaration From Master Page On Multiple Web App Pages


I have a value declared on my .NET web app master page in code behind...

Dim SiteID As New Integer
SiteID = System.Web.HttpContext.Current.Request.UserHostAddress.Substring(8, 2).ToString

I want to be able to reference the "SiteID" declaration on other pages without having to enter the above code on each page. Can this be done?

Thanks


Solution

  • Well, say in your master page, you have this:

    Public Class SiteMaster
        Inherits MasterPage
    
        Public SiteID As Integer
    
    
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    
            SiteID = 1234
    
    
        End Sub
    etc. etc. rest of master page code here....
    

    So, your example code? Well, assuming your declare the var at the class level? (and declare as public)

    Then in any child page, you can do this

        Dim MyMaster As SiteMaster = Me.Master
    
        Label1.Text = MyMaster.SiteID
    

    So, you are free to get the master page object in your child page. Do keep in mind that in cases in which you have more then one master page, then you want to use the correct master page type (in most cases SiteMaster).

    And your code snip does not make 100% sense, since the declare of the variable would be in the site master page (at class level, not event code stub level, since such variables would go out of scope once the given code stub completes). However, as noted, simply declare the variable as Public, and at the page class level, and with above code, then any child page is free to use that variable.

    However, do keep in mind, that when a child + master page loads, the child page load event fires first, and then the master page load event fires. So, in theory, code in the child page load event runs before the page load event in the master page. Hence, setting values in the master page load event may well not work if you require that value in the page load event of the child page. So, "when" you setup the variable in your master page, the order of events can often matter.

    If you need/want (most likely) to setup public class vars in the master page?

    Then use the "_Init" event of the master page to setup such public variables in the master page. This event runs first, and before the child and master page load events. Hence, this will allow use of such public variables setup in the master page in the child page load event.

    Hence, your master code can be say this:

        Dim s7 As String = "0"
        Dim s8 As String = "0"
    
        s7 =
            HttpContext.Current.Request.UserHostAddress.Substring(7, 2).ToString()
    
        s8 =
            HttpContext.Current.Request.UserHostAddress.Substring(8, 2).ToString()
    
        SiteID = 60     ' default value if all conditions fail
    
        Select Case s7
            Case "20", "40", "60"
                SiteID = s7
        End Select
    
        Select Case s8
            Case "20", "40", "60"
                SiteID = s8
        End Select
    

    So, with above, then in your child page, you can do this: (and note that we can't load the var in the page class, we have to wait at least until the page load event.

    Hence not here:

    Public Class GetMasterValue
        Inherits System.Web.UI.Page
    
        Dim MyMaster As MyCalendar2.SiteMaster = Me.Master  <-- WILL NOT WORK!!!!
    
    
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    
    
            Label1.Text = MyMaster.SiteID
    
        End Sub
    
    
        Dim MyMaster As MyCalendar2.SiteMaster = Me.Master
    

    So, this will work:

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    
        Dim MyMaster As MyCalendar2.SiteMaster = Me.Master
    
        Label1.Text = MyMaster.SiteID
    
    End Sub
    

    So, in the child page, we have to wait at least until page load event. If you want the value scoped to all code and global to that child page?

    The this:

    Dim MyMaster As MyCalendar2.SiteMaster = Nothing
    
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    
    
        MyMaster = Me.Master
    
        Label1.Text = MyMaster.SiteID
    
    End Sub
    

    So, you can scope the master page var to the whole page (class level), but you have to wait to at least until the page load event in that child page to obtain use of values that ARE BEING SETUP by the master page.

    So, for controls and in "general", you can reference the master page in child page (and code order will not matter).

    However, as noted, the order of events in the master page matters much WHEN setting up variables in master page, since the child page load event triggers before the master page event.

    So, in this case here, we must ensure master page code that is to setup public variables has to run BEFORE the child page load event (we acheived this by moving the variable assignmentss to the init event in the master page).

    In your case, you did not follow my example, and setup the instance of the master page in child by using a declare + set in one line at the class level.

    Thus, the reference setting of the master page object in child page thus should wait until page load event.