Wednesday 30 November 2011

INCLUDE FILES

If you are getting into web development, especially with ASP programming, include files are going to become one of your best friends. In essence, an include file allows you to write an ASP script one time and then include it in multiple web pages without having to rewrite all the code.

So when and how do you do this? One of the most basic examples is placing your database connection within an include statement and then using a simple include statement to include your connection string in your web pages.

First, you create a folder named /includes within your root directory.

Next, you create a new file named connection.asp and save it within your new /includes folder.

All that is left is to input a simple one line include statement into each page where you need to have a database connection. Here is how the include statement looks:




The include statement cannot be placed inside of your <% %>. If you need to place an include statement within your ASP code, you need to close your ASP code, input the include statement, and reopen your ASP code like this:
<%' ASP code here%>

<%' ASP code here%>

OK, you've got that. Now what is the advantage of using an include file like this? Well, think about it. Let's say your website is 100 pages and that 6 months from now something is your database conenction chages. Rather than having to go through and edit your connection string in all 100 pages, you can simply edit it one time in your include file and that will automatically update it throughout your website.

There are two way of including files in a web page. You can use INCLUDE VIRTUAL or INCLUDE FILE. Our preference is to use INCLUDE VIRTUAL. This means that you can just enter the virtual path of your website to the file you want to include. In the example of the path is "/includes/connection.asp".

http://www.aspwebpro.com/tutorials/asp/includefiles.asp

No comments:

Post a Comment