User Control; read value from hosting aspx code-behind

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Gene

    User Control; read value from hosting aspx code-behind

    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]

    #2
    Re: User Control; read value from hosting aspx code-behind

    From your user control code-behind you can use code similar to this:

    If Page.SomePublic Property = "Whatever" Then ...

    --
    I hope this helps,
    Steve C. Orr, MCSD, MVP



    "Gene" <MrWilder@jw.co m> wrote in message
    news:O5uVJojJEH A.2680@TK2MSFTN GP11.phx.gbl...[color=blue]
    > I have a number of aspx pages on which a single user control appears. All[/color]
    of[color=blue]
    > the aspx pages and the user control make user of code-behind modules. I[/color]
    need[color=blue]
    > 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.
    >
    >[/color]


    Comment

    • Curt_C [MVP]

      #3
      Re: User Control; read value from hosting aspx code-behind

      On the page make sure it's public

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

      --
      Curt Christianson
      Owner/Lead Developer, DF-Software



      "Gene" <MrWilder@jw.co m> wrote in message
      news:O5uVJojJEH A.2680@TK2MSFTN GP11.phx.gbl...[color=blue]
      > I have a number of aspx pages on which a single user control appears. All[/color]
      of[color=blue]
      > the aspx pages and the user control make user of code-behind modules. I[/color]
      need[color=blue]
      > 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.
      >
      >[/color]


      Comment

      • Gene

        #4
        Re: User Control; read value from hosting aspx code-behind

        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_da rkfalz.com> wrote in message
        news:e7jCGsjJEH A.1388@TK2MSFTN GP09.phx.gbl...[color=blue]
        > On the page make sure it's public
        >
        > From the control, something like
        > string myVar = this.ParentPage .variableName.T oString();
        >
        > --
        > Curt Christianson
        > Owner/Lead Developer, DF-Software
        > www.Darkfalz.com
        >
        >
        > "Gene" <MrWilder@jw.co m> wrote in message
        > news:O5uVJojJEH A.2680@TK2MSFTN GP11.phx.gbl...[color=green]
        > > I have a number of aspx pages on which a single user control appears.[/color][/color]
        All[color=blue]
        > of[color=green]
        > > the aspx pages and the user control make user of code-behind modules. I[/color]
        > need[color=green]
        > > for logic in the user control's code-behind to read the current value of[/color][/color]
        a[color=blue][color=green]
        > > variable in the hosting aspx page's code-behind. How is this[/color][/color]
        accomplished?[color=blue][color=green]
        > >
        > > Thanks.
        > >
        > >[/color]
        >
        >[/color]


        Comment

        • Curt_C [MVP]

          #5
          Re: User Control; read value from hosting aspx code-behind

          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



          "Gene" <MrWilder@jw.co m> wrote in message
          news:OTTctyjJEH A.2244@tk2msftn gp13.phx.gbl...[color=blue]
          > 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[/color]
          to[color=blue]
          > fire *before* the page_load of the user control. Is that a safe[/color]
          assumption?[color=blue]
          >
          > Thanks again.
          >
          >
          >
          > "Curt_C [MVP]" <software_AT_da rkfalz.com> wrote in message
          > news:e7jCGsjJEH A.1388@TK2MSFTN GP09.phx.gbl...[color=green]
          > > On the page make sure it's public
          > >
          > > From the control, something like
          > > string myVar = this.ParentPage .variableName.T oString();
          > >
          > > --
          > > Curt Christianson
          > > Owner/Lead Developer, DF-Software
          > > www.Darkfalz.com
          > >
          > >
          > > "Gene" <MrWilder@jw.co m> wrote in message
          > > news:O5uVJojJEH A.2680@TK2MSFTN GP11.phx.gbl...[color=darkred]
          > > > I have a number of aspx pages on which a single user control appears.[/color][/color]
          > All[color=green]
          > > of[color=darkred]
          > > > the aspx pages and the user control make user of code-behind modules.[/color][/color][/color]
          I[color=blue][color=green]
          > > need[color=darkred]
          > > > for logic in the user control's code-behind to read the current value[/color][/color][/color]
          of[color=blue]
          > a[color=green][color=darkred]
          > > > variable in the hosting aspx page's code-behind. How is this[/color][/color]
          > accomplished?[color=green][color=darkred]
          > > >
          > > > Thanks.
          > > >
          > > >[/color]
          > >
          > >[/color]
          >
          >[/color]


          Comment

          • Gene

            #6
            Re: User Control; read value from hosting aspx code-behind

            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 'myPublicVariab le'

            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_da rkfalz.com> wrote in message
            news:ugtrF3jJEH A.4052@TK2MSFTN GP11.phx.gbl...[color=blue]
            > 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" <MrWilder@jw.co m> wrote in message
            > news:OTTctyjJEH A.2244@tk2msftn gp13.phx.gbl...[color=green]
            > > 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[/color][/color]
            order[color=blue][color=green]
            > > for this to work as expected, I need for the page_load event of the aspx[/color]
            > to[color=green]
            > > fire *before* the page_load of the user control. Is that a safe[/color]
            > assumption?[color=green]
            > >
            > > Thanks again.
            > >
            > >
            > >
            > > "Curt_C [MVP]" <software_AT_da rkfalz.com> wrote in message
            > > news:e7jCGsjJEH A.1388@TK2MSFTN GP09.phx.gbl...[color=darkred]
            > > > On the page make sure it's public
            > > >
            > > > From the control, something like
            > > > string myVar = this.ParentPage .variableName.T oString();
            > > >
            > > > --
            > > > Curt Christianson
            > > > Owner/Lead Developer, DF-Software
            > > > www.Darkfalz.com
            > > >
            > > >
            > > > "Gene" <MrWilder@jw.co m> wrote in message
            > > > news:O5uVJojJEH A.2680@TK2MSFTN GP11.phx.gbl...
            > > > > I have a number of aspx pages on which a single user control[/color][/color][/color]
            appears.[color=blue][color=green]
            > > All[color=darkred]
            > > > of
            > > > > the aspx pages and the user control make user of code-behind[/color][/color][/color]
            modules.[color=blue]
            > I[color=green][color=darkred]
            > > > need
            > > > > for logic in the user control's code-behind to read the current[/color][/color][/color]
            value[color=blue]
            > of[color=green]
            > > a[color=darkred]
            > > > > variable in the hosting aspx page's code-behind. How is this[/color]
            > > accomplished?[color=darkred]
            > > > >
            > > > > Thanks.
            > > > >
            > > > >
            > > >
            > > >[/color]
            > >
            > >[/color]
            >
            >[/color]


            Comment

            • Curt_C [MVP]

              #7
              Re: User Control; read value from hosting aspx code-behind

              newVar = ((YourPage)Page ).publicVariabl eName.ToStirng( );


              --
              Curt Christianson
              Owner/Lead Developer, DF-Software



              "Gene" <MrWilder@jw.co m> wrote in message
              news:%23D5pr%23 jJEHA.208@tk2ms ftngp13.phx.gbl ...[color=blue]
              > I've tried the initial task using the syntax both you and Steve[/color]
              recommended,[color=blue]
              > 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 'myPublicVariab le'
              >
              > 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_da rkfalz.com> wrote in message
              > news:ugtrF3jJEH A.4052@TK2MSFTN GP11.phx.gbl...[color=green]
              > > 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" <MrWilder@jw.co m> wrote in message
              > > news:OTTctyjJEH A.2244@tk2msftn gp13.phx.gbl...[color=darkred]
              > > > Thanks Curt and Steve: Now a related question: I set the public[/color][/color][/color]
              variable[color=blue][color=green][color=darkred]
              > > > from the aspx page_load event procedure, and want to I read that[/color][/color][/color]
              public[color=blue][color=green][color=darkred]
              > > > variable from the page_load event procedure of the user control. In[/color][/color]
              > order[color=green][color=darkred]
              > > > for this to work as expected, I need for the page_load event of the[/color][/color][/color]
              aspx[color=blue][color=green]
              > > to[color=darkred]
              > > > fire *before* the page_load of the user control. Is that a safe[/color]
              > > assumption?[color=darkred]
              > > >
              > > > Thanks again.
              > > >
              > > >
              > > >
              > > > "Curt_C [MVP]" <software_AT_da rkfalz.com> wrote in message
              > > > news:e7jCGsjJEH A.1388@TK2MSFTN GP09.phx.gbl...
              > > > > On the page make sure it's public
              > > > >
              > > > > From the control, something like
              > > > > string myVar = this.ParentPage .variableName.T oString();
              > > > >
              > > > > --
              > > > > Curt Christianson
              > > > > Owner/Lead Developer, DF-Software
              > > > > www.Darkfalz.com
              > > > >
              > > > >
              > > > > "Gene" <MrWilder@jw.co m> wrote in message
              > > > > news:O5uVJojJEH A.2680@TK2MSFTN GP11.phx.gbl...
              > > > > > I have a number of aspx pages on which a single user control[/color][/color]
              > appears.[color=green][color=darkred]
              > > > All
              > > > > of
              > > > > > the aspx pages and the user control make user of code-behind[/color][/color]
              > modules.[color=green]
              > > I[color=darkred]
              > > > > need
              > > > > > for logic in the user control's code-behind to read the current[/color][/color]
              > value[color=green]
              > > of[color=darkred]
              > > > a
              > > > > > variable in the hosting aspx page's code-behind. How is this
              > > > accomplished?
              > > > > >
              > > > > > Thanks.
              > > > > >
              > > > > >
              > > > >
              > > > >
              > > >
              > > >[/color]
              > >
              > >[/color]
              >
              >[/color]


              Comment

              • Matt Berther

                #8
                Re: User Control; read value from hosting aspx code-behind

                Hello Gene,
                [color=blue]
                > 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.[/color]

                Derive your own page class.

                Ex:

                public class MyPage : System.Web.UI.P age
                {
                public string myPublicVariabl e; // 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.Pa ge;
                string s = myPage.myPublic Variable;

                --
                Matt Berther
                Musings of an agile development manager and occasional code monkey

                Comment

                Working...