By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,386 Members | 1,727 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,386 IT Pros & Developers. It's quick & easy.

global variables question

P: n/a
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
Share this Question
Share on Google+
8 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.