Using common variables, subroutines and functions defined in custom namespace and class on master pages and content pages.

Posted on 2007-08-09
Last Modified: 2008-01-09
I am new to ASP.NET so I appreciate any help you can offer.

I am trying to develop some common variables, subroutines and functions to use in master pages and content pages in a new ASP.NET 2.0 website. I am developing in Visual Studio 2005 and am having a problem with variables declared in my namespace/class persisting between page reloads.

I have three files:

/App_Code/ns_common.vb file is listed below. It's quite a bit bigger than this, but this gives you the idea. I define a namespace "rzg", and the "common" class, and a global variable "NavBarTop" in this file:

Imports Microsoft.VisualBasic
Namespace rzg
    Public Class common
        Public Shared NavBarTop As String = ""
            Public Shared Sub BuildNavigation()            
                  ' In this sub I open an XML file for data, loop
                  ' through it and build the contents of the variable
                  ' NavBarTop which is a site navigation bar for all
                  ' pages at the website                        
        End Sub
    End Class
End Namespace

/master_main.master is as follows. I am trying to display the navigation bar "NavBarTop" in this file and call the subroutine to generate it in the Page_Load subroutine:

<%@ Master Language="VB"%>
<script runat="server">
    Sub Page_Load(ByVal Sender As Object, ByVal E As EventArgs)
    End Sub
<html xmlns="" >
<head runat="server">
    <title>Untitled Page</title>
        <asp:contentplaceholder id="ContentPlaceHolder1" runat="server">

/test_master.aspx is basically just an empty file right now for testing:

<%@ Page Language="VB" MasterPageFile="~/master_main.master" Title="Untitled Page" Trace="true"%>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">

I am not using code behind pages.

This seems to let me share my code (variables, subroutines and functions) between pages, but each time I refresh or reload the test_master.aspx file in my browser (IE) it keeps appending the value of NavBarTop to itself. I read somewhere that Public Shared variables persist between trips to the server, and this certainly seems to be the case.

Is there a better way to do this?

Is there a way to stop the NavBarTop from growing between page reloads?

Question by:redzoneglobal
    LVL 1

    Accepted Solution

    In the page load do this.

    If Page.IsPostBack = False then
    End If

    That will just call it once on initial load.

    Or clearing navbar prior to add in elements.

    LVL 12

    Expert Comment

    You are right shared variabels persists its state!!

    if you want to use a function located in
    Namespace rzg
    and     Public Class common

    try to make an instance like

    Dim obj As New rzg.common  and you can acess functions like other OO languages


    if the functions are shared you can call functions like Common.YourFunction()
    these are same as basic OOPS concept!!

    in  Public Shared NavBarTop As String = ""
    NavBarTop  persists its data utill AppDomain unloads!!

    To share data between pages you can you statemanagements lik
    Cookies etc.. each hav typical usage


    LVL 1

    Author Comment

    Thanks Charles. The "IsPostBack" test has no impact, but I cleared the NavBar at the beginning of the subroutine as you suggested and that did the trick.

    Apparently, the line:

    Public Shared NavBarTop As String = ""

    at the top of the class doesn't reset the variable. I'm used to a "stateless" environment as with PERL, etc, so maintaining the state of the variables in ASP.NET had me stumped.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
    ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    737 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

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now