473,324 Members | 2,581 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,324 software developers and data experts.

global variables question

This is probably a commonly asked question, but I can't find a good way to
search for it, so I will ask here.

I assumed that there had to be an easy way to access a page level variable
from a user control.
IE. default.aspx has a variable "gName"
default.aspx has a user control, "Navigation"
I want to be able to access gName inside "Navigation"'s page_load

This appears to not be possible.... and it makes sense looking at the
structure.... but how are we expected to share data across classes?
Weird dumb question, but I don't get it. I can think of work arounds,
sessions, etc. But that seems like a rough solution

Hope this makes sense to someone,
Steve
Nov 18 '05 #1
8 1387
Steve,

Not sure exactly what you're doing, but this is very doable. What language
are you using and I will give you a code sample.

Jim Cheshire, MCSE, MCSD [MSFT]
Developer Support
ASP.NET
ja******@online.microsoft.com

This post is provided as-is with no warranties and confers no rights.

--------------------
From: "Steve Klett" <sk************@yahoo.com>
Subject: global variables question
Date: Fri, 21 Nov 2003 09:48:31 -0800
Lines: 18
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
Message-ID: <em**************@tk2msftngp13.phx.gbl>
Newsgroups: microsoft.public.dotnet.framework.aspnet
NNTP-Posting-Host: 190.muaa.sttl.sttwa01r1.dsl.att.net 12.102.48.190
Path: cpmsftngxa07.phx.gbl!cpmsftngxa10.phx.gbl!TK2MSFTN GXA05.phx.gbl!TK2MSFTNGP08
.phx.gbl!tk2msftngp13.phx.gblXref: cpmsftngxa07.phx.gbl microsoft.public.dotnet.framework.aspnet:191923
X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet

This is probably a commonly asked question, but I can't find a good way to
search for it, so I will ask here.

I assumed that there had to be an easy way to access a page level variable
from a user control.
IE. default.aspx has a variable "gName"
default.aspx has a user control, "Navigation"
I want to be able to access gName inside "Navigation"'s page_load

This appears to not be possible.... and it makes sense looking at the
structure.... but how are we expected to share data across classes?
Weird dumb question, but I don't get it. I can think of work arounds,
sessions, etc. But that seems like a rough solution

Hope this makes sense to someone,
Steve


Nov 18 '05 #2
hey Jim,

C#. Eager to see what have...

-SK
"Jim Cheshire [MSFT]" <ja******@online.microsoft.com> wrote in message
news:r0**************@cpmsftngxa07.phx.gbl...
Steve,

Not sure exactly what you're doing, but this is very doable. What language are you using and I will give you a code sample.

Jim Cheshire, MCSE, MCSD [MSFT]
Developer Support
ASP.NET
ja******@online.microsoft.com

This post is provided as-is with no warranties and confers no rights.

--------------------
From: "Steve Klett" <sk************@yahoo.com>
Subject: global variables question
Date: Fri, 21 Nov 2003 09:48:31 -0800
Lines: 18
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
Message-ID: <em**************@tk2msftngp13.phx.gbl>
Newsgroups: microsoft.public.dotnet.framework.aspnet
NNTP-Posting-Host: 190.muaa.sttl.sttwa01r1.dsl.att.net 12.102.48.190
Path:

cpmsftngxa07.phx.gbl!cpmsftngxa10.phx.gbl!TK2MSFTN GXA05.phx.gbl!TK2MSFTNGP08 phx.gbl!tk2msftngp13.phx.gbl
Xref: cpmsftngxa07.phx.gbl microsoft.public.dotnet.framework.aspnet:191923X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet

This is probably a commonly asked question, but I can't find a good way tosearch for it, so I will ask here.

I assumed that there had to be an easy way to access a page level variablefrom a user control.
IE. default.aspx has a variable "gName"
default.aspx has a user control, "Navigation"
I want to be able to access gName inside "Navigation"'s page_load

This appears to not be possible.... and it makes sense looking at the
structure.... but how are we expected to share data across classes?
Weird dumb question, but I don't get it. I can think of work arounds,
sessions, etc. But that seems like a rough solution

Hope this makes sense to someone,
Steve

Nov 18 '05 #3
First, a Page is a class (of course), so in order to make a "page level
variable" (which would be a Field or Property of the class) available to
another class inside that Page, it must be declared as Public, and appear
outside of any Method definitions. Then it's simply a matter of referencing
the Page class and the field or property name. Example:

Dim str As String = Page.StringFieldName

--
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.

"Steve Klett" <sk************@yahoo.com> wrote in message
news:em**************@tk2msftngp13.phx.gbl...
This is probably a commonly asked question, but I can't find a good way to
search for it, so I will ask here.

I assumed that there had to be an easy way to access a page level variable
from a user control.
IE. default.aspx has a variable "gName"
default.aspx has a user control, "Navigation"
I want to be able to access gName inside "Navigation"'s page_load

This appears to not be possible.... and it makes sense looking at the
structure.... but how are we expected to share data across classes?
Weird dumb question, but I don't get it. I can think of work arounds,
sessions, etc. But that seems like a rough solution

Hope this makes sense to someone,
Steve

Nov 18 '05 #4
Steve,

Here is a segment of my Webform class showing the property:

public class WebForm1 : System.Web.UI.Page
{
public string testProp
{
get
{
return "Got it!";
}
}

private void Page_Load(object sender, System.EventArgs e)
{
// Some stuff in here...
}
}
Now here's the User Control code:

public abstract class WebUserControl1 : System.Web.UI.UserControl
{
protected System.Web.UI.WebControls.Label Label1;

private void Page_Load(object sender, System.EventArgs e)
{
string s = ((WebForm1)this.Page).testProp.ToString();
Label1.Text = s;
}
}
Hope that helps.

Jim Cheshire, MCSE, MCSD [MSFT]
Developer Support
ASP.NET
ja******@online.microsoft.com

This post is provided as-is with no warranties and confers no rights.

--------------------
From: "Steve Klett" <sk************@yahoo.com>
References: <em**************@tk2msftngp13.phx.gbl> <r0**************@cpmsftngxa07.phx.gbl>Subject: Re: global variables question
Date: Fri, 21 Nov 2003 10:44:26 -0800
Lines: 66
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
Message-ID: <#J**************@TK2MSFTNGP12.phx.gbl>
Newsgroups: microsoft.public.dotnet.framework.aspnet
NNTP-Posting-Host: 190.muaa.sttl.sttwa01r1.dsl.att.net 12.102.48.190
Path: cpmsftngxa07.phx.gbl!cpmsftngxa06.phx.gbl!TK2MSFTN GP08.phx.gbl!TK2MSFTNGP12.
phx.gblXref: cpmsftngxa07.phx.gbl microsoft.public.dotnet.framework.aspnet:191939
X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet

hey Jim,

C#. Eager to see what have...

-SK
"Jim Cheshire [MSFT]" <ja******@online.microsoft.com> wrote in message
news:r0**************@cpmsftngxa07.phx.gbl...
Steve,

Not sure exactly what you're doing, but this is very doable. What

language
are you using and I will give you a code sample.

Jim Cheshire, MCSE, MCSD [MSFT]
Developer Support
ASP.NET
ja******@online.microsoft.com

This post is provided as-is with no warranties and confers no rights.

--------------------
>From: "Steve Klett" <sk************@yahoo.com>
>Subject: global variables question
>Date: Fri, 21 Nov 2003 09:48:31 -0800
>Lines: 18
>X-Priority: 3
>X-MSMail-Priority: Normal
>X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
>X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
>Message-ID: <em**************@tk2msftngp13.phx.gbl>
>Newsgroups: microsoft.public.dotnet.framework.aspnet
>NNTP-Posting-Host: 190.muaa.sttl.sttwa01r1.dsl.att.net 12.102.48.190
>Path:

cpmsftngxa07.phx.gbl!cpmsftngxa10.phx.gbl!TK2MSFT NGXA05.phx.gbl!TK2MSFTNGP0

8
phx.gbl!tk2msftngp13.phx.gbl
>Xref: cpmsftngxa07.phx.gblmicrosoft.public.dotnet.framework.aspnet:191923 >X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
>
>This is probably a commonly asked question, but I can't find a good wayto >search for it, so I will ask here.
>
>I assumed that there had to be an easy way to access a page levelvariable >from a user control.
>IE. default.aspx has a variable "gName"
>default.aspx has a user control, "Navigation"
>I want to be able to access gName inside "Navigation"'s page_load
>
>This appears to not be possible.... and it makes sense looking at the
>structure.... but how are we expected to share data across classes?
>Weird dumb question, but I don't get it. I can think of work arounds,
>sessions, etc. But that seems like a rough solution
>
>Hope this makes sense to someone,
>Steve
>
>
>



Nov 18 '05 #5
this is cool, I just wasn't thinking about it correct I guess.
Thanks a lot for the example! ;)

-Steve
"Jim Cheshire [MSFT]" <ja******@online.microsoft.com> wrote in message
news:dB**************@cpmsftngxa07.phx.gbl...
Steve,

Here is a segment of my Webform class showing the property:

public class WebForm1 : System.Web.UI.Page
{
public string testProp
{
get
{
return "Got it!";
}
}

private void Page_Load(object sender, System.EventArgs e)
{
// Some stuff in here...
}
}
Now here's the User Control code:

public abstract class WebUserControl1 : System.Web.UI.UserControl
{
protected System.Web.UI.WebControls.Label Label1;

private void Page_Load(object sender, System.EventArgs e)
{
string s = ((WebForm1)this.Page).testProp.ToString();
Label1.Text = s;
}
}
Hope that helps.

Jim Cheshire, MCSE, MCSD [MSFT]
Developer Support
ASP.NET
ja******@online.microsoft.com

This post is provided as-is with no warranties and confers no rights.

--------------------
From: "Steve Klett" <sk************@yahoo.com>
References: <em**************@tk2msftngp13.phx.gbl> <r0**************@cpmsftngxa07.phx.gbl>
Subject: Re: global variables question
Date: Fri, 21 Nov 2003 10:44:26 -0800
Lines: 66
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
Message-ID: <#J**************@TK2MSFTNGP12.phx.gbl>
Newsgroups: microsoft.public.dotnet.framework.aspnet
NNTP-Posting-Host: 190.muaa.sttl.sttwa01r1.dsl.att.net 12.102.48.190
Path:

cpmsftngxa07.phx.gbl!cpmsftngxa06.phx.gbl!TK2MSFTN GP08.phx.gbl!TK2MSFTNGP12. phx.gbl
Xref: cpmsftngxa07.phx.gbl microsoft.public.dotnet.framework.aspnet:191939
X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet

hey Jim,

C#. Eager to see what have...

-SK
"Jim Cheshire [MSFT]" <ja******@online.microsoft.com> wrote in message
news:r0**************@cpmsftngxa07.phx.gbl...
Steve,

Not sure exactly what you're doing, but this is very doable. What

language
are you using and I will give you a code sample.

Jim Cheshire, MCSE, MCSD [MSFT]
Developer Support
ASP.NET
ja******@online.microsoft.com

This post is provided as-is with no warranties and confers no rights.

--------------------
>From: "Steve Klett" <sk************@yahoo.com>
>Subject: global variables question
>Date: Fri, 21 Nov 2003 09:48:31 -0800
>Lines: 18
>X-Priority: 3
>X-MSMail-Priority: Normal
>X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
>X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
>Message-ID: <em**************@tk2msftngp13.phx.gbl>
>Newsgroups: microsoft.public.dotnet.framework.aspnet
>NNTP-Posting-Host: 190.muaa.sttl.sttwa01r1.dsl.att.net 12.102.48.190
>Path:

cpmsftngxa07.phx.gbl!cpmsftngxa10.phx.gbl!TK2MSFT NGXA05.phx.gbl!TK2MSFTNGP0
8 phx.gbl!tk2msftngp13.phx.gbl
>Xref: cpmsftngxa07.phx.gbl

microsoft.public.dotnet.framework.aspnet:191923
>X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
>
>This is probably a commonly asked question, but I can't find a good

wayto
>search for it, so I will ask here.
>
>I assumed that there had to be an easy way to access a page level

variable
>from a user control.
>IE. default.aspx has a variable "gName"
>default.aspx has a user control, "Navigation"
>I want to be able to access gName inside "Navigation"'s page_load
>
>This appears to not be possible.... and it makes sense looking at the
>structure.... but how are we expected to share data across classes?
>Weird dumb question, but I don't get it. I can think of work arounds,
>sessions, etc. But that seems like a rough solution
>
>Hope this makes sense to someone,
>Steve
>
>
>


Nov 18 '05 #6
OK, so we have access to Page members from inside user control classes. I
must have screwed up or been confused on something, cause that makes sense.
Thanks for the explanation!

-Steve
"Kevin Spencer" <ke***@takempis.com> wrote in message
news:%2***************@TK2MSFTNGP09.phx.gbl...
First, a Page is a class (of course), so in order to make a "page level
variable" (which would be a Field or Property of the class) available to
another class inside that Page, it must be declared as Public, and appear
outside of any Method definitions. Then it's simply a matter of referencing the Page class and the field or property name. Example:

Dim str As String = Page.StringFieldName

--
Kevin Spencer
.Net Developer
Microsoft MVP
Big things are made up
of lots of little things.

"Steve Klett" <sk************@yahoo.com> wrote in message
news:em**************@tk2msftngp13.phx.gbl...
This is probably a commonly asked question, but I can't find a good way to search for it, so I will ask here.

I assumed that there had to be an easy way to access a page level variable from a user control.
IE. default.aspx has a variable "gName"
default.aspx has a user control, "Navigation"
I want to be able to access gName inside "Navigation"'s page_load

This appears to not be possible.... and it makes sense looking at the
structure.... but how are we expected to share data across classes?
Weird dumb question, but I don't get it. I can think of work arounds,
sessions, etc. But that seems like a rough solution

Hope this makes sense to someone,
Steve


Nov 18 '05 #7
Well, I'm still having trouble.

In my page class, I have defined a public string name "llocation";

inside a user control Page_Load event I tried to access the member with
Page.location

I get compiler errors.
C:\Inetpub\wwwroot\NTSdirect_1\controls\Support_Op tions.ascx.cs(23):
'System.Web.UI.Page' does not contain a definition for 'location'

I'm still missing how this should be possible.
-SK
"Kevin Spencer" <ke***@takempis.com> wrote in message
news:%2***************@TK2MSFTNGP09.phx.gbl...
First, a Page is a class (of course), so in order to make a "page level
variable" (which would be a Field or Property of the class) available to
another class inside that Page, it must be declared as Public, and appear
outside of any Method definitions. Then it's simply a matter of referencing the Page class and the field or property name. Example:

Dim str As String = Page.StringFieldName

--
Kevin Spencer
.Net Developer
Microsoft MVP
Big things are made up
of lots of little things.

"Steve Klett" <sk************@yahoo.com> wrote in message
news:em**************@tk2msftngp13.phx.gbl...
This is probably a commonly asked question, but I can't find a good way to search for it, so I will ask here.

I assumed that there had to be an easy way to access a page level variable from a user control.
IE. default.aspx has a variable "gName"
default.aspx has a user control, "Navigation"
I want to be able to access gName inside "Navigation"'s page_load

This appears to not be possible.... and it makes sense looking at the
structure.... but how are we expected to share data across classes?
Weird dumb question, but I don't get it. I can think of work arounds,
sessions, etc. But that seems like a rough solution

Hope this makes sense to someone,
Steve


Nov 18 '05 #8
No problem, Steve.

Jim Cheshire, MCSE, MCSD [MSFT]
Developer Support
ASP.NET
ja******@online.microsoft.com

This post is provided as-is with no warranties and confers no rights.

--------------------
From: "Steve Klett" <sk************@yahoo.com>
References: <em**************@tk2msftngp13.phx.gbl> <r0**************@cpmsftngxa07.phx.gbl>
<#J**************@TK2MSFTNGP12.phx.gbl>
<dB**************@cpmsftngxa07.phx.gbl>Subject: Re: global variables question
Date: Fri, 21 Nov 2003 14:35:21 -0800
Lines: 145
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
Message-ID: <eb**************@TK2MSFTNGP09.phx.gbl>
Newsgroups: microsoft.public.dotnet.framework.aspnet
NNTP-Posting-Host: 190.muaa.sttl.sttwa01r1.dsl.att.net 12.102.48.190
Path: cpmsftngxa07.phx.gbl!cpmsftngxa10.phx.gbl!cpmsftng xa06.phx.gbl!cpmsftngxa09.
phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP09.phx.gblXref: cpmsftngxa07.phx.gbl microsoft.public.dotnet.framework.aspnet:192001
X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet

this is cool, I just wasn't thinking about it correct I guess.
Thanks a lot for the example! ;)

-Steve
"Jim Cheshire [MSFT]" <ja******@online.microsoft.com> wrote in message
news:dB**************@cpmsftngxa07.phx.gbl...
Steve,

Here is a segment of my Webform class showing the property:

public class WebForm1 : System.Web.UI.Page
{
public string testProp
{
get
{
return "Got it!";
}
}

private void Page_Load(object sender, System.EventArgs e)
{
// Some stuff in here...
}
}
Now here's the User Control code:

public abstract class WebUserControl1 : System.Web.UI.UserControl
{
protected System.Web.UI.WebControls.Label Label1;

private void Page_Load(object sender, System.EventArgs e)
{
string s = ((WebForm1)this.Page).testProp.ToString();
Label1.Text = s;
}
}
Hope that helps.

Jim Cheshire, MCSE, MCSD [MSFT]
Developer Support
ASP.NET
ja******@online.microsoft.com

This post is provided as-is with no warranties and confers no rights.

--------------------
>From: "Steve Klett" <sk************@yahoo.com>
>References: <em**************@tk2msftngp13.phx.gbl>

<r0**************@cpmsftngxa07.phx.gbl>
>Subject: Re: global variables question
>Date: Fri, 21 Nov 2003 10:44:26 -0800
>Lines: 66
>X-Priority: 3
>X-MSMail-Priority: Normal
>X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
>X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
>Message-ID: <#J**************@TK2MSFTNGP12.phx.gbl>
>Newsgroups: microsoft.public.dotnet.framework.aspnet
>NNTP-Posting-Host: 190.muaa.sttl.sttwa01r1.dsl.att.net 12.102.48.190
>Path:

cpmsftngxa07.phx.gbl!cpmsftngxa06.phx.gbl!TK2MSFT NGP08.phx.gbl!TK2MSFTNGP12

Nov 18 '05 #9

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

Similar topics

8
by: Dan | last post by:
Quick question about passing variables to subs to reduce the need for publicly declared variables in VB6. If I have an event sub (MouseDown) that runs a few lines of code, how can I use a variable...
3
by: sarmin kho | last post by:
Hi Pythoners, i have been using a lot of global variables in the python script i am working on. the global variables are shared and used by all various 'definitions' : def name (): global all...
9
by: Tony Johansson | last post by:
Hello! I know it's bad design to use global variables. I just want to ask a question about them. Is global variables and global static variables the same. These are define outside any...
35
by: whisper | last post by:
My question is whether it is better to use a global variable to hold a dynamically malloced multidim array or pass around pointers to it. The details are below (forgive the long winded explanation)...
24
by: LP | last post by:
After a code review one coworker insisted that global are very dangerous. He didn't really give any solid reasons other than, "performance penalties", "hard to maintain", and "dangerous". I think...
44
by: fabio | last post by:
Why? i' ve heard about this, the usage of global vars instead of locals is discouraged, but why? thx :)
18
by: robert | last post by:
Using global variables in Python often raises chaos. Other languages use a clear prefix for globals. * you forget to declare a global * or you declare a global too much or in conflict * you...
37
by: eoindeb | last post by:
Sorry to ask another global variable question, but from reading other posts I'm still not sure whether to use them or not. I have a program with a set function that calls 4 other functions in...
1
weaknessforcats
by: weaknessforcats | last post by:
C++: The Case Against Global Variables Summary This article explores the negative ramifications of using global variables. The use of global variables is such a problem that C++ architects have...
6
by: lazy | last post by:
hi, I have some constants defined in a php script say config.php. I want to use the variables there defined in other scripts. couple of questions regd that: 1. Is there an alternative to...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.