Do UserControls have acces to the container?

Discussion in 'ASP .Net Web Controls' started by Jenda Krynicky, Sep 7, 2004.

  1. Sorry I'm probably missing something basic, but can't find it in the docs.

    Is it possible to access the public properties of the page containing the
    UserControl from within the control? Without having to set the link up from
    within the code of the page of course.

    Most of my pages inherit not from System.Web.UI.Page, but from my own class.
    The class has a Public property, an object containing info about the
    currently logged in user. Some of the pages use a UserControl I made. The
    control also needs access to the object (or needs to create its own based on
    the info in Context.User.Identity).

    I know I could add a public property "login" to the control and set it to
    the created object from within the initialization code of the pages, I know
    I could do it by just inheriting another template page from the one I
    already have and inherit the pages that use this UserControl from the new
    template, but I'd rather not. I think the control must have a reference to
    its container already.

    Thanks, Jenda
     
    Jenda Krynicky, Sep 7, 2004
    #1
    1. Advertising

  2. Jenda Krynicky

    Joel Guest

    All web controls have a Page property. Just cast it to your derived type.

    </joel>


    "Jenda Krynicky" <> wrote in message
    news:...
    > Sorry I'm probably missing something basic, but can't find it in the docs.
    >
    > Is it possible to access the public properties of the page containing the
    > UserControl from within the control? Without having to set the link up

    from
    > within the code of the page of course.
    >
    > Most of my pages inherit not from System.Web.UI.Page, but from my own

    class.
    > The class has a Public property, an object containing info about the
    > currently logged in user. Some of the pages use a UserControl I made. The
    > control also needs access to the object (or needs to create its own based

    on
    > the info in Context.User.Identity).
    >
    > I know I could add a public property "login" to the control and set it to
    > the created object from within the initialization code of the pages, I

    know
    > I could do it by just inheriting another template page from the one I
    > already have and inherit the pages that use this UserControl from the new
    > template, but I'd rather not. I think the control must have a reference to
    > its container already.
    >
    > Thanks, Jenda
    >
    >
     
    Joel, Oct 21, 2004
    #2
    1. Advertising

  3. Joel wrote:
    > All web controls have a Page property. Just cast it to your derived type.
    >


    While this is definitely true, it's rarely a good idea to have the User
    Control reference up to its parent container in this manner. Ideally,
    User Controls should be agnostic about their container. This would
    allow them to be plugged in and out of various pages without having to
    muck with the code. By tying the UC to its parent, you destroy any
    hopes at encapsulation and reusability. (Assuming that the UC is
    accessing properties/methods of the parent container that are not
    inherent to the Page class.)

    If it is essential that the UC interacts with the parent page, a better
    approach would be to use an interface or delegates. (Again, assuming
    that the UC is accessing something other than a property/method already
    outlaid in the Page class...)




    > "Jenda Krynicky" <> wrote in message
    > news:...
    >
    >>Sorry I'm probably missing something basic, but can't find it in the docs.
    >>
    >>Is it possible to access the public properties of the page containing the
    >>UserControl from within the control? Without having to set the link up

    >
    > from
    >
    >>within the code of the page of course.
    >>
    >>Most of my pages inherit not from System.Web.UI.Page, but from my own

    >
    > class.
    >
    >>The class has a Public property, an object containing info about the
    >>currently logged in user. Some of the pages use a UserControl I made. The
    >>control also needs access to the object (or needs to create its own based

    >
    > on
    >
    >>the info in Context.User.Identity).
    >>
    >>I know I could add a public property "login" to the control and set it to
    >>the created object from within the initialization code of the pages, I

    >
    > know
    >
    >>I could do it by just inheriting another template page from the one I
    >>already have and inherit the pages that use this UserControl from the new
    >>template, but I'd rather not. I think the control must have a reference to
    >>its container already.



    --

    Scott Mitchell

    http://www.4GuysFromRolla.com

    * When you think ASP.NET, think 4GuysFromRolla.com!
     
    Scott Mitchell [MVP], Oct 22, 2004
    #3
  4. Wessam Zeidan, Oct 22, 2004
    #4
  5. > While this is definitely true, it's rarely a good idea
    > to have the User Control reference up to its parent
    > container in this manner. Ideally, User Controls
    > should be agnostic about their container. This would
    > allow them to be plugged in and out of various pages
    > without having to muck with the code. By tying the
    > UC to its parent, you destroy any hopes at encapsulation
    > and reusability. (Assuming that the UC is accessing
    > properties/methods of the parent container that are
    > not inherent to the Page class.)


    This is all nice and good (and probably learned from some purist book)
    but it does not apply in this case.

    1) All pages except Login.aspx inherit from Projectname.TSPage instead
    from System.Web.UI.Page. And I do not want to use something specific to
    the individual pages, but a public property of the Projectname.TSPage.
    2) Almost all pages in the project except Login.aspx use that user
    control.
    3) The user control is totaly application specific, it would have no
    meaning whatsoever outside the app.

    So thanks for your concern, Jenda


    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
     
    Jenda Krynicky, Oct 22, 2004
    #5
  6. Jenda Krynicky wrote:
    >>While this is definitely true, it's rarely a good idea
    >>to have the User Control reference up to its parent
    >>container in this manner. Ideally, User Controls
    >>should be agnostic about their container. This would
    >>allow them to be plugged in and out of various pages
    >>without having to muck with the code. By tying the
    >>UC to its parent, you destroy any hopes at encapsulation
    >>and reusability. (Assuming that the UC is accessing
    >>properties/methods of the parent container that are
    >>not inherent to the Page class.)

    >
    >
    > This is all nice and good (and probably learned from some purist book)
    > but it does not apply in this case.


    It was learned the ol' fashion way - through experience.

    > 1) All pages except Login.aspx inherit from Projectname.TSPage instead
    > from System.Web.UI.Page. And I do not want to use something specific to
    > the individual pages, but a public property of the Projectname.TSPage.


    As I said, if the data you are referencing is specific to the base class
    or interface of the page, then by all means the UC can go ahead and
    access it. Here, let me reiterate where I said that:

    >>(Assuming that the UC is accessing properties/methods of the
    >>parent container that are not inherent to the Page class.)


    > 3) The user control is totaly application specific, it would have no
    > meaning whatsoever outside the app.


    Components often have a wily way of needing their scope to be widened
    after the fact. All I am saying is that in the future you may want to
    use this UC in another project, only to find that you made some
    design-time decisions that hinder its reusability.

    --

    Scott Mitchell

    http://www.4GuysFromRolla.com

    * When you think ASP.NET, think 4GuysFromRolla.com!
     
    Scott Mitchell [MVP], Oct 24, 2004
    #6
    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. Vivi Orunitia
    Replies:
    11
    Views:
    4,544
    Martijn Lievaart
    Feb 4, 2004
  2. Maitre Bart
    Replies:
    2
    Views:
    550
    Maitre Bart
    Feb 11, 2004
  3. Steven T. Hatton
    Replies:
    4
    Views:
    3,989
    Rob Williscroft
    Dec 5, 2004
  4. Juan T. Llibre
    Replies:
    18
    Views:
    1,884
    mocoder
    Dec 20, 2006
  5. fabrice

    Acces controls on aspx page from usercontrols

    fabrice, May 25, 2005, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    121
Loading...

Share This Page