although ASP.NET 2.0 still supports both of these models, several significant changes have been made.
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.
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