Page #24 (Chapter 3 - Building ASP Applications)

Chapter 3 - Building ASP Applications

Visual Basic Developers Guide to ASP and IIS
A. Russell Jones
  Copyright 1999 SYBEX Inc.

Using Include Files
To reduce the amount of repetitive code or HTML in your ASP pages, you can include external files inside your ASP page. An include file is code from an external file that the server places in the ASP page at runtime. Include files have an .inc extension. You control where the server places the content of an include file with an include directive. An include directive is formatted as an HTML comment, so servers that don't understand the directive will ignore it, as will browsers. The entire process is exactly the same as if you were to cut and paste the contents of the file into your ASP page. Here's an example of an include directive:
<!—- #INCLUDE FILE="c:\ include\myInclude.inc" -—>
The include directive tells the server to replace the include directive with the contents of the file—in this case, myInclude.inc. There are two forms of include directives: #INCLUDE FILE and #INCLUDE VIRTUAL. The FILE form requires a physical path for the file, whereas the VIRTUAL type references a file in a virtual directory.
Regardless of which form you use, the ASP engine performs all include replacements before it processes any code. Therefore, you cannot use code to determine which file to include or whether to include a file. The following code will not work as intended—the ASP engine will include both files.
<%
if myVar=True then
     %>
     <!—- #INCLUDE FILE="c:\include\myInclude.inc" -—>
     <%
else
     %>
     <!—- #INCLUDE FILE="c:\include\yourInclude.inc" -—>
     <%
end if
When the ASP engine parses this file, it will replace the two include directives with the contents of myInclude.inc and yourInclude.inc—and it will make the replacement before it runs the code for the if…end if structure. This is a common misconception, so I'll repeat. Once again, with emphasis: You cannot use code to determine which file to include and you cannot use code to determine whether a file will be included.
Despite this, with a little planning, you can still take advantage of include files. Using the preceding example, suppose myInclude.inc contains the HTML to display the graphic HappyBirthday.gif and that yourInclude.inc contains HTML to display the graphic HappyAnniversary.gif. In this case, the code would work even though the ASP engine replaces both include directives. This time, although the ASP engine inserts the HTML code for both files, the If…Then structure ignores one of them, depending on the value of the myVar variable. Therefore, only one of the files displays on the browser, which is the intent.
Include files are useful for inserting often-used functions or subroutines into an ASP page. By placing the code in an include file, you can update all pages that reference the code by making changes in only one place. Using include files in this way is like placing your favorite routines in a module in VB.
Include files are not limited to code. I've seen many sites that use them for toolbars, common graphics, common sections of text, etc. You can create include files that are all code, all HTML, or a mixture of both—just like any other ASP page. You can and should use include files to enforce consistency and reuse code in your application.



Visual Basic Developer[ap]s Guide to ASP and IIS
Visual Basic Developer[ap]s Guide to ASP and IIS
ISBN: 782125573
EAN: N/A
Year: 2005
Pages: 98

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