ASP.NET的页面中对其他文件的引用
2010-02-07 22:51
253 查看
先来看看ASP风格的
===============
你可以把一个ASP页面的内容在服务器执行之前添加到另外一个ASP页面中, 方式是使用#include命令.
#include命令被用来创建函数, headers, footers, 或者将要被多个页面重用的部分.
如何使用?
这是"mypage.asp"
这是"wisdom.inc"文件
"One should never increase, beyond what is necessary,
the number of entities required to explain anything."
这是"time.inc"文件
<%
Response.Write(Time)
%>
如果你在浏览器里查看源代码, 你会看到这样:
语法
<!--#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的文件.
举例:
在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的注释边界符括起来, 来避免它被解释成为字面上的文本.
举例:
参考资料:
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页面的内容在服务器执行之前添加到另外一个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页面中引用js文件无效的问题的解决方法
- Asp.net 让其他后缀名、扩展名文件像aspx页面一样被解析
- Asp.net页面引用SAP IQ 16 iAnywhere.Data.SQLAnywhere.V4.0.dll报错,语言文件没找到
- Asp.net页面中引用js文件无效的问题的解决方法
- 从其他项目复制cs类文件到新项目后无法引用问题解决方法(开发环境vs2010,asp.net项目)
- 如何在Asp.net 页面中添加CSS和JS的引用, 使得不是文件路径的限制
- Asp.net页面中引用js文件无效的问题的解决方法
- HTML控件ID和NAME属性的区别,以及如何在asp.net页面的.CS文件中获得.ASPX页面中HTML控件的值
- ASP.Net中表单POST到其他页面的方法分享
- 【原】asp.net导出word,结果导出aspx页面文件的问题
- 有些疑惑,一个普通的aspx页面的文件,IIS怎么知道改用 asp.net1.1还是 2.0来解释呢?
- vs 开发 asp.net过程中 引用 js文件出错的解决办法
- 引用asp.net母版页后,母版页和内容页的页面事件执行顺序
- 【转】在Asp.net中弹出对话框,然后跳转到其他页面问题
- 引用asp.net母版页后,母版页和内容页的页面事件执行顺序
- ASP.net_form表单提交路径和之前做的其他网站路径都一样,但是提交后显示404错误,找不到页面
- 【原】Asp.net生成Excel文件并下载(更新:解决使用迅雷下载页面而不是文件的问题)
- asp.net上传execl文件后,在页面上加载显示。
- 黑马程序员_学习日记71_723ASP.NET(文件上传下载、IsPostBack原理、页面生命周期)