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

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?

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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


redzoneglobalAuthor Commented:
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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.