When you think ASP, think...
Recent Articles xml
All Articles
ASP.NET Articles
Related Web Technologies
User Tips!
Coding Tips
spgif spgif

Sample Chapters
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Stump the SQL Guru!
XML Info
Author an Article
spgif spgif
ASP ASP.NET ASP FAQs Feedback topnav-right
Print this page.
User Tips: A Neat Use for Include Files Encompassing both HTML content and ASP Code

By Jeremy J.

I've got a suggestion for a tip I've picked up when making ASP/SQL pages.

I often find my self using a similar design over a large number of pages, and I want to have a header and a footer. I know this could be set up with two lines:

<!--#include file="header.inc"--> and <!--#include file="footer.inc"-->

but for me that means editing more files, and doesn't allow easily to see both files at once, for example if I made a large change to the site design. Also, on some pages I want to include a small form that updates something in a database using SQL, and depending on the results of that, possibly forward the user to another page. For example - I have a logon page for an admin area called logon.asp. The user enters their username on the page and the actual checking of the user is done on that page:

If request("action") = "logon"
  'check user
  'if valid response.redirect("main.asp")
End If
<!--#include file="header.inc"-->
<form action="<%=scriptname%>?action=logon" method="post">
<input type="text" name="user"><input type="submit"></form>
<!--#include file="footer.inc"-->

However, there can be some problems doing things like this - if the checking part needs to look something up on a database, then I need to specifically set up the database connection string beforehand - if it was in the header, then I'd have to do the checking after the header was shown, and then I couldn't use the forwarding method. If this is the same string as I need for several pages, and it may change, then I need to include a third file called something like database.inc and include that at the top of every page. Help! - 3 include files.

However, I discovered a nifty way of only using one include file, and it certainly solves all my problems. The file is called includes.asp and looks like this:

<!-- Include this information on each page -->
'Set up Connection String
Scriptname = Request.ServerVariables("SCRIPT_NAME")
<!-- End of general information -->

<% Sub Header(PageName) %>
.. Do header stuff
<% End Sub %>

<% Sub Footer %>
.. Do footer stuff
<% End Sub %>

Now, creating new database-enabled pages is very simple - I simply write them like this:

<!--#include file="includes.asp"-->
<% call header("New Page") %>
Page Content goes here
<% call footer %>

Simple, eh? Also - if I wanted to put any code at the top of the page, I can put it between the <!--#include line and the <% Call header line. Note, I also used a variable to change the title of the page - other variables could be used for similar purposes.

Now, if I need to change and of the information on pages, I simply need to alter the includes.asp file - where everything is in one place.

Great site guys - keep up the good work! I'll let you know if I think of any other useful things I've learnt in ASP/SQL, mostly from working on the site at http://www.planetquake.com/airquake2/.


Happy Programming!

Return to user tips...

ASP.NET [1.x] [2.0] | ASPFAQs.com | Advertise | Feedback | Author an Article