Including Other Files: the Server-Side Include

One of the handiest features of ASP is the server-side include. This feature lets you dynamically include arbitrary files within an ASP Web page at run time. This feature is extremely useful for creating global functions, headers, footers, or other elements that need to be reused on multiple pages. If you need to change these functions or HTML fragments, you can do so once and the change will be automatically reflected in all pages that reference the file using a server-side include. One trade-off associated with server-side includes is that they add a small amount of overhead to the server-side processing because the Web server has to find the file to be included and merge it into the ASP Web page. Note that include files can be used from regular HTML pages as well as ASP Web pages.

The #INCLUDE directive inserts the content of another file into an .asp file before ASP processes it, as shown in the following syntax:

<!—#INCLUDE VIRTUAL|FILE="filename"--> 

Included files do not need a special filename extension; however, we recommend giving included files an .inc filename extension to distinguish them from other types of files.

Use the VIRTUAL keyword to indicate a path beginning with a virtual directory. For information on using virtual directories, see the Microsoft Web server's online documentation. For instance, if a file named footer.inc resides in a virtual directory named /Myapp, the following line inserts the contents of footer.inc into the file containing the line:

<!—#INCLUDE VIRTUAL="/myapp/footer.inc"--> 

The FILE keyword indicates a relative path. A relative path begins with the directory that contains the including file. For instance, if the file is in the same directory as Myapp and the file header1.inc is in Myapp/Headers, the following line inserts header1.inc in your file:

<!—#INCLUDE FILE="headers/header1.inc"--> 

The FILE parameter with the ../ syntax is used to include a file from a parent directory. Script commands and procedures must be contained within the script delimiters <% and %>, the HTML tags <SCRIPT> and </SCRIPT>, or the HTML tags <OBJECT> and </OBJECT>. That is, a user cannot open a script delimiter in an including .asp file and then close the delimiter in an included file. The script or script command must be a complete unit.

The Include design-time control (DTC) is the most convenient way to add a server-side include to your project. (The Include DTC that ships with Visual InterDev 6 is the same Include DTC that shipped with Visual InterDev 1.) You can add the Include DTC to your Toolbox and use it to include files into a page. This DTC lets you choose from a graphical, tree view of all the elements in your Web site, and it automatically generates the appropriate include syntax.



Programming Microsoft Visual InterDev 6. 0
Programming Microsoft Visual InterDev 6.0
ISBN: 1572318147
EAN: 2147483647
Year: 2005
Pages: 143

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net