473,435 Members | 1,771 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,435 software developers and data experts.

Properties accessible from different parts of the page

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("REMOT E_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

Nov 19 '05 #1
1 1170
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

"BenB" wrote:
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("REMOT E_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

Nov 19 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

22
by: Generic Usenet Account | last post by:
A lot has been said in this newsgroup regarding the "evil" set/get accessor methods. Arthur Riel, (of Vanguard Training), in his class, "Heuristis for O-O Analysis & Design", says that there is...
1
by: WeAreGoing! | last post by:
Hello. I need to transfer an MD5 digest number between two pages on different domains. I know this is generally not possible, but I have full access on one domain and can insert Javascript at...
18
by: Dixie | last post by:
Can I set the Format property in a date/time field in code? Can I set the Input Mask in a date/time field in code? Can I set the Format of a Yes/No field to Checkbox in code? I am working on...
10
by: Sunny | last post by:
Hi, I have an old problem which I couldn't solve so far. Now I have found a post in that group that gave me an idea, but I can not fully understand it. The problem is: I'm trying to use a...
3
by: Joel Reinford | last post by:
I am attempting to learn how to use Master Pages in ASP.NET 2.0 and am having no success in getting the content page to see the public properties in the master page. I am using code beside and have...
5
by: ajk | last post by:
Hi I was wondering how one normally does to hide standard properties in a user control e.g. X,Y or Width. I have one approach where I have a base object which hides some of the properties and...
11
by: Web Search Store | last post by:
Hello, I set up a web page with 2 user controls. In classic asp, the first one did all the declarations, and the second one used the values, and could reset it. In ASP.Net so far I can't...
4
by: Seth Williams | last post by:
If I have some Master Page properties, and assign them when the master page first loads, knowing that I can access these property values in the current Content pages, will these properties be...
26
by: optimistx | last post by:
A variable in global scope var a1 = 'contents of global variable a1'; can be references (with some limitations) as window; // or window.a1; // or even window;
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.