Properties accessible from different parts of the page

B

BenB

Hi

I have nested masterpages. On each master page there are some user web
controls (ascx). I want to have common variables (strings, ints etc) that I
can access throughout all parts of the page - the master page, user web
controls and page itself. I just want to set them once on each page for
performance reasons because a database query will be required (once I get
this working).

I realise that i could set the properties in the New() method of the
PageVariables class (see code below) but don't want it to be run every time
this class is used on each ascx or aspx (I figure each "Dim abc as New
MyNamespace.PageVariables" would fire the database query again). So I've
tried to set values into these properties by calling LoadPageVariables()
from the page load event of the masterpage. I would rather use the master
page than have to call it from every page.

I've copied the code below. The problem is that these properties don't seem
to be holding their values between classes?? I can set a property value
from eg the aspx page load event and recall it immediately, but no values
are coming out when I use the code below.

In ASP3 I would have used an include file to set up all the variables, but
now this .NET is really confusing and frustrating me!!

How can this be done please? Or should I be approaching this differently?
I can't help thinking there must be an easier way. I you're kind enough to
answer, please don't asssume much understanding of .NET!

Many thanks
Ben

****
/code/pagevariables.vb
**********
Imports Microsoft.VisualBasic

Namespace MyNamespace
Public Class PageVariables
Private _UserIPNumber As Long
Private _UserIPAddress As String

Public Sub New()
'Could use here to set values but don't want to
'query database everytime the class is accessed which
'will be more than once on same aspx page
End Sub

Public Property UserIPAddress() As String
Get
Return _UserIPAddress
End Get
Set(ByVal value As String)
_UserIPAddress = value
End Set
End Property

Public Property UserIPNumber() As Long
Get
Return _UserIPNumber
End Get
Set(ByVal value As Long)
_UserIPNumber = value
End Set
End Property

End Class
End Namespace

*********

/code/GeneralSubs.vb
*************

Imports Microsoft.VisualBasic

Namespace MyNamespace
Public Class GeneralSubs
Public Sub LoadPageVariables()
Dim PageVariables As New PageVariables
Dim CustomFunctions As New CustomFunctions
PageVariables.UserIPAddress =
HttpContext.Current.Request.ServerVariables("REMOTE_ADDR")
PageVariables.UserIPNumber =
CustomFunctions.CLngIP(PageVariables.UserIPAddress)
End Sub
End Class
End Namespace


*********
default.aspx
**********
<%@ Page Language="VB"%>

<script runat="server">
Protected Sub Page_Load(ByVal o As Object, ByVal e As EventArgs)
Dim PVar As New MyNamespace.PageVariables
Label1.Text = PVar.UserIPAddress

End Sub
</script>


<asp:Content
ID="sitecontent"
ContentPlaceHolderID="sitecontent"
Runat="server">
<asp:Label id="Label1" runat="server" Text="Content"></asp:Label>
</asp:Content>

***********
/masterpages/main.master
*******
Namespace MyNamespace
Public Class MainMaster
Inherits System.Web.UI.MasterPage

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs)
Dim GeneralSubs As New GeneralSubs
GeneralSubs.LoadPageVariables()
End Sub

End Class
End Namespace
 
G

Guest

Hi,

I suggest that you have a "shared" variable in the topmost master page and
fill the variable in the "new" method. Also, check whether the variable is
null, and only if it is null, you may fill it. And if you want to fill it,
even if it is not null, try giving a parameter in "new" and use it.

Does this help?

Prakash.C
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top