|
ASP.NET Book
The online ASP.NET Tutorial Book |
|||||||||
|
|
|||||||||
ASP.NET BasicsHTML BasicsASP.NET Web Server Advanced HTML Application Designing Using Visual Studio ASP.NET Standards ASP.NET Styling ASP.NET Navigation ASP.NET TipsASP.NET ValidationHTML forms CSS Styling CSS Advanced ASP.NET Features ASP.NET Image Effects Common mistakes DB Design tips Building ApplicationsDesign Secure AppsBuild Secure Apps |
1. The Code-Behind Model |
||||||||
|
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”. In other words, there are two files for each page of an ASP.NET application, they are:
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 Table of Contents» Chapter 1 - The Code-Behind Model [top] |
|||||||||