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

User Control; read value from hosting aspx code-behind

P: n/a
I have a number of aspx pages on which a single user control appears. All of
the aspx pages and the user control make user of code-behind modules. I need
for logic in the user control's code-behind to read the current value of a
variable in the hosting aspx page's code-behind. How is this accomplished?

Thanks.
Nov 18 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
From your user control code-behind you can use code similar to this:

If Page.SomePublicProperty = "Whatever" Then ...

--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://Steve.Orr.net
"Gene" <Mr******@jw.com> wrote in message
news:O5**************@TK2MSFTNGP11.phx.gbl...
I have a number of aspx pages on which a single user control appears. All of the aspx pages and the user control make user of code-behind modules. I need for logic in the user control's code-behind to read the current value of a
variable in the hosting aspx page's code-behind. How is this accomplished?

Thanks.

Nov 18 '05 #2

P: n/a
On the page make sure it's public

From the control, something like
string myVar = this.ParentPage.variableName.ToString();

--
Curt Christianson
Owner/Lead Developer, DF-Software
www.Darkfalz.com
"Gene" <Mr******@jw.com> wrote in message
news:O5**************@TK2MSFTNGP11.phx.gbl...
I have a number of aspx pages on which a single user control appears. All of the aspx pages and the user control make user of code-behind modules. I need for logic in the user control's code-behind to read the current value of a
variable in the hosting aspx page's code-behind. How is this accomplished?

Thanks.

Nov 18 '05 #3

P: n/a
Thanks Curt and Steve: Now a related question: I set the public variable
from the aspx page_load event procedure, and want to I read that public
variable from the page_load event procedure of the user control. In order
for this to work as expected, I need for the page_load event of the aspx to
fire *before* the page_load of the user control. Is that a safe assumption?

Thanks again.

"Curt_C [MVP]" <software_AT_darkfalz.com> wrote in message
news:e7**************@TK2MSFTNGP09.phx.gbl...
On the page make sure it's public

From the control, something like
string myVar = this.ParentPage.variableName.ToString();

--
Curt Christianson
Owner/Lead Developer, DF-Software
www.Darkfalz.com
"Gene" <Mr******@jw.com> wrote in message
news:O5**************@TK2MSFTNGP11.phx.gbl...
I have a number of aspx pages on which a single user control appears. All
of
the aspx pages and the user control make user of code-behind modules. I

need
for logic in the user control's code-behind to read the current value of

a variable in the hosting aspx page's code-behind. How is this accomplished?
Thanks.


Nov 18 '05 #4

P: n/a
put in a break point and test it to see.
You may need to move it up to the intialize event though

--
Curt Christianson
Owner/Lead Developer, DF-Software
www.Darkfalz.com
"Gene" <Mr******@jw.com> wrote in message
news:OT**************@tk2msftngp13.phx.gbl...
Thanks Curt and Steve: Now a related question: I set the public variable
from the aspx page_load event procedure, and want to I read that public
variable from the page_load event procedure of the user control. In order
for this to work as expected, I need for the page_load event of the aspx to fire *before* the page_load of the user control. Is that a safe assumption?
Thanks again.

"Curt_C [MVP]" <software_AT_darkfalz.com> wrote in message
news:e7**************@TK2MSFTNGP09.phx.gbl...
On the page make sure it's public

From the control, something like
string myVar = this.ParentPage.variableName.ToString();

--
Curt Christianson
Owner/Lead Developer, DF-Software
www.Darkfalz.com
"Gene" <Mr******@jw.com> wrote in message
news:O5**************@TK2MSFTNGP11.phx.gbl...
I have a number of aspx pages on which a single user control appears. All
of
the aspx pages and the user control make user of code-behind modules.
I need
for logic in the user control's code-behind to read the current value
of a variable in the hosting aspx page's code-behind. How is this accomplished?
Thanks.



Nov 18 '05 #5

P: n/a
I've tried the initial task using the syntax both you and Steve recommended,
however, either way I get compile-time errors, the message of which is
something like:
'System.Web.UI.Page' does not contain a definition for 'myPublicVariable'

It makes sense that such compile-time errors would occur, as the user
control - at compile time - does not know which aspx page will be hosting
it, and the compiler therefore cannot know where to look to see if the
public property exists. Am I confused? Is it possible to do this without
getting the compile-time error and I'm just missing something.

Thanks.


"Curt_C [MVP]" <software_AT_darkfalz.com> wrote in message
news:ug**************@TK2MSFTNGP11.phx.gbl...
put in a break point and test it to see.
You may need to move it up to the intialize event though

--
Curt Christianson
Owner/Lead Developer, DF-Software
www.Darkfalz.com
"Gene" <Mr******@jw.com> wrote in message
news:OT**************@tk2msftngp13.phx.gbl...
Thanks Curt and Steve: Now a related question: I set the public variable
from the aspx page_load event procedure, and want to I read that public
variable from the page_load event procedure of the user control. In order
for this to work as expected, I need for the page_load event of the aspx to
fire *before* the page_load of the user control. Is that a safe

assumption?

Thanks again.

"Curt_C [MVP]" <software_AT_darkfalz.com> wrote in message
news:e7**************@TK2MSFTNGP09.phx.gbl...
On the page make sure it's public

From the control, something like
string myVar = this.ParentPage.variableName.ToString();

--
Curt Christianson
Owner/Lead Developer, DF-Software
www.Darkfalz.com
"Gene" <Mr******@jw.com> wrote in message
news:O5**************@TK2MSFTNGP11.phx.gbl...
> I have a number of aspx pages on which a single user control
appears. All
of
> the aspx pages and the user control make user of code-behind

modules. I need
> for logic in the user control's code-behind to read the current
value of
a
> variable in the hosting aspx page's code-behind. How is this

accomplished?
>
> Thanks.
>
>



Nov 18 '05 #6

P: n/a
newVar = ((YourPage)Page).publicVariableName.ToStirng();
--
Curt Christianson
Owner/Lead Developer, DF-Software
www.Darkfalz.com
"Gene" <Mr******@jw.com> wrote in message
news:%2*****************@tk2msftngp13.phx.gbl...
I've tried the initial task using the syntax both you and Steve recommended, however, either way I get compile-time errors, the message of which is
something like:
'System.Web.UI.Page' does not contain a definition for 'myPublicVariable'

It makes sense that such compile-time errors would occur, as the user
control - at compile time - does not know which aspx page will be hosting
it, and the compiler therefore cannot know where to look to see if the
public property exists. Am I confused? Is it possible to do this without
getting the compile-time error and I'm just missing something.

Thanks.


"Curt_C [MVP]" <software_AT_darkfalz.com> wrote in message
news:ug**************@TK2MSFTNGP11.phx.gbl...
put in a break point and test it to see.
You may need to move it up to the intialize event though

--
Curt Christianson
Owner/Lead Developer, DF-Software
www.Darkfalz.com
"Gene" <Mr******@jw.com> wrote in message
news:OT**************@tk2msftngp13.phx.gbl...
Thanks Curt and Steve: Now a related question: I set the public variable from the aspx page_load event procedure, and want to I read that public variable from the page_load event procedure of the user control. In order for this to work as expected, I need for the page_load event of the
aspx
to
fire *before* the page_load of the user control. Is that a safe

assumption?

Thanks again.

"Curt_C [MVP]" <software_AT_darkfalz.com> wrote in message
news:e7**************@TK2MSFTNGP09.phx.gbl...
> On the page make sure it's public
>
> From the control, something like
> string myVar = this.ParentPage.variableName.ToString();
>
> --
> Curt Christianson
> Owner/Lead Developer, DF-Software
> www.Darkfalz.com
>
>
> "Gene" <Mr******@jw.com> wrote in message
> news:O5**************@TK2MSFTNGP11.phx.gbl...
> > I have a number of aspx pages on which a single user control

appears. All
> of
> > the aspx pages and the user control make user of code-behind

modules.
I
> need
> > for logic in the user control's code-behind to read the current

value
of
a
> > variable in the hosting aspx page's code-behind. How is this
accomplished?
> >
> > Thanks.
> >
> >
>
>



Nov 18 '05 #7

P: n/a
Hello Gene,
see if the public property exists. Am I confused? Is it possible to do
this without getting the compile-time error and I'm just missing
something.


Derive your own page class.

Ex:

public class MyPage : System.Web.UI.Page
{
public string myPublicVariable; // Note: I would never make this as a field in production code. This is just to illustrate.
}

From your user control:

MyPage myPage = (MyPage)this.Page;
string s = myPage.myPublicVariable;

--
Matt Berther
http://www.mattberther.com
Nov 18 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.