User Control; read value from hosting aspx code-behind

Discussion in 'ASP .Net' started by Gene, Apr 19, 2004.

  1. Gene

    Gene Guest

    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.
     
    Gene, Apr 19, 2004
    #1
    1. Advertising

  2. 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" <> wrote in message
    news:...
    > 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.
    >
    >
     
    Steve C. Orr [MVP, MCSD], Apr 19, 2004
    #2
    1. Advertising

  3. Gene

    Curt_C [MVP] Guest

    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" <> wrote in message
    news:...
    > 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.
    >
    >
     
    Curt_C [MVP], Apr 19, 2004
    #3
  4. Gene

    Gene Guest

    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:...
    > 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" <> wrote in message
    > news:...
    > > 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.
    > >
    > >

    >
    >
     
    Gene, Apr 19, 2004
    #4
  5. Gene

    Curt_C [MVP] Guest

    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" <> wrote in message
    news:...
    > 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:...
    > > 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" <> wrote in message
    > > news:...
    > > > 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.
    > > >
    > > >

    > >
    > >

    >
    >
     
    Curt_C [MVP], Apr 19, 2004
    #5
  6. Gene

    Gene Guest

    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:...
    > 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" <> wrote in message
    > news:...
    > > 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:...
    > > > 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" <> wrote in message
    > > > news:...
    > > > > 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.
    > > > >
    > > > >
    > > >
    > > >

    > >
    > >

    >
    >
     
    Gene, Apr 19, 2004
    #6
  7. Gene

    Curt_C [MVP] Guest

    newVar = ((YourPage)Page).publicVariableName.ToStirng();


    --
    Curt Christianson
    Owner/Lead Developer, DF-Software
    www.Darkfalz.com


    "Gene" <> wrote in message
    news:%23D5pr%...
    > 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:...
    > > 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" <> wrote in message
    > > news:...
    > > > 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:...
    > > > > 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" <> wrote in message
    > > > > news:...
    > > > > > 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.
    > > > > >
    > > > > >
    > > > >
    > > > >
    > > >
    > > >

    > >
    > >

    >
    >
     
    Curt_C [MVP], Apr 19, 2004
    #7
  8. Gene

    Matt Berther Guest

    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
     
    Matt Berther, Apr 19, 2004
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ben Miller [msft]

    Re: Code Behind vs. no code behind: error

    Ben Miller [msft], Jun 27, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    593
    Alphonse Giambrone
    Jun 28, 2003
  2. Paolo Pignatelli
    Replies:
    6
    Views:
    2,196
    Paolo Pignatelli
    Feb 21, 2005
  3. Charlie@CBFC
    Replies:
    2
    Views:
    2,086
    S. Justin Gengo
    Oct 15, 2005
  4. Jason
    Replies:
    0
    Views:
    125
    Jason
    Oct 6, 2005
  5. teo1991
    Replies:
    0
    Views:
    604
    teo1991
    Apr 2, 2009
Loading...

Share This Page