Code Behind vs. Code Inline
ASP.NET provides two ways that you can organize code within your pages.
Inline Code Separation
The example below demonstrates a simple ASP.NET page with three server controls, a TextBox, Button, and a Label. Initially these controls just render their HTML form equivalents. However, when a value is typed in the TextBox and the Button is clicked on the client, the page posts back to the server and the page handles this click event in the code of the page, dynamically updating the Text property of the Label control. The page then re-renders to reflect the updated text. This simple example demonstrates the basic mechanics behind the server control model that has made ASP.NET one of the easiest Web programming models to learn and master.
VB Inline Code Separation
Note that in the preceding example the event handler for the Button was located between
<script></script> tags in the same page containing the server controls. ASP.NET calls this type of page programming code-inline, and it is very useful when you want to maintain your code and presentation logic in a single file. However, ASP.NET also supports another way to factor your code and presentation content, called the code-behind model. When using code-behind, the code for handling events is located in a physically separate file from the page that contains server controls and markup. This clear delineation between code and content is useful when you need to maintain these separately, such as when more than one person is involved in creating the application. It is often common in group projects to have designers working on the UI portions of an application while developers work on the behavior or code. The code-behind model is well-suited to that environment.
Simplified Code Behind Model New in 2.0
ASP.NET 2.0 introduces an improved runtime for code-behind pages that simplifies the connections between the page and code. In this new code-behind model, the page is declared as a partial class, which enables both the page and code files to be compiled into a single class at runtime. The page code refers to the code-behind file in the
CodeFile attribute of the
<%@ Page %> directive, specifying the class name in the
Inherits attribute. Note that members of the code behind class must be either public or protected (they cannot be private).
VB CodeBehind Code Separation
The advantage of the simplified code-behind model over previous versions is that you do not need to maintain separate declarations of server control variables in the code-behind class. Using partial classes (new in 2.0) allows the server control IDs of the ASPX page to be accessed directly in the code-behind file. This greatly simplifies the maintenance of code-behind pages.