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

G

Garth17

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?
 
B

Brock Allen

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.
 
G

Garth17

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?
 
B

Brock Allen

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top