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
/master_main.master
/test_master.aspx


/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)
        rzg.common.BuildNavigation()
    End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <div>
     <%=rzg.common.NavBarTop%>
    </div>
          <div>
        <asp:contentplaceholder id="ContentPlaceHolder1" runat="server">
        </asp:contentplaceholder>
    </div>
</body>
</html>


/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">
</asp:Content>


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?

Thanks.
LVL 1
redzoneglobalAsked:
Who is Participating?
 
Charles-RussellCommented:
In the page load do this.

If Page.IsPostBack = False then
Call NAVBARLOAD
End If

That will just call it once on initial load.

Or clearing navbar prior to add in elements.

0
 
renjurdevanCommented:
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

obj.YourFunction()

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
Session
Caching
QuerryStrings
Application
Cookies etc.. each hav typical usage

Regards
Renju

0
 
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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.