更新 : 2007 年 11 月
このトピックは、@ Page ディレクティブの CodeBehind 属性を使用する Web ページとユーザー コントロールを作成し、コンパイルした後、.NET Framework Version 2.0 の CodeFile 属性と分離コード ファイルの部分クラスとを使用するように変換する方法を示します。
ASP.NET Version 2.0 の新しい Web ページの分離コード モデルは、部分クラスに基づいています。ページのマークアップは .aspx ファイル 1 つに格納され、コードは分離コード ファイルの部分クラスに定義されます。既存の Web ページを新しい分離コード モデルに変換すると、マークアップとコードとの分離がさらに向上します。これは、部分クラスではインスタンス変数や明示的なイベント バインディングをインクルードする必要がないからです。
@ Page ディレクティブの CodeBehind 属性と Inherits 属性を使用する Web ページは、.NET Framework 2.0 でも動作します。ただし、分離コード ファイルをコンパイルし、生成されたアセンブリを Bin フォルダに配置する必要があります。
ASP.NET 2.0 の分離コード モデルに移行する場合、.aspx ファイルと分離コード ファイルに協調的な変更を加える必要があります。.aspx ファイルでは、CodeBehind 属性を CodeFile 属性に置き換えます。分離コード ファイルでは、部分クラスを使用します。新しい分離コード モデルを使用することの利点は、ASP.NET コンパイラが自動的に分離コード ファイルをコンパイルするため、これらのファイルを明示的にコンパイルする必要がなくなることです。
また、新しい分離コード モデルを使用する場合、別の分離コード ファイルへの参照を @ Register ディレクティブを使用して追加する必要もあります。
次の手順で作成する Web ページにアクセスするには、Microsoft Internet Information Services (IIS) で仮想ディレクトリを作成する必要があります。IIS で使用する仮想ディレクトリの作成方法の詳細については、「方法 : 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 runat="server"> <title>Code-Behind Using the CodeBehind Attribute</title> </head> <body> <form id="form1" runat="server"> <div> <asp:Label id="Label1" runat="server"></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 runat="server"> <title>Code-Behind Example</title> </head> <body> <form id="form1" runat="server"> <div> <asp:Label id="Label1" runat="server"></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 Class
public 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" runat="server"> Label text in user control. </asp:Label>
<%@ Control Language="C#" AutoEventWireup="false" CodeBehind="CodeBehindExampleUserControl.ascx.cs" Inherits="CodeBehindExampleUserControl" %> <asp:Label id="Label2" runat="server"> 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 Class
public 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.vb
csc /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 runat="server"> <title>Code-Behind Example</title> </head> <body> <form id="form1" runat="server"> <div> <asp:Label id="Label1" runat="server"></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 runat="server"> <title>Code-Behind Example</title> </head> <body> <form id="form1" runat="server"> <div> <asp:Label id="Label1" runat="server"></asp:Label> </div> </form> </body> </html>
これで、.aspx ファイルの @ Page ディレクティブで、CodeBehind 属性ではなく CodeFile 属性が使用されます。@ 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 Class
public 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 ページで使用する追加コードの定義に部分クラスが使用されます。これで、.aspx ページで使用されている Label コントロールの宣言が不要になりました。CodeBehind 属性に基づいた従来の分離コード ページでは、この宣言が必要でした。
ユーザー コントロールの .aspx ファイルを次のコード例のように変更します。
<%@ Control Language="VB" AutoEventWireup="false" CodeFile="CodeBehindExampleUserControl.ascx.vb" Inherits="CodeBehindExampleUserControl" %> <asp:Label id="Label2" runat="server"> Label text in user control. </asp:Label>
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="CodeBehindExampleUserControl.ascx.cs" Inherits="CodeBehindExampleUserControl" %> <asp:Label id="Label2" runat="server"> Label text in user control. </asp:Label>
これで、Web ページの場合と同様に、ユーザー コントロールでは、CodeBehind 属性ではなく CodeFile 属性が使用されます。
ユーザー コントロールの分離コード ファイルを次のコード例のように変更します。
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 Class
public partial class CodeBehindExampleUserControl : System.Web.UI.UserControl { protected void Page_Load(object sender, System.EventArgs e) { // User control code. } }
これで、Web ページの分離コード ファイルの場合と同様に、ユーザー コントロールの分離コード ファイルでは、部分クラスが使用されます。
前述の手順で作成した CodeBehindExample.dll ファイルを Bin フォルダから削除します。
ページは、その分離コード ファイルと共に、最初の要求があったときに ASP.NET 2.0 によりコンパイルされます。
Web ページの CodeBehindExample.aspx を要求して、従来と同様に動作することを確認します。