ASP.NET的页面中对其他文件的引用
2011-07-01 03:31
232 查看
ASP.NET的页面中对其他文件的引用
先来看看ASP风格的===============
你可以把一个ASP页面的内容在服务器执行之前添加到另外一个ASP页面中, 方式是使用#include命令.
#include命令被用来创建函数, headers, footers, 或者将要被多个页面重用的部分.
如何使用?
这是"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>
语法
<!--#include virtual="somefilename"-->
这里的virtual表明地址是起自一个虚拟路径. 比如说, 一个名叫header.inc的文件存在于一个叫做/html的虚拟目录下, 那么下面的一段命令会插入header.inc的内容
<!-- #include virtual ="/html/header.inc" -->
<!--#include file ="somefilename"-->
这里的file表明路径是一个相对路径. 如果有一个文件在html文件夹下, 并且header.inc文件存在于html/headers中, 那么下面的代码会插入header.inc文件的内容
<!-- #include file ="headers/header.inc" -->
在ASP.NET页面中动态包括HTML文件和客户段脚本文件
===========================
因为ASP.NET应用程序在发送给客户端之前要经过编译, 运行的, 所以在服务器端include文件的时候, 你不能使用变量来替代文件名(比如说:<!-- #include PathType = FileName –>" ). 然而, 你可以使用Response或者StreamReader对象来向HTML流中写入被include的文件.
举例:
<%@ Page Language="vb" AutoEventWireup="false"%> <html> <body> <% Response.WriteFile ("Yourfile.inc") %> </body> </html>
在ASP.NET页面中插入指定文件的内容, 包括Web pages(.aspx文件), user control files(.ascs文件), 还有Global.asax文件
============================
语法, 与ASP风格的include 相同.
<!-- #include file|virtual="filename" -->
Remarks
赋予File或者Virtual属性的值必须被双引号括起来(""). 被included的文件会在任何动态代码执行前被处理. Include文件能被用来包含从静态文本(比如说普通的页面header或者公司地址), 到包含土工的服务器端代码(server-side code), 控件(control), 或者开发者想要插入到其他页面中的HTML标签块.
尽管你也可以使用这个#include 的方式来重用代码, 在ASP.NET中通常的更好的方式是使用Web user controls. 因为user control提供了面向对象的编程模型, 而且比服务器端的include功能更加强大.
#include标签必须被HTML或XML的注释边界符括起来, 来避免它被解释成为字面上的文本.
举例:
<html> <body> <!-- #Include virtual="/include/header.inc" --> Here is the main body of the .aspx file. <!-- #Include virtual="/include/footer.inc" --> </body> </html>
参考资料:
How To Dynamically Include Files in ASP.NET
http://support.microsoft.com/kb/306575
Server-Side Include Directive Syntax
http://msdn.microsoft.com/en-us/library/3207d0e3.aspx
ASP Including Files
http://www.w3schools.com/asp/asp_incfiles.asp
分类: ASP.NET
相关文章推荐
- ASP.NET的页面中对其他文件的引用
- 从其他项目复制cs类文件到新项目后无法引用问题解决方法(开发环境vs2010,asp.net项目)
- Asp.net页面中引用js文件无效的问题的解决方法
- Asp.net 让其他后缀名、扩展名文件像aspx页面一样被解析
- 如何在Asp.net 页面中添加CSS和JS的引用, 使得不是文件路径的限制
- Asp.net页面引用SAP IQ 16 iAnywhere.Data.SQLAnywhere.V4.0.dll报错,语言文件没找到
- Asp.net页面中引用js文件无效的问题的解决方法
- Asp.net页面中引用js文件无效的问题的解决方法
- Asp.NET大文件上传组件开发总结(四)---封送数据给Asp.NET页面
- asp.net获取当前页面文件名,参数,域名等方法。统一session验证和权限验证的方法
- HTML控件ID和NAME属性的区别,以及如何在asp.net页面的.CS文件中获得.ASPX页面中HTML控件的值
- ASP.NET使aspx页面能接受HTML,asp的页面传送的文件
- 引用 运行asp.net程序时候,编译器错误消息: CS0016: 未能写入输出文件“c:\WINDOWS\Microsoft.NET\Fra
- ASP.net C#获取其他网站的页面内容
- asp.net上传execl文件后,在页面上加载显示。
- Asp.net生成Excel文件并下载(更新:解决使用迅雷下载页面而不是文件的问题)
- [转]asp.net母板页引用文件的路径问题
- ASP.NET 封装JS文件到DLL中并在页面中调用
- ASP.NET 后台添加css和javascript的文件引用
- ASP.NET 未经登录的用户访问其他页面时转入指定页面