Access pages base class properties from User Control

Discussion in 'ASP .Net' started by Mikip, Jan 18, 2004.

  1. Mikip

    Mikip Guest

    Hi,

    I have some properties that are common to all pages so I have created a base
    class which every page inherits from. I now also want to use these
    properties within User Controls on the page. Is there any way to get to
    these properties through the parent property of the user control - I can't
    seem to figure out how to get there. If I can't do this, do I have to do the
    same thing as the page and create a base user control class with the same
    code in - Seems silly to have to execute the same code twice on a page?

    Thanks in advance for any help

    Mike
     
    Mikip, Jan 18, 2004
    #1
    1. Advertising

  2. Mikip

    Anatoly Guest

    UserControl.Page return reference to Page object contains this control.
    Since you use another class for your page you must cast this property to
    your page class:
    For example
    if your base page class named MyBasePage

    ((MyBasePage)MyUserControl.Page).[my base page property/method]

    HTH

    "Mikip" <> wrote in message
    news:%...
    > Hi,
    >
    > I have some properties that are common to all pages so I have created a

    base
    > class which every page inherits from. I now also want to use these
    > properties within User Controls on the page. Is there any way to get to
    > these properties through the parent property of the user control - I can't
    > seem to figure out how to get there. If I can't do this, do I have to do

    the
    > same thing as the page and create a base user control class with the same
    > code in - Seems silly to have to execute the same code twice on a page?
    >
    > Thanks in advance for any help
    >
    > Mike
    >
    >
     
    Anatoly, Jan 18, 2004
    #2
    1. Advertising

  3. Mikip

    Mikip Guest

    Anatoly,
    Thanks for the reply...yes, if i maybe would have used a few more of my
    limited neurons, I would have seen this! Incidently, do you think its better
    to declare a private field in the UserControl class, do the cast once
    assigning the base property to this field and use the private field within
    the UserControl class. Will it save some performance not having to make the
    cast everytime I want to use a base class property.?

    Thx

    Mike
    "Anatoly" <> wrote in message
    news:...
    > UserControl.Page return reference to Page object contains this control.
    > Since you use another class for your page you must cast this property to
    > your page class:
    > For example
    > if your base page class named MyBasePage
    >
    > ((MyBasePage)MyUserControl.Page).[my base page property/method]
    >
    > HTH
    >
    > "Mikip" <> wrote in message
    > news:%...
    > > Hi,
    > >
    > > I have some properties that are common to all pages so I have created a

    > base
    > > class which every page inherits from. I now also want to use these
    > > properties within User Controls on the page. Is there any way to get to
    > > these properties through the parent property of the user control - I

    can't
    > > seem to figure out how to get there. If I can't do this, do I have to do

    > the
    > > same thing as the page and create a base user control class with the

    same
    > > code in - Seems silly to have to execute the same code twice on a page?
    > >
    > > Thanks in advance for any help
    > >
    > > Mike
    > >
    > >

    >
    >
     
    Mikip, Jan 18, 2004
    #3
  4. Mikip

    Shiv Kumar Guest

    Mike

    In reality, this smells of bad design since your UserControl now is aware of
    your Base Page Class and "talks" to it. Of course, if your UserControl and
    base page are specific to this application then its not too bad.

    Typically UserControls (like other controls) should be agnostic of the
    specific page.

    --
    Shiv R. Kumar
    http://www.matlus.com
     
    Shiv Kumar, Jan 18, 2004
    #4
    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. oliviers
    Replies:
    3
    Views:
    697
    oliviers
    Feb 15, 2007
  2. Alf P. Steinbach
    Replies:
    6
    Views:
    573
    John Carson
    Sep 3, 2005
  3. Max2006
    Replies:
    2
    Views:
    538
    Max2006
    Jul 13, 2007
  4. Hicham Mouline
    Replies:
    1
    Views:
    629
    Victor Bazarov
    Apr 20, 2009
  5. Karan Rajput
    Replies:
    2
    Views:
    156
    Abinoam Jr.
    Dec 22, 2010
Loading...

Share This Page