推荐:ASP 3.0高级编程(四十六)表10-5 支持数据绑定的HTML元素HTML元素 绑定属性 可否更新数据 可否表格绑定 可否作为HTML显示 A href 不可 不可 不可 APPLET PARAM 可以 不可 不可 BUTTON innerText
#include命令用于在多重页面上创建需重复使用的函数、页眉、页脚或者其他元素等。
#include 命令
通过使用#include命令,我们可以在服务器执行某个ASP文件之前,把另一个ASP文件插入这个文件中。#include命令用于在多重页面上创建需重复使用的函数、页眉、页脚或者其他元素等。
如何使用#include命令
这里有一个名为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> |
这是wisdom.inc文件:
One should never increase, beyond what is necessary,
the number of entities required to explain anything.
这是time.inc文件:
| 以下为引用的内容: <% Response.Write(Time) %> |
在浏览器中查看的源代码应该类似这样:
| 以下为引用的内容:
<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> |
Including文件的语法:
如需在ASP中引用文件,请把#include命令置于注释标签之中:
| 以下为引用的内容:
<!--#include virtual=somefilename--> |
或者:
| 以下为引用的内容:
<!--#include file =somefilename--> |
关键词Virtual
关键词virtual指明位于虚拟目录的路径。
如果名为header.inc位于名为/html的虚拟目录中,下面这行代码会插入文件header.inc中的内容:
| 以下为引用的内容:
<!-- #include virtual =/html/header.inc --> |
关键词File
关键词File可指明一个相对的路径。相对路径起始于含有引用文件的目录。
假设某文件位于html文件夹的子文件夹headers中,下面这段代码可引用header.inc文件的内容:
| 以下为引用的内容:
<!-- #include file =headers/header.inc --> |
注意:被引用文件的路径是相对于引用文件的。假如包含#include声明的文件不在html目录中,这个声明就不会起效。
您同样可以使用关键词file和语法(../)来引用上级目录中的文件。
提示和注释
在上面的一节中,我们使用.inc来作为被引用文件的后缀。注意:假如某用户尝试直接浏览某个INC文件,这个文件中内容就会暴露。假如被引用的文件中的内容涉及机密,那么最好还是使用asp最为后缀。ASP文件中的源代码被编译后是不可见的。被引用的文件也可引用其他文件,同时ASP文件可以对同一个文件引用多次。
重要事项:在脚本执行前,被引用的文件就会被处理和插入。
下面的代码无法执行,这是由于ASP会在为变量赋值之前执行#include命令:
| 以下为引用的内容:
<% fname=header.inc %> <!--#include file=<%=fname%>--> |
不能在脚本分隔符之间包含文件引用:
| 以下为引用的内容: <% For i = 1 To n <!--#include file=count.inc--> Next %> |
但是这段脚本可以工作:
| 以下为引用的内容:
<% For i = 1 to n %> <!--#include file=count.inc --> <% Next %> |
分享:ASP把长的数字用逗号隔开显示以下为引用的内容: 000000000000000000000000000000000000000000000000000000000000000<%Function Comma(str)If Not(IsNumeric