
The Code-Behind Web Page Model
If you prefer the code-behind model, there are a few benefits. Code-behind pages offer a clean separation of
HTML markup and code. It is possible to have designers working on the mark up file while programmers
author the C# code. Code is not unnecessarily exposed to page designers or others who are working only with
the HTML mark up.
Consider the following C# and VB code, which now holds the button-click event handler within the code-
behind file.
public partial class _Default : System.Web.UI.Page
{
protected void btnFillData_Click(object sender, EventArgs e)
{
SqlConnection sqlConn = new SqlConnection
("Data Source=.;Database=Cars;integrated security=true");
SqlCommand cmd = new SqlCommand("SELECT * FROM Inventory", sqlConn);
sqlConn.Open();
SqlDataReader dr = cmd.ExecuteReader();
carsGridView.DataSource = dr;
carsGridView.DataBind();
dr.Close();
sqlConn.Close();
}
}
Partial Class _Default
Inherits System.Web.UI.Page
Sub btnFillData_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim sqlConn As New _
SqlConnection("Data Source=.;Database=Cars;integrated security=true")
Dim cmd As SqlCommand = _
New SqlCommand("SELECT * FROM Inventory", sqlConn)
sqlConn.Open()
Dim dr As SqlDataReader = cmd.ExecuteReader()
carsGridView.DataSource = dr
carsGridView.DataBind()
dr.Close()
sqlConn.Close()
End Sub
End Class
Here are some helpful points to keep in mind for both models. Additional files, properties, methods can also
be declared in the code-behind file. Web Application projects always use the code-behind model. C#
developers do not have access to the list of Page events when using the code-behind model. They can use
snippets to help create event handlers for the Page’s events.
Directives
ASP.NET *.aspx files (as well as other ASP.NET file types) typically begin with one or more directives.
These directives are used by the runtime compiler, and in some cases Visual Studio, to build and process a
page and its controls. Directives are often used to set public properties (name/value pairs) of whatever object
they are describing. If a given attribute is not set in a Directive, it will be assigned a default value.
Directives are always placed within <%@…%> tags. Good practice dictates that all @-directives be placed at
the top of the file, although they technically can be placed anywhere. Visual Studio will offer a listing of all
available Directives and their associated attributes through IntelliSense.
An ASP.NET file (ASPX, ASCX, etc.) can contain a good number of possible directives. You will see many
directives during the run of this class, however, here is a list of some of the more common directives. Consult
the NET Framework SDK documentation for full details regarding all directives and their attributes.
The Page directive is very important, and must be in every *.aspx file (regardless of the page architecture you
choose). At minimum, you will have a ‘language’ attribute which defines the .NET language used in server
side <script> blocks. You will most likely also be setting the name of code-behind file, base class, Theme,
Master page, Tracing, and enabling view state.
Here are some of the most popular attributes of the Page directive:
Recall that the Import directive is used to “bring in” a .NET namespace used within a single ASP.NET file.
You will not need to use this directive if you are making use of code-behind files, however! If this is the case,
simply use the C# ‘using’ or VB “Imports” keyword in your code-behind file. Be sure not to confuse the
single-file “Import” Page directive (C#, VB, etc.) with the VB “Imports” keyword.
Watch for the different directives that appear at the top of the files as you go through the course. You’ll see
different directives used with different files - .ascx, .aspx, .ashx, .master, etc.
The Code-Behind Web Page Model
Table of Contents
Copyright (c) 2008. Intertech, Inc. All Rights Reserved. This information is to be used exclusively as an
online learning aid. Any attempts to copy, reproduce, or use for training is strictly prohibited.
Courseware
Training Resources
Tutorials

ASP.NET Directive
|
Meaning in Life
|
@ Page
|
Defines page-specific attributes used by the ASP.NET page parser and compiler. Can be included only in .aspx files.
|
@ Control
|
Like a Page directive, the Control directive is used to define control-specific attributes for .ascx files (user controls). Only used with .ascx files.
|
@ Assembly
|
Declaratively references an assembly to the current page or user control, making all of its public members available.
|
@ Import
|
Explicitly imports a namespace into a page or user control.
|
@ Implements
|
Declaratively indicates that a page or user control implements a specified .NET Framework interface.
|
@ OutputCache
|
Controls the page/control caching policies based on the query string.
|
@ Reference
|
Declaratively links a page or user control to the current page or user control.
|
@ Register
|
Used to register user controls and custom controls on an .aspx, .ascx, or .master pages with name-friendly aliases.
|
@ Master
|
The Master directive is only used with .master pages.
|
@ WebHandler
|
Only used by .ashx Web handler files, which are called directly and do not have a control tree.
|
|
Attribute of the Page Directive
|
Meaning in Life
|
AutoEventWireup
|
Enables or disables automatic handling of page level events based on the name of the control and of the event.
|
CodeFile CodeBehind (used for legacy support)
|
The name of the file that holds the definition of the base class, which can be partial.
|
EnableViewState
|
Turns view state on and off, which maintains state information across page requests in a hidden form field. The default is true.
|
Inherits
|
Defines a code-behind class or base class for the page to inherit. Can be any class derived from the Page class.
|
MasterPageFile
|
Defines the master page for the current .aspx file.
|
Strict
|
Indicates that the page should be compiled using the Visual Basic Option Strict mode. Ignored for C#-based pages.
|
Trace
|
These attributes control how tracing should be supported by the .aspx file.
|
Theme
|
Assigns a theme to the current .aspx file.
|
|
Services