更新:2007 年 11 月
這個主題將示範如何建立使用 @ Page 指示詞之 CodeBehind 屬性 (Attribute) 的 Web 網頁和使用者控制項、加以編譯,然後將它們轉換成使用 CodeFile 屬性搭配 .NET Framework 2.0 版中程式碼後置 (Code-Behind) 檔案內的部分類別。
ASP.NET 2.0 版中新 Web 網頁的程式碼後置模型是以部分類別為基礎。網頁的標記會儲存在某個檔案 (.aspx 檔) 中,而程式碼則定義在部分類別 (程式碼後置的檔案) 中。將您現有的 Web 網頁轉換成新的程式碼後置模型可更有效地分離標記和程式碼,因為您不需要在部分類別中納入執行個體變數 (Instance Variable) 或明確事件繫結。
使用 @ Page 指示詞之 CodeBehind 和 Inherits 屬性的 Web 網頁將可繼續在 .NET Framework 2.0 中運作。不過,您必須編譯程式碼後置的檔案並將產生的組件 (Assembly) 放置於 Bin 資料夾中。
如果您選擇移轉至 ASP.NET 2.0 程式碼後置模型,就必須對您的 .aspx 檔案和程式碼後置的檔案進行對等的變更。在 .aspx 檔中,您要將 CodeBehind 屬性取代成 CodeFile 屬性。在程式碼後置的檔案中,您要使用部分類別。使用新程式碼後置模型的優點在於,您不需要明確編譯程式碼後置的檔案,因為 ASP.NET 編譯器會自動為您完成此工作。
此外,使用新的程式碼後置模型時,您必須使用 @ Register 指示詞,加入其他程式碼後置檔案的參考。
若要存取下列程序中建立的 Web 網頁,您必須在 Microsoft Internet Information Services (IIS) 中建立虛擬目錄。如需建立 IIS 虛擬目錄的詳細資訊,請參閱 HOW TO:在 IIS 5.0 和 6.0 中建立和設定虛擬目錄。
若要編譯使用 CodeBehind 屬性的 Web 網頁和使用者控制項
請建立使用 @ Page 指示詞之 CodeBehind 屬性的 Web 網頁,如下列程式碼範例所示。
<%@ Page Language="VB" AutoEventWireup="true" CodeBehind="CodeBehindExample.aspx.vb" Inherits="CodeBehindExample" %> <!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 > <title>Code-Behind Using the CodeBehind Attribute</title> </head> <body> <form id="form1" > <div> <asp:Label id="Label1" ></asp:Label> </div> </form> </body> </html><%@ Page Language="C#" AutoEventWireup="true" CodeBehind="CodeBehindExample.aspx.cs" Inherits="CodeBehindExample" %> <!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 > <title>Code-Behind Example</title> </head> <body> <form id="form1" > <div> <asp:Label id="Label1" ></asp:Label> </div> </form> </body> </html>Public Class CodeBehindExample Inherits System.Web.UI.Page Protected Label1 As System.Web.UI.WebControls.Label Protected Sub Page_Load(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles Me.Load Dim uc As CodeBehindExampleUserControl = _ CType(LoadControl("~/CodeBehindExampleUserControl.ascx"), _ CodeBehindExampleUserControl) Label1.Text = CType(uc.FindControl("Label2"), _ System.Web.UI.WebControls.Label).Text End Sub End Classpublic class CodeBehindExample : System.Web.UI.Page { protected System.Web.UI.WebControls.Label Label1; protected void Page_Load(object sender, System.EventArgs e) { CodeBehindExampleUserControl uc = (CodeBehindExampleUserControl)LoadControl ("~/CodeBehindExampleUserControl.ascx"); Label1.Text = ((System.Web.UI.WebControls.Label) uc.FindControl("Label2")).Text; } }請注意,對於在 .aspx 網頁上宣告的 Label 控制項而言,程式碼後置檔案中會具有對應的宣告。此外,程式碼後置類別是一般類別定義而非部分類別。
請建立使用 @ Control 指示詞之 CodeBehind 屬性的使用者控制項,如下列程式碼範例所示。
<%@ Control Language="VB" AutoEventWireup="false" CodeBehind="CodeBehindExampleUserControl.ascx.vb" Inherits="CodeBehindExampleUserControl" %> <asp:Label id="Label2" > Label text in user control. </asp:Label><%@ Control Language="C#" AutoEventWireup="false" CodeBehind="CodeBehindExampleUserControl.ascx.cs" Inherits="CodeBehindExampleUserControl" %> <asp:Label id="Label2" > Label text in user control. </asp:Label>Public Class CodeBehindExampleUserControl Inherits System.Web.UI.UserControl Protected Sub Page_Load(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles Me.Load ' User control code. End Sub End Classpublic class CodeBehindExampleUserControl : System.Web.UI.UserControl { protected void Page_Load(object sender, System.EventArgs e) { // User control code. } }請注意,與 Web 網頁的程式碼後置檔案很類似之處在於,使用者控制項的程式碼後置檔案會使用類別而非部分類別。
請編譯 Web 網頁和使用者控制項,然後將程式碼程式庫放置於應用程式的 Bin 資料夾中。使用 CodeBehind 屬性時,會要求您將所有程式碼後置類別編譯成單一程式碼程式庫並將產生的 .dll 檔放置於 Bin 資料夾中。對於 Web 網頁和使用者控制項範例而言,編譯這兩個程式碼後置檔案的編譯陳述式為:
vbc /target:library /nologo /out:bin\CodeBehindExample.dll /r:System.dll,System.Web.dll CodeBehindExample.aspx.vb CodeBehindExampleUserControl.ascx.vbcsc /target:library /nologo /out:bin\CodeBehindExample.dll CodeBehindExample.aspx.cs CodeBehindExampleUserControl.ascx.cs在瀏覽器中要求 Web 網頁 CodeBehindExample.aspx,以便確定它能是否在 .NET Framework 2.0 中運作。
若要將使用 CodeBehind 屬性的 Web 網頁和使用者控制項轉換成 ASP.NET 2.0 程式碼後置模型
請對 Web 網頁的 .aspx 檔進行變更,以符合下列程式碼範例。
<%@ Reference Control="~/CodeBehindExampleUserControl.ascx" %> <%@ Page Language="VB" AutoEventWireup="false" CodeFile="CodeBehindExample.aspx.vb" Inherits="CodeBehindExample" %> <!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 > <title>Code-Behind Example</title> </head> <body> <form id="form1" > <div> <asp:Label id="Label1" ></asp:Label> </div> </form> </body> </html><%@ Reference Control="~/CodeBehindExampleUserControl.ascx" %> <%@ Page Language="C#" AutoEventWireup="true" CodeFile="CodeBehindExample.aspx.cs" Inherits="CodeBehindExample" %> <!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 > <title>Code-Behind Example</title> </head> <body> <form id="form1" > <div> <asp:Label id="Label1" ></asp:Label> </div> </form> </body> </html>然後就會使用 CodeFile 屬性來取代 .aspx 檔中 @ Page 指示詞的 CodeBehind 屬性。@ Reference 指示詞是用來參考程式碼後置頁面中的使用者控制項。
請對您 Web 網頁的程式碼後置檔案進行變更,以符合下列程式碼範例。
Partial Class CodeBehindExample Inherits System.Web.UI.Page Protected Sub Page_Load(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles Me.Load Dim uc As CodeBehindExampleUserControl = _ CType(LoadControl("~/CodeBehindExampleUserControl.ascx"), _ CodeBehindExampleUserControl) Label1.Text = CType(uc.FindControl("Label2"), _ System.Web.UI.WebControls.Label).Text End Sub End Classpublic partial class CodeBehindExample : System.Web.UI.Page { protected void Page_Load(object sender, System.EventArgs e) { CodeBehindExampleUserControl uc = (CodeBehindExampleUserControl)LoadControl ("~/CodeBehindExampleUserControl.ascx"); Label1.Text = ((System.Web.UI.WebControls.Label) uc.FindControl("Label2")).Text; } }在程式碼後置的檔案中,部分類別是用來定義 .aspx 網頁的其他編碼。如同先前以 CodeBehind 屬性為基礎的程式碼後置網頁一樣,現在不需要宣告 .aspx 網頁上使用的 Label 控制項。
請對使用者控制項的 .aspx 檔進行變更,以符合下列程式碼範例。
<%@ Control Language="VB" AutoEventWireup="false" CodeFile="CodeBehindExampleUserControl.ascx.vb" Inherits="CodeBehindExampleUserControl" %> <asp:Label id="Label2" > Label text in user control. </asp:Label><%@ Control Language="C#" AutoEventWireup="true" CodeFile="CodeBehindExampleUserControl.ascx.cs" Inherits="CodeBehindExampleUserControl" %> <asp:Label id="Label2" > Label text in user control. </asp:Label>與 Web 網頁類似之處在於,使用者控制項現在會使用 CodeFile 屬性來取代 CodeBehind 屬性。
請對您使用者控制項的程式碼後置檔案進行變更,以符合下列程式碼範例。
Partial Class CodeBehindExampleUserControl Inherits System.Web.UI.UserControl Protected Sub Page_Load(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles Me.Load ' User control code. End Sub End Classpublic partial class CodeBehindExampleUserControl : System.Web.UI.UserControl { protected void Page_Load(object sender, System.EventArgs e) { // User control code. } }與 Web 網頁程式碼後置檔案類似之處在於,使用者控制項的程式碼後置檔案現在會使用部分類別。
從 Bin 資料夾中刪除先前程序建立的 CodeBehindExample.dll 檔。
ASP.NET 2.0 將會在首次要求時,編譯您的網頁和程式碼後置的檔案。
要求 Web 網頁 CodeBehindExample.aspx 並確定它是否如同之前般運作。