Using Includes in ASP
A simple form of server side includes is including a file before a page is sent to the browser. The code is as follows:
<!-- #include virtual="foo.html" -->
- The contents of foo.html will be included in the current page before the page is sent to the browser. This assumes foo.html is in the same directory; if in another directory, the appropriate path must be specified.
- The code to include another page does not appear between the ASP delimiters of <% and %> but instead between HTML comment indicators, i.e. <!-- and -->.
- A file can contain multiple includes, for example inserting a header at one point and a footer at another.
- Dreamweaver (at least as far back as version 3) understands includes and in Design View will show the page after the code has been included but in Code View will just show the include statement(s). This is exactly what you want.
- In FrontPage 2000, the effect of the inclusion can be viewed by selecting File>Preview in Browser>Internet Explorer where the HTML view will again just shown the include statement(s).
Virtual Includes can be useful in several contexts, such as including the same code in several pages or to include code but keep your attention in coding on other portions of the code rather than the code you are including.
A trivial example of using a virtual include:
includejs.asp file:
<%@ LANGUAGE="JScript" %>
<% connCW = Server.CreateObject("ADODB.Connection")
connCW.Open("Driver={Microsoft Access Driver (*.mdb)}; DBQ=" + Server.MapPath("registration.mdb"));
rsItem = connCW.Execute("SELECT * from students") %>
<!-- #include virtual="heading.htmlf" -->
<% while (!rsItem.EOF) { %>
<tr>
<td><% =rsItem("sid") %></td>
<td><% =rsItem("first") %></td>
<td><% =rsItem("last") %></td>
<td><% =rsItem("state") %></td>
</tr>
<% rsItem.MoveNext() }
connCW.Close() %>
</table>
</body>
</html>heading.htmlf (use of this extension is a convention to indicate that it is a fragment)
<html>
<head>
</head>
<body>
<table>
<tr>
<td>ID</td>
<td>First Name</td>
<td>Last Name</td>
<td>State</td>
</tr>Revised: September 29, 2002. Comments to William Pegram, wpegram@nvcc.edu