ASP.NET Basics

HTML Basics
ASP.NET Web Server
Advanced HTML
Application Designing
Using Visual Studio
ASP.NET Standards
ASP.NET Styling
ASP.NET Navigation

ASP.NET Tips

ASP.NET Validation
HTML forms
CSS Styling
CSS Advanced
ASP.NET Features
ASP.NET Image Effects
Common mistakes
DB Design tips

Building Applications

Design Secure Apps
Build Secure Apps

1. The Code-Behind Model

« Previous Chapter

Next Chapter »

As you would already have known, ASP.NET has always supported a programming technique called the Code-Behind, but ASP.NET 2.0 introduces a new programming technique with some important changes to the way code-behind works.

Code-Behind gives you the ability to separate the code that defines the appearance of a Web page from the code that executes in response to events.

For example:

The loading of a page or clicking of a button.

The code that defines a page’s appearance is stored in as aspx file, which includes both the HTML and ASP tags and has the extension “.aspx”.

For example:

The aspx file for a page named “default” is “default.aspx”.

The file that contains the code that’s run in response to page events is called the Code-behind file. It has the extension “.vb” or “.cs”, depending on the programming language being used.

For example:

If the language is Visual Basic, the code-behind file for the “default.aspx” page is “default.aspx.vb”.
If the language is C#, the code-behind file for the “default.aspx” page is “default.aspx.cs”.

In other words, there are two files for each page of an ASP.NET application, they are:

  1. An “.aspx” file that defines the appearance of the page
  2. A Code-behind file, “.aspx.vb” or “.aspx.cs”, which provides the executable code for the page.

For a simple programming perspective, the code-behind file in ASP.NET 2.0 works pretty much the same as it does in ASP.NET 1 x.

For example:

If you double-click a button in Visual Studio’s Design view, the code editor opens and Visual Studio generates a method that handles the click event for the button. Then this method executes whenever a user clicks the button.

But what is actually happening behind is very different in ASP.NET 2.0 from ASP.NET 1 x. The complicated details depend on a new feature of ASP.NET 2.0 called Partial classes. Partial Classes are capable of splitting the code that defines a class into two or more source files.

The new Code-behind Model has one very practical and important advantage that is the code-behind file in ASP.NET 2.0 does not have any code that is generated by Visual Studio. In ASP.NET 1 x, the code-behind file has a hidden region of code called the Web Form Designer Generated Code. This was required to keep the code-behind file synchronized with the “.aspx” file which sometimes made it possible for the “.aspx” file and the code-behind file to fall out of sync with each other.

For example:

If you deleted or changed the name of the control in the “.aspx” file, the corresponding definition for the control in the code-behind file might not be deleted or changed. This then sometimes gives a compile error when you try to run the page. This type of problem occurs less frequently in Visual Studio 2005 because the code-behind file does not include any generated code.

ASP.NET 2.0 also provides a Code-beside model, in which the C# and VB code is embedded within the “.aspx” file rather than stored as a partial class in a separate file. To use the code-beside model, all you have to do is uncheck the Place Code in Separate File option in the dialog box that appears when you create a New Page. As a general rule the code-behind is more preferred to code-beside because code-behind provides better separation of the application’s presentation and logic code. But you can also use code-beside when developing simple and smaller projects. Here at ASPNET Book, all the applications use code-behind

« Previous Chapter

Next Chapter »

Table of Contents

» Chapter 1 - The Code-Behind Model
Chapter 2 - App_Folders
Chapter 3 - Master Pages
        Chapter 3.1 - How to create a Default Master Page
        Chapter 3.2 - How to create a Master Page
        Chapter 3.3 - How to create a Default Content Page
Chapter 4 - Data Controls
        Chapter 4.1 - The SQL Data Source Control
                Chapter 4.1.1 - Other Data Sources in ASP.NET 2.0
        Chapter 4.2 - The GridView Control
        Chapter 4.3 - The DetailsView Control
        Chapter 4.4 - The FormView Control
Chapter 5 - Login Controls
Chapter 6 - The Wizard Control
Chapter 7 - Navigation Control
Chapter 8 - Web Parts
Chapter 9 - Themes
Chapter 10 - The Web Site Administration Tool

[top]