淺談ASP.NET的include的使用方法

來源:互聯網
上載者:User

我們學過的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>

【主檔案:index.aspx】

複製代碼 代碼如下:代碼

Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--><%@ Page Language=C# %>

<center>

<form runat=server>

<!-- #include file="head.aspx" -->

<br/>

<p>This is a new test page.Please look at the info:</p>

<br/><br/>

User's Name: <b><asp:label id=label1 runat=server/></b>

<br/><br/>

User's Pass: <b><asp:label id=label2 runat=server/></b>

<br/><br/>

<!-- #include file="end.a" -->

</form>

</center>

關鍵技術·記述:

上面的例子還能說明什麼呢?

l include可以把幾個檔案最終拼成一個檔案,而作為元素的每一頁,只是最後拼成的最終頁的片段。

l 被拼的頁、最終的頁,均為ASP.NET代碼容器,而非HTML文本。

l 拼頁時,是按順序的。

l 這些檔案中的代碼,在被拼時,只是普通文字,當最終拼成後,才會被檢查/編譯/報錯/顯示……

l 一個檔案中可以多次include另一個檔案。但前提是不能使“聲明/定義”(標識符)部分重複(重名)。

l 如果在各檔案中終有“runat=server”的控制項,一定要注意<form runat=”server”>的始/終位置。

l 一頁中只能有一個<form runat=”server”>,就算可以設定form的id也不能有多個。

l 一頁中可能多次出現<script runat=”server”>,而且其都會運行在這個“最終頁”顯示前。
還有就是,它的運行與頁面順序無關,它是一個“代碼聲明塊”,它的元素只會被調用後運行。

l 我們可以把<script runat=”server”>們,放在頁面的任何地方,包括<form runat=”server”>內。

l 用include法引用檔案,副檔名可隨意。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.