如果你是从asp程序员转过来一定不习惯.net的编程风格吧,代码和页面时分离的,asp和php里面时常是引入,
比如<!--#include File="xxxxxxx"-->,这个其实在.net里面也有的,而且以好几种方式存在。
1.用户控件,比如你新建一个用户控件ccA.ascx,里面内容为:
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="ccA.ascx.cs" Inherits="MyWebSiteTest.Controls.ccA" %> 我是ccA控件呈现的
然后在要引入的页面头部先注册:
<%@ Register TagPrefix="ctr" TagName="Text" Src="~/Controls/ccA.ascx" %>
再在需要的地方写入就OK了
<ctr:Text ID="Text1" runat="server" />
2.用户控件的嵌套,就是用户控件里面嵌套用户控件,这个道理和用户控件有些类似,但是有些细节需要知道,就是子控件的属性。
3.用#include标签来引入
比如新建一个header.inc文件里面写入如下代码:
<div class="top"></div> <div class="main"> <div class="left"></div> <div class="right"></div> <div style="clear:both;"></div> </div>
然后我们将它直接引入到页面中:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="incfile.aspx.cs" Inherits="MyWebSiteTest.Manager.incfile" %> <%--<%@ Register TagPrefix="ctr" TagName="Text" Src="~/Controls/ccA.ascx" %>--%> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>页面包含inc文件</title> <style type="text/css"> bory{ margin:0; padding:0;} .content{ 1000px; margin:0 auto; padding:0;} .top,.bottom{ height:115px; background-color:#7fa9d5;} .left{ float:left; 250px; height:600px; border:1px solid #333; background-color:#946;} .right{ float:right; 720px; height:600px; border:1px solid #f00; background-color:#95C754;} </style> </head> <body> <form id="form1" runat="server"> <ctr:Text runat="server" /> <div class="content"> <span style="color:#ff0000;"><!--#include File="header.inc"--></span> 注释:这个是相对路径 <div class="bottom"></div> </div> </form> </body> </html>
3.对于普通的htm/html文件包含也就一句话的事,比如:
<!--#include virtual="/_data/shtm/footer.htm" -->
file和Virtual的区别:
<!-- #include file = "FileName" -->
<!-- #include Virtual = "FileName" -->
file和Virtual的区别在于file使用相对路径,而Virtual是你的web站点内虚拟目录的完整虚拟路径,比如:
<!-- #include file = "inc/char.inc" -->
就表示包含当前文件所在的虚拟目录路径下面的inc目录下的char.inc
写成 virtual就可能就需要这么写了:
<!-- #include virtual = "/myweb/inc/char.inc" --> 注释:myweb是虚拟根目录名称