global variables question

  • Thread starter Thread starter Steve Klett
  • Start date Start date
S

Steve Klett

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
 
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
(e-mail address removed)

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

--------------------
 
hey Jim,

C#. Eager to see what have...

-SK


Jim Cheshire said:
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
(e-mail address removed)

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

--------------------
From: "Steve Klett" <[email protected]>
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: <[email protected]>
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!TK2MSFTNGXA05.phx.gbl!TK2MSFTNGP08
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 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
 
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,

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
(e-mail address removed)

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

--------------------
From: "Steve Klett" <[email protected]>
References: <[email protected]>
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#[email protected]>
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!TK2MSFTNGP08.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 said:
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
(e-mail address removed)

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

--------------------
From: "Steve Klett" <[email protected]>
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: <[email protected]>
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!TK2MSFTNGXA05.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 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
 
this is cool, I just wasn't thinking about it correct I guess.
Thanks a lot for the example! ;)

-Steve
Jim Cheshire said:
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
(e-mail address removed)

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

--------------------
From: "Steve Klett" <[email protected]>
References: <[email protected]>
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#[email protected]>
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!TK2MSFTNGP08.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 said:
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
(e-mail address removed)

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

--------------------
From: "Steve Klett" <[email protected]>
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: <[email protected]>
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!TK2MSFTNGXA05.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
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
 
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
 
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_Options.ascx.cs(23):
'System.Web.UI.Page' does not contain a definition for 'location'

I'm still missing how this should be possible.


-SK
 
No problem, Steve.

Jim Cheshire, MCSE, MCSD [MSFT]
Developer Support
ASP.NET
(e-mail address removed)

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

--------------------
From: "Steve Klett" <[email protected]>
References: <[email protected]>
<[email protected]>
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: <[email protected]>
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!cpmsftngxa06.phx.gbl!cpmsftngxa09.
phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP09.phx.gbl
Xref: 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 said:
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
(e-mail address removed)

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

--------------------
From: "Steve Klett" <[email protected]>
References: <[email protected]>
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#[email protected]>
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!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP12
 
Back
Top