473,378 Members | 1,412 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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

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?

Nov 19 '05 #1
3 2919
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?


Nov 19 '05 #2
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?

Nov 19 '05 #3
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?


Nov 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: richardlane | last post by:
Hi, basic question here - I'm struggling with the transfer from asp to asp.net a bit, especially in seeing the 'bigger picture' of how things are best structured. I have a website made up...
5
by: Wysiwyg | last post by:
I'm new to c# programming and can't figure out how to avoid duplicating common code in multiple classes when I'm restricted to using different system base classes.. I'm using c# in asp.net to write...
1
by: Michael Evanchik | last post by:
Tying not to spaghetti code which seems to be easy to do in .net, im trying to do my main .net html in index.aspx, use repeated .net html in an .ascx files and all code im doing in .vb code behind...
3
by: Brent Minder | last post by:
What is the most efficient way to code asp.net pages when you break your page up into user controls? For example: If you have a page with a header (control .ascx), body, and footer (control...
2
by: Christian Ista | last post by:
Hello, To each pages, I have a common part (a graphic part). Is it possible to put the code (HTML) in a file and insert it in each pages or I have to copy/paste the code in each pages ? ...
4
by: Vladimír Kolesnik | last post by:
Hi there, does anybody know, how to create a class, which is can be inherited by both aspx (System.Web.UI.Page) as well as by ascx (System.Web.UI.UserControl) classes. I have the bunch of same...
6
by: Don Wash | last post by:
Hi All! I'm developing ASP.NET pages using VB.NET language. My background is VB6 and ASP3. Right now, I'm evaluating strategies on creating reusable and common functions and methods for ASP.NET....
5
by: djscratchnsniffing | last post by:
i know you can access an ascx's properties/methods from an aspx file. Let's say you have an aspx file with two code-behind files(ascx files). Can you access one of the ascx file's...
10
by: =?Utf-8?B?SmFrb2IgTGl0aG5lcg==?= | last post by:
We have a complex VS solution with several web projects. The reason is the applications share a lot of business logic that change often and we want to observe the implications of all changes. ...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.