Thursday, June 18, 2009

ASP Server.Execute vs Include, passing variables and dynamically loading different pages

Sometimes you have to work with legacy applications that utilize asp (classic) vb from the 90’s (ahh the days of .com where the internet was supposed to make everyone a millionaire and retire at 30).

Anyways…
So here is the issue I came across in ASP VB regarding dynamic layouts. The basic issue is that the client wants to make a template for the application so that it looks like their own website. Great idea, just swap out the include header and footer in your code right (hopefully you created include files for your header and footer in your application layout)? problem solved.

But lets take this a step further. Now that we know this is something that can happen sales/marketing will probably jump on this idea and say that the application can be customized to look like your website. Thinking like a developer (aka try to automate, reduce redundant work and save your sanity) rather then having to modify and maintain a ton of different client’s applications with the same functionality but slightly different HTML, why not create a template type of framework so that the application could read different templates dynamically without the need of developing the custom HTML for each page. Easy right? Just have some code be able to dynamically change the header and footer files?

In ASP.NET with the advent of usercontrols and Masterpages it gave the developer the opportunity to really make the website content layout dynamic and programmable. Basically the ability to swap different templates, control, html etc… in different areas programatically with great ease.

In ASP VB you cannot change the include file programatically. This is due to the way asp read include files. It processes the include first then executes the asp code. Basically you must have a hardcoded file in the include no variables.

This will NOT work:

dim FileToInclude
FileToInclude = “header.asp”


Well shoot, that means you can’t change the includes by coding. Well there is another thing that will solve this issue (although with a problem), The Server.Execute operation.

This WILL work:

dim FileToInclude
FileToInclude = “header.asp”
Server.Execute(”FileToInclude”)

Great right?
Well one major problem. Any page specific variables (ex. NOT application , session) that exists before the execute will not be passed.

You will not be able to see the the LookAtMe variable in the FileToInclude asp page

LookAtMe = false
FileToInclude = “header.asp”
Server.Execute(”FileToInclude”)
The only work around I found was to use a session variable (I know very very bad practice).

Why would I care about passing the variable? Well suppose you have a variable that is on the top of each page that contains a number that is critical to setting something in the header.asp file. For example maybe a flag that states whether part of the menu should be shown if you are an Admin or a User for the application.

After this fun little exercise it gave me a great appreciation for MasterPages and UserControls . When usercontrols first came out I was wondering what was the reason for their use, why not use an include, but here is a perfect example of why to use UserControls and MasterPages.

No comments:

Post a Comment