我們學過的Code分離到不同文件的方法,主要包括:
程序集.dll、<inherits src>.cs、<script src>.cs、用戶控件.ascx、include、Response.WriteFile()
程序集.dll:這是最高級的方法,它引用的是一個被編譯為IL的DLL(程序集)文件。
<inherits src>.cs:用這種方法,可以先定義一個繼承Page類的新類,再在ASPX/ASCX文件中加工此類。
<script src>.cs:可以把<script runat=”server”>部分分離到一個新文件。
用戶控件.ascx:把一段ASP.NET代碼作為一個控件引入。
include:這是今天的主題,詳見下。
Response.WriteFile():他只能用于引入一段“純客戶端代碼(DHTML)”,擴展名可隨意。
實驗項目描述:
我想沒有什么比一個UI更說明問題了,那么這是一個什么頁面呢?
這是一個典型的“上中下”結構的網頁,在實作中:“頁頭/頁腳”可能是不變的,而中部可能是變化的。
于是在實作中,如果用“include法”我們需要把三個部分離出來,單為一個文件。
之后,可以使用一個“主文件”,分別把它們三個文件include進來。
而今天,我們只是一個實驗,所以我們是這樣設計的:
中部為一個“主文件”,之后把上下兩部分include進來。
最后,我們還會把一些關鍵技術進行總結。
代碼實現:
【上部分文件:head.aspx】
代碼如下:
代碼
Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--><script runat=server>
void click1 (object a,EventArgs b)
{ label1.Text=text1.Text;
label2.Text=text2.Text;}
</script>
<h1>The Softzz's New page</h1>
<p>2004-11-15</p>
Name:<asp:textbox id="text1" runat="server"/>
Pass:<asp:textbox id="text2" runat="server"
textmode="password"/>
<asp:button id="button1" runat="server"
Text="ClickMe" OnClick="click1"/>
<HR width="80%" SIZE="1">
【上部分文件:end.a】
代碼如下:
代碼
Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--><script runat=server>
void click2 (object a,EventArgs b)
{ label1.Text=text3.Text;
label2.Text=text4.Text;
}
</script>
<HR width="80%" SIZE="1">
Name:<asp:textbox id="text3" runat="server"/>
Pass:<asp:textbox id="text4" runat="server"
textmode="password"/>
<asp:button id="button2" runat="server"
Text="ClickMe" OnClick="click2"/>
<h5><%= DateTime.Now.ToString() %></h5>
<b><p>CopyRight: SoftZZ</p></b>
新聞熱點
疑難解答
圖片精選