share common methods and properties for .ASPX and .ASCX Pages

Discussion in 'ASP .Net' started by Garth17, Mar 17, 2005.

  1. Garth17

    Garth17 Guest

    I'm trying to figure out a solution for sharing common properties and
    methods in all me .aspx and .ascx pages.

    In classic ASP I would use include directives.

    So far I have made 2 base classes WebFormBase and UserControlBase. And
    then set all my webforms and usercontrols inherit from the appropriate
    one. But this forces me to replicate my code between to the two base
    classes.

    I'm working with a project that is a mix between Classic ASP and
    ASP.NET. Our session variables are placed into the HTTP-Header from a
    very old ISAPI filter. We retrieve them using
    Request.ServerVariable("HTTP_CUSTOMVAR") Calls.

    For ASP.NET purposes I create a base class property for each of our
    ISAPI session variables. So that inside the WebForm that inherit it
    they are all documented with intellisys like Me.sesPersonID , etc.

    But my problem is that I have to replicate these properties in both of
    the base classes for webforms and usercontrols. I would like to code
    this retrieval only once and have both the UserControlBaseClass and the
    WebFormBaseClass use the same properties.

    This would also be useful for public methods and functions that I want
    to share in both User Controls and Webforms.

    What are my best Options?
     
    Garth17, Mar 17, 2005
    #1
    1. Advertising

  2. Garth17

    Brock Allen Guest

    Why don't you just make a class that represents that data and make several
    statis read-only properties that fetch the data you need from HttpContext.Current.Request?
    This, IMO, is a much better solution than the common base class approach
    taken by so many people when building ASP.NET applications.

    -Brock
    DevelopMentor
    http://staff.develop.com/ballen



    > I'm trying to figure out a solution for sharing common properties and
    > methods in all me .aspx and .ascx pages.
    >
    > In classic ASP I would use include directives.
    >
    > So far I have made 2 base classes WebFormBase and UserControlBase. And
    > then set all my webforms and usercontrols inherit from the appropriate
    > one. But this forces me to replicate my code between to the two base
    > classes.
    >
    > I'm working with a project that is a mix between Classic ASP and
    > ASP.NET. Our session variables are placed into the HTTP-Header from
    > a very old ISAPI filter. We retrieve them using
    > Request.ServerVariable("HTTP_CUSTOMVAR") Calls.
    >
    > For ASP.NET purposes I create a base class property for each of our
    > ISAPI session variables. So that inside the WebForm that inherit it
    > they are all documented with intellisys like Me.sesPersonID , etc.
    >
    > But my problem is that I have to replicate these properties in both of
    > the base classes for webforms and usercontrols. I would like to code
    > this retrieval only once and have both the UserControlBaseClass and
    > the WebFormBaseClass use the same properties.
    >
    > This would also be useful for public methods and functions that I want
    > to share in both User Controls and Webforms.
    >
    > What are my best Options?
    >
     
    Brock Allen, Mar 17, 2005
    #2
    1. Advertising

  3. Garth17

    Garth17 Guest

    I see your point. I just made a custom MySession Class.

    And now I instantiate it inside both the base classes as a property.
    So now on the webforms if I want to get at session data I could make a
    call to Me.mySession.sesPersonID. mySession being the Public property
    of the base class that returns a MySession object instantiated from my
    new MySession.vb class.

    This does solve the code duplication problem.


    Is that elegant way of handling this?

    Or should I not be exposing this class as property of a base class?
    And instead just instantiate the MySession class in all my Webforms and
    ASCX pages separately?
     
    Garth17, Mar 18, 2005
    #3
  4. Garth17

    Brock Allen Guest

    You say you instantiate this MySession class as a property from your base?
    No, this isn't exactly what I was suggesting. Here's an example (forgive
    my VB.NET pseudo-code):

    Class MyCommonInfo
    Shared ReadOnly Property ClientIPAddress as String
    Get as String
    return HttpContext.Current.Request.UserHostAddress
    End Get
    End Property
    End Class

    Then in your page:

    Sub Page_Load(...)
    myLabel.Text = "Your IP address is " & MyCommonInfo.ClientIPAddress
    End Sub

    The trick here is the Shared keyword -- this means you can just call the
    method or property without creating an instance of the class. Make it much
    easier to use and there's not a temporary object on the heap when there doesn't
    need to be.

    -Brock
    DevelopMentor
    http://staff.develop.com/ballen



    > I see your point. I just made a custom MySession Class.
    >
    > And now I instantiate it inside both the base classes as a property.
    > So now on the webforms if I want to get at session data I could make a
    > call to Me.mySession.sesPersonID. mySession being the Public
    > property
    > of the base class that returns a MySession object instantiated from my
    > new MySession.vb class.
    > This does solve the code duplication problem.
    >
    > Is that elegant way of handling this?
    >
    > Or should I not be exposing this class as property of a base class?
    > And instead just instantiate the MySession class in all my Webforms
    > and ASCX pages separately?
    >
     
    Brock Allen, Mar 18, 2005
    #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. Red
    Replies:
    1
    Views:
    402
    pubb AT hotmail DOT com
    May 9, 2004
  2. Vladimír Karásek

    using common code in ascx and aspx classes

    Vladimír Karásek, Jul 21, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    1,709
    Bryant Hankins
    Jul 21, 2004
  3. Vladimír Kolesnik

    using common code for aspx and ascx classes

    Vladimír Kolesnik, Jul 21, 2004, in forum: ASP .Net
    Replies:
    5
    Views:
    822
    Vladimír Kolesnik
    Jul 22, 2004
  4. just_life
    Replies:
    0
    Views:
    708
    just_life
    Aug 1, 2007
  5. Saraswati lakki
    Replies:
    0
    Views:
    1,415
    Saraswati lakki
    Jan 6, 2012
Loading...

Share This Page