更新:2007 年 11 月
本主题演示如何创建一个使用 @ Page 指令的 CodeBehind 属性的网页和用户控件,编译它们,然后将它们转换为使用 CodeFile 属性和 .NET Framework 2.0 版中的代码隐藏文件中的分部类。
ASP.NET 2.0 版中新的网页代码隐藏模型基于分部类。页的标记存储在一个文件(.aspx 文件)中,代码定义在一个分部类(代码隐藏文件)中。将现有网页转换到新的代码隐藏模型允许更大程度的标记和代码分离,因为不必在分部类中包含实例变量或显式事件绑定。
使用 @ Page 指令的 CodeBehind 和 Inherits 属性的网页仍然可以在 .NET Framework 2.0 中使用。但是,必须编译代码隐藏文件并将结果程序集放在 Bin 文件夹中。
如果选择迁移到 ASP.NET 2.0 代码隐藏模型,则必须对 .aspx 文件和代码隐藏文件进行协同更改。在 .aspx 文件中,您将把 CodeBehind 属性替换为 CodeFile 属性。在代码隐藏文件中,您将使用分部类。使用新的代码隐藏模型的优点在于不必显式编译代码隐藏文件,因为 ASP.NET 编译器会自动编译这些文件。
此外,在使用新的代码隐藏模型时,必须使用 @ Register 指令添加对其他代码隐藏文件的引用。
若要访问在下面的过程中创建的网页,需要在 Microsoft Internet 信息服务 (IIS) 中创建一个虚拟目录。有关创建 IIS 虚拟目录的详细信息,请参见如何:在 IIS 5.0 和 6.0 中创建和配置虚拟目录。
编译使用 CodeBehind 属性的网页和用户控件
创建一个使用 @ Page 指令的 CodeBehind 属性的网页,如下面的代码示例所示。
<%@ 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. } }
注意,与网页的代码隐藏文件类似,用户控件的代码隐藏文件使用类而不是分部类。
编译该网页和用户控件,并将代码库放到应用程序的 Bin 文件夹中。使用 CodeBehind 属性要求将所有代码隐藏类编译为单个代码库,并将结果 .dll 文件放在 Bin 文件夹中。对于该网页和用户控件示例,同时编译这两个代码隐藏文件的编译语句为:
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
在浏览器中请求网页 CodeBehindExample.aspx 以验证它是否能在 .NET Framework 2.0 中运行。
将使用 CodeBehind 属性的网页和用户控件转换到 ASP.NET 2.0 代码隐藏模型
对网页的 .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 指令中使用了 CodeFile 属性而不是 CodeBehind 属性。@ Reference 指令用于引用代码隐藏页中的用户控件。
对网页代码隐藏文件进行更改以匹配下面的代码示例。
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>
与网页类似,该用户控件现在使用 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 Class
public partial class CodeBehindExampleUserControl : System.Web.UI.UserControl { protected void Page_Load(object sender, System.EventArgs e) { // User control code. } }
与网页的代码隐藏文件类似,该用户控件的代码隐藏文件现在使用一个分部类。
从 Bin 文件夹中删除在前述过程中创建的 CodeBehindExample.dll 文件。
ASP.NET 2.0 将在发出第一次请求时连同代码隐藏文件一起编译该页。
请求网页 CodeBehindExample.aspx 并验证它是否像以前一样可正常使用。