当前位置: 首页 > 网络学院 > 服务端脚本教程 > ASP > ASP #include

ASP
ASP Drive
ASP File
ASP Folder
ASP Dictionary
ASP ADO
ASP AdRotator
ASP BrowserCap
ASP Content Linking
ASP Content Rotator
ASP Quick Ref
ASP 摘要
ASP 实例

ASP #include


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-03-01   浏览: 904 ::
收藏到网摘: n/a

The #include directive is used to create functions, headers, footers, or elements that will be reused on multiple pages.
#include指示命令是用来建立在多个页面上被重复使用的方程、标题(页眉)、落款(页脚)或者是元素的


The #include Directive
#include 指令

You can insert the content of one ASP file into another ASP file before the server executes it, with the #include directive. The #include directive is used to create functions, headers, footers, or elements that will be reused on multiple pages.
你可以使用#include指示命令在服务器执行其中一个ASP文件之前将另一个ASP文件导入进来。#include指示命令是用来建立在多个页面上被重复使用的方程、标题(页眉)、落款(页脚)或者是元素的


How to Use the #include Directive
如何使用#include指示命令

Here is a file called "mypage.asp":
以下是一个名为“mypage.asp”的文件:

<html>
<body>
<h3>Words of Wisdom:</h3>
<p><!--#include file="wisdom.inc"--></p>
<h3>The time is:</h3>
<p><!--#include file="time.inc"--></p>
</body>
</html> 

Here is the "wisdom.inc" file:
以下为“wisdom.inc”文件:

"One should never increase, beyond what is necessary,
the number of entities required to explain anything."

Here is the "time.inc" file:
以下为“time.inc”文件:

<%
Response.Write(Time)
%>

If you look at the source code in a browser, it will look something like this:
如果你通过浏览器查看“mypage.asp”源文件,你将看到以下代码:

<html>
<body>
<h3>Words of Wisdom:</h3>
<p>"One should never increase, beyond what is necessary,
the number of entities required to explain anything."</p>
<h3>The time is:</h3>
<p>11:33:42 AM</p>
</body>
</html>


Syntax for Including Files
包含文件所要遵循的语法

To include a file in an ASP page, place the #include directive inside comment tags:
如果你要将一个文件导入到ASP文件当中,使用的导入标签如下:

<!--#include virtual="somefilename"-->
or
<!--#include file ="somefilename"-->

The Virtual Keyword
关键字 Virtual

Use the virtual keyword to indicate a path beginning with a virtual directory.
使用Virtual标签来指示出一条虚拟路径

If a file named "header.inc" resides in a virtual directory named /html, the following line would insert the contents of "header.inc":
如果一个名为“header.inc”的文件存放在名为“html”的虚拟文件夹下,那么下面就是把“header.inc”文件的内容插入其中的方法:

<!-- #include virtual ="/html/header.inc" -->

The File Keyword
File 关键字

Use the file keyword to indicate a relative path. A relative path begins with the directory that contains the including file.
你可以使用file关键词来指示一条相对路径。如果一个名为“header.inc”的文件存放在名为“htmlheader”的文件夹下,那么下面就是把“header.inc”文件的内容插入其中的方法:

If you have a file in the html directory, and the file "header.inc" resides in htmlheaders, the following line would insert "header.inc" in your file:

<!-- #include file ="headersheader.inc" -->

Note that the path to the included file (headersheader.inc) is relative to the including file. If the file containing this #include statement is not in the html directory, the statement will not work.
注意:如上述例子所示,被导入的文件“headersheader.inc”的路径是相对于导入文件而言的;如果被导入的文件“header.inc”不是存在于“html”文件夹下,那文件将不能被导入。

You can also use the file keyword with the syntax (..) to include a file from a higher-level directory.
你同样也可以使用(../)语法将文件导入到更高一级文件夹下一个文件。


Tips and Notes
提示和注意事项

In the sections above we have used the file extension ".inc" for included files. Notice that if a user tries to browse an INC file directly, its content will be displayed. If your included file contains confidential information or information you do not want any users to see, it is better to use an ASP extension. The source code in an ASP file will not be visible after the interpretation. An included file can also include other files, and one ASP file can include the same file more than once.
以上的例子当中,被导入文件的扩展名都是”.inc”。注意:如果使用者直接去浏览“INC 文件”,它的内容是可以被显示出来的。如果被导入的文件当中包含有机密成分或者有你不想让别人看到的内容,你最好使用“.ASP”的文件,因为ASP的源代码是不会被浏览器获取的。你也可以把文件导入其中一文件后,再把这个文件导入另一个文件;一个文件也可以被多次导入另外一个文件。

Important: Included files are processed and inserted before the scripts are executed.
重点:被导入的文件将在脚本执行之前被执行和插入。

The following script will not work because ASP executes the #include directive before it assigns a value to the variable:
下面的脚本将不会被执行因为导入文件名中存在变量:

<%
fname="header.inc"
%>
<!--#include file="<%=fname%>"-->

You cannot open or close a script delimiter in an INC file. This script will not work:
你不能在脚本定界符中导入INC文件,如下面的例子将不被执行:

<%
For i = 1 To n <!--#include file="count.inc"-->
Next
%>

But this script will work:
但下面的代码可以执行:

<% For i = 1 to n %>
<!--#include file="count.inc" -->
<% Next %>

评论 (0) All

登陆 | 还没注册?