
经常有这样的要求,根据不同的需求要求include不同的文件如各个人的不同设置,所以要求能动态include文件受<! #include file="filename.asp" --> 宏限制
必须存在该文件并且会预先编译(不管前面是否加以条件)
经常有这样的要求,根据不同的需求要求include不同的文件
如各个人的不同设置,所以要求能动态include文件。
代码如下:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | Functioninclude(filename) Dimre,content,fso,f,aspStart,aspEnd set fso=CreateObject("Scripting.FileSystemObject") set f=fso.OpenTextFile(server.mappath(filename)) content=f.ReadAll f.close set f=nothing set fso=nothing set re=new RegExp re.pattern="^s*=" aspEnd=1 aspStart=inStr(aspEnd,content,"<%")+2 do while aspStart>aspEnd+1 Response.write Mid(content,aspEnd,aspStart-aspEnd-2) aspEnd=inStr(aspStart,content,"%>")+2 Execute(re.replace(Mid(content,aspStart,aspEnd-aspStart-2),"Response.Write ")) aspStart=inStr(aspEnd,content,"<%")+2 loop Response.write Mid(content,aspEnd) set re=nothing EndFunction |
使用范例:
include("youinc.asp")
| 1 2 3 4 5 6 7 | <% "得到用户所在的省份 DimProvince Province = Request.form("Province") "下面显示这个省份相关的信息 %> <!--#include virtual="/ Province/<%= Province%>"--> |
你觉得上面的代码可以工作吗?不!简单地使用ASP来达到动态包含,你不能达到你想要的效果。为什么?因为:Include命令先于ASP代码而得到执行,所以,上面的代码并没有按照作者的意愿,先得到用户所在的省份,再包含这个省份的信息!
如果你确实需要动态包含,你可以这样做:
| 1 2 3 4 5 6 7 8 9 | <% SelectCaseProvince Case1: %> <!--#include file="1.asp" <%Case2: %> <!--#include file="2.asp" <%Case3: %> <!--#include file="3.asp" <%EndSelect%> |
应该说,这段代码可以得到你想要的结果。但是,由于你的用户可能来自于33个省,你难道包含33个文件?特别要说明的是,SSInc.dll是不知道你究竟需要哪个包含文件的(事实上,这时候Province还没有值),所以,她把所有的文件都包含进来了!你可以想象,这时候的文件有多大!然后,ASP.DLL会去扫描这个文件中的ASP代码,然后执行!
所以,每当这样的时候,你应该考虑其他的思路,比如数据库,或者采用FileSystemObject。
解答
ASP程序员经常面临的最大挑战之一是动态Include文件。由于#include 在ASP代码执行之前处理,所以,看起来,动if/else的脑筋是不可能的。
真是这样吗?
根据你使用Include的目的,以及你将Include的文件数目,使用if/else也许可以解决问题。但这绝对不是任何时候可以奏效的,而且也不是一种有效的解决办法,因为你需要做许多的手工工作。
假设有两个样本HTM文件,1.htm和2.htm,为简化起见,假设文件的内容如下:
<!-- 1.HTM: -->
<font color=#ff0000>This is 1.htm</font>
<!-- 2.HTM: -->
<font color=#0000ff>This is 2.htm</font>
现在我们来试试动态Include:
| 1 2 3 4 5 6 7 8 9 10 11 | <% if request.querystring("param")="2" then %> <!--#include file="2.htm"--> <% else %> <!--#include file="1.htm"--> <% end if %> |
请注意:上面的两个#include 实际上都得到了处理。你可以实际运行一下,看看效果:http://localhost/Test.asp?param=1
http://localhost/Test.asp?param=2
http://localhost/Test.asp
上面我们是把一个querystring作为条件。你还可以把时间、日期、浏览器版本等作为条件。但是,条件越复杂,这种方法的效率越差。下面提供了另外一种思路:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | <% if request("param")="2" then filespec ="2.htm" else filespec ="1.htm" end if filespec = server.mapPath(filespec) scr ="scripting.fileSystemObject" set fs = server.createobject(scr) set f = fs.openTextFile(filespec) content = f.readall set f = nothing set fs = nothing response.write(content) %> |
在IIS5.0/ASP3.0中,有两种新的方法来支持“动态包含”:
<%
server.transfer filename
server.execute filename
%>
如果正好使用的是IIS5.0和ASP3.0,那么Ok! 但是IIS5.0需要运行在Windows 2000上。
很多时候,由于程序设计需要,要求在asp的include包含文件里调用动态的文件。如<!--#include file="map1.asp"-->其中的1是个动态参数,需要request获取。但可惜的是,include语句里并不能含有变量,否则将提示找不到文件错误。本文将提供3种方法解决该问题。
首先,我们需要了解,包含文件的提示和警告信息:被包含的文件可以包含其他文件。只要“#include”命令不导致循环,.asp 文件也可以多次包含同一文件。例如,如果文件 First.asp 包含文件 Second.inc,则 Second.inc 不能反过来包含 First.asp。文件也不能包含其自身。ASP 检测这样的循环或嵌套错误,生成错误消息,并停止处理请求的 .asp 文件。
解决方法一:FSO调用方法
ASP语言是强大易用的语言,我们不要把自己的思维局限固定在一个角落里,“条条大道通罗马”,一个思路不通,换个思路吧。
本方法采用变通的方法实现同样的包含功能。即FSO调用。代码如下:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <% FunctiongetFileContents(strIncludeFile)"函数:读取包含文件内容 DimobjFSO,objText,strPage SetobjFSO = Server.CreateObject("Scripting.FileSystemObject")"调用FSO对象 SetobjText = objFSO.OpenTextFile(Server.MapPath(strIncludeFile)) getFileContents = objText.ReadAll objText.Close SetobjText =Nothing SetobjFSO =Nothing EndFunction dim p p=request("p")"2种方式:request.form ,request.querystring response.write getFileContents("map"&p&".asp") %> |
这样,利用fso函数读取包含文件的内容,然后用response.write把包含文件的内容输出,即实现和include命令同样的功能了,轻松实现自由输出诸如 map1.asp,map2.asp… 等文件内容了。
解决方法二:if...elseif...
此方法适用于要包含的文件数量不多的情况下,也是懒人+笨人的方法,呵呵。代码如下:
| 1 2 3 4 5 6 7 | <%if a=1 then%> <!--#include file="map1.asp"--> <%elseif a=2 then%> <!--#include file="map2.asp"--> <%elseif a=3 then%> <!--#include file="map3.asp"--> <%end if%> |
解决方法三:select case
方法和第二种差不多,稍微好点。代码如下:
| 1
Previous: 用ASP开发网页需要牢记的注意事项
Next: 公司官网建设有何意义?需要注意哪些因素?
Disclaimer: The content on this page is collected and edited by Jinghuang Network for reference only. We do not claim ownership or bear legal responsibility for external materials. If you find any copyright infringement, please contact us with proof, and we will remove the content within 5 working days. For more insights on Global Web Optimization and Global Web Development, please visit our official site (www.0731jianzhan.com).
Related News
Relevant News
Recommended Services
Hot Services
0731-82272030 Address: Room 1202, North Bldg, Xingwei Mingzuo, Yuhua Dist, Changsha Jinghuang Network specializes in corporate sites, e-commerce, apps, and full-stack systems. We provide stable, high-performance platforms with full tech support and maintenance.
WeChat Official
Copyright © 2015-2026 Jinghuang Network Xiang ICP 19019417-1 All Rights Reserved Sitemap
Get
Quote Submit Needs× |