Web Development

General discussion


ASP.NET Equivalent of Server-Side Includes?

By Underground_In_TN ·
My company's website is already ASP-based, but we are creating new pages in ASP.NET where it makes sense. But I'm having trouble coding the .NET pages to work with the old ASP pages due to the fact that .NET doesn't support server-side includes. Let me explain:

In the part of the website where users have logged in, we are storing a very few pieces of data in session variables, then referencing them as needed on each page. Each page includes an INC file that contains code to check a particular session variable, and if it's not populated because the user hasn't logged in or his session has expired, direct him to the appropriate "session expired" notice page. If it is populated, a series of other variables (Dim'd in the INC file and referenced later in each page) are populated according to the contents of another session variable.

How do I accomplish the same thing in ASP.NET? I could put this into a custom code snippet, but while that's nice for when I first create a new page, it's taking one step backwards as far as code reuse and ease of maintenance goes.

Or, I could rework this as a class and make those INC variable as properties of the class, but classes can't directly access session variables, meaning I can't populate the properties in the New constructor. So I'd have to duplicate code in each page that uses the class to populate those properties in the Page_Load sub, and now I've got a different set of ease-of-maintenance issues.

There has to be another way to do this in .NET. Isn't there?

This conversation is currently closed to new comments.

Thread display: Collapse - | Expand +

All Comments

Collapse -

One way...

by onbliss In reply to ASP.NET Equivalent of Ser ...

...if I understood your issue correctly here is what I think you can do to avoid duplicating the code in each page.

1)Create a base page, by creating a class that derives of System.Web.UI.Page, and place your code to check the session variable and other common stuff in the Page_Load. Let's call this page as AppBasePage

2)Then you derive all your pages of this AppBasePage class.

This would also ease out your maintenance issue, any future change would be just at the base page level. And each derived page still can access the base page members and act differently (if necessary).

Let me know if this works out for you.

edited: clarity and punctuation

Collapse -

At the risk of exposing my ignorance...

by Underground_In_TN In reply to One way...

... can you tell me how, or point me to a sample in the MSDN help or a web site? I am very new to ASP.NET. Geez, I sound like I'm trying to get you to do my work for me, but I'm really not. I just need a lot of detail until I find my way around .NET.

With my project open, I've added a new class, but I'm not sure how to make it derive from System.Web.UI.Page. I clicked Website > Add Reference ... and double-clicked System.Web (I don't see System.Web.UI.Page in the Add Reference window), but I can't tell if that did anything at all. Am I heading in the right direction?

edited to add disclaimer and more groveling...

Collapse -

2.0 or 1.1

by onbliss In reply to At the risk of exposing m ...

No problemO. At one point in time, even I was ignorant. And I am still learning things.

Incase you are working with .net 2.0, then you could use "master pages" to do this job.

Here is a link that addresses your specific question: http://www.codeguru.com/csharp/.net/net_asp/webforms/article.php/c11939/

Here are few websites that talk about the base page concept:

1) http://aspnet.4guysfromrolla.com/articles/041305-1.aspx

2) http://dotnetjunkies.com/Article/95EBDB54-B8D2-4D33-9DC2-C246793A3A77.dcik

3) http://aspalliance.com/63

General ASP.Net articles

Read both the Application life and Page life cycle.

Collapse -


by Underground_In_TN In reply to 2.0 or 1.1

That does indeed look like it is what I'm after.

However, after creating a master page with the partial class MyMasterPage (I'm using 2.0) and changing the Inherits= of another page to MyMasterPage, I get the error "Could not load type 'MyMasterPage'" when I try to view that page in a browser. So far, googling this error doesn't turn up any useful fixes.

This is what frustrates me to no end with .NET. Sometimes it seems like nothing ever works like it should, or as all the examples say it should work.

Collapse -

I can understand...

by onbliss In reply to 2.0

...the frusturation. Some of the coding in ASP.net involves some paradigm change from the classic ASP style coding.

Okay here are some good video tutorials: http://www.asp.net/learn/videos/default.aspx?tabid=63#howdoi

In there is a tutorial for Master Pages as well.

By the way, http://www.asp.net is a good site with plenty of resources. You should check it out.

This link has a good number of blogs as well: http://weblogs.asp.net/

Scott Guthrie was involved in some of the videos and his blog is good too. Here are his tip/tricks blog page: http://weblogs.asp.net/scottgu/pages/ASP.NET-2.0-Tips_2C00_-Tricks_2C00_-Recipes-and-Gotchas.aspx

Happy .net coding :-)

Collapse -

I dont think thats right

by callred In reply to 2.0

That's because you dont set inherits equal to a master page. You set Inherits to the name of the corresponding code behind class (which is what it is already set to if you select the code behind option when you create a new page). You set the "masterpagefile" attribute to the path of your master page. This is real easy. Look at the page in design view, and find the masterpagefile property and set it accordingly.

Collapse -

An easy alternative

by kirkk In reply to ASP.NET Equivalent of Ser ...

>NET 2.0 should make this easier, but if you're still on 1.x you can create a Header.ascx and Footer.ascx user controls, which just drop onto the pages at top and bottom.

Caveats: you can't include any input controls in the header or footer, because the form tags with runat=server have to be on the page - you can't split them up and put the open tag in the header and close tag in the footer. Also, working with a split table can be tricky.

Collapse -

ASP Session vars <> .Net Session vars

by reidb In reply to ASP.NET Equivalent of Ser ...

I'm interested in the part of the question about using classic ASP session vars and .Net session vars. We too are transitioning, page-by-page from classic ASP to ASP .Net and need to share session data across both types of pages on the same domain.

Anyone have experience or a recommendation?


Collapse -

One way

by callred In reply to ASP Session vars <> .Net ...

You may have already thought of this, but...

Since you are migrating to ASP.NET anyway, you could just re-implement the session handling calls in your classic asp page(s) to write their info to a database table (with the ASP.NET session table schema). Then, since ASP.NET has built in database-backed sessions, there would be virtually no work needed on that end.

Of course you could just use a centralized file share of some sort, with xml (or other format) file-based session management. But then, its a customized session management solution on both ends (asp and asp.net).

Collapse -

Thanks for the idea

by reidb In reply to One way

I'm finding that you can handle this via cookies, database, ISAPI, or combinations thereof.

Related Discussions

Related Forums