?
Solved

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

Posted on 2007-08-09
3
Medium Priority
?
687 Views
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
/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.
0
Comment
Question by:redzoneglobal
3 Comments
 
LVL 1

Accepted Solution

by:
Charles-Russell earned 2000 total points
ID: 19667566
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
 
LVL 12

Expert Comment

by:renjurdevan
ID: 19667674
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
 
LVL 1

Author Comment

by:redzoneglobal
ID: 19667685
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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses
Course of the Month14 days, 17 hours left to enroll

840 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