网站导航免费论文 原创论文 论文搜索 原创论文 网学软件 学术大家 资料中心 会员中心 问题解答 原创论文 大学论文导航 设计下载 最新论文 下载排行 原创论文 论文源代码
返回网学首页
网学联系
最新论文 推荐专题 热门论文 素材专题
当前位置: 网学 > 编程文档 > ASP.net > 正文

MigratingfromASP.NET1.xtoASP.NET2.0

来源:http://myeducs.cn 联系QQ:点击这里给我发消息 作者: 用户投稿 来源: 网络 发布时间: 12/10/12
tance issues and forced the developer to keep track of two files for each Web page.

although ASP.NET 2.0 still supports both of these models, several significant changes have been made.

Code Inline

the code inline model is now the default model for Visual Studio 2005. Any code you add to the page will automatically be added to a <script> block within the ASPX file instead of to a code-behind class. However, Visual Studio 2005 still displays the code in the code view. In other words, you can keep using Visual Studio like you always have, except that code will be placed directly in the ASPX page instead of a separate class.

figure 1a and 1b. Switching views

note that the code is still separated from the content through <script> blocks and placement in the file. However, as a developer, you only have to worry about one file now instead of synchronizing two separate files.

Code Behind

if you want to work with a separate code file, you have to create the file when you create the ASPX page. Fortunately, creating a code-behind file is as simple as clicking a checkbox when you decide to create a new page.

figure 2. Creating a code-behind file

the primary difference between a code-behind file in ASP.NET 1.x and ASP.NET 2.0 is that a code-behind file is now a partial class rather than a full class that inherits from the ASPX page. A partial class is a new .NET construct that allows you to define a single class in multiple source files. In ASP.NET 2.0, a partial class is particularly useful for code-behind files, as it removes the inheritance relationship that is present with the older code behind model.

figure 3. Code behind, old and new

the two partial classes (ASPX and code behind) are merged into a single class during compilation. The code-behind file is therefore free of all of the control declarations and inheritance issues associated with the old code-behind model. The most striking difference can be seen in the actual code-behind file, which no longer contains all of the auto-generated code that used to be necessary to maintain the inheritance relationship.

an old code-behind file contained an initialization region, as well as initialization code for every control placed on the page:

  public class WebForm1 : System.Web.UI.Page  {    protected System.Web.UI.WebControls.Label Label1;    private void Page_Load(object sender, System.EventArgs e) {    }    #region Web Form Designer generated code      override protected void OnInit(EventArgs e)      {        InitializeComponent();        base.OnInit(e);      }      private void InitializeComponent()      {            this.Load += new System.EventHandler(this.Page_Load);      }    #endregion    void Page_Load(object sender, EventArgs e)    {      Label1.Text = "Hello ASP.NET 2.0";    }  }}

a new file removes all of this code since the controls are declared in the ASPX page (the other half of the partial class):

namespace ASP {  public partial class Webform1_aspx  {    void Page_Load(object sender, Event	 
			

网学推荐

免费论文

原创论文

设为首页 | 加入收藏 | 论文首页 | 论文专题 | 设计下载 | 网学软件 | 论文模板 | 论文资源 | 程序设计 | 关于网学 | 站内搜索 | 网学留言 | 友情链接 | 资料中心
版权所有 QQ:3710167 邮箱:3710167@qq.com 网学网 [Myeducs.cn] 您电脑的分辨率是 像素
Copyright 2008-2015 myeducs.Cn www.myeducs.Cn All Rights Reserved 湘ICP备09003080号