ASP.NET Code Behind PagesBy Mani Raja
|Learn More About ASP.NET!|
|To learn more about ASP.NET, be sure to check out our listing of ASP.NET Articles! You'll find links to great ASP.NET articles sprinkled all over the Web!|
Even though ASP.NET is the next version of ASP, it is more than just a next version. ASP.NET is completely re-designed from the ground up and it provides a neat object oriented programming model. An ASP.NET page is an object derived from the .NET Page class. We all know that ASP.NET provides several new features and one of the important features is the separation of code and content.
In today's ASP application, we have a mix of HTML and Scripts making the code difficult to read, debug and maintain. ASP.NET relieves us from this problem by promoting the separation of code and content. That is, the user interface and the user interface programming logic need not necessarily be written in a single page. There are two ways in which you can separate the code and content:
1.) By using Code-behind files that are pre-compiled modules written in any
of the Microsoft .NET runtime compliant languages.
2.) By developing the frequently used page logic in to separate files called Pagelets (also known as page controls) and by using them several times in your ASP.NET pages.
As the title of the article indicates, I will explain the first mechanism that is used to neatly encapsulate the UI functionality in to a separate pre-compiled module. You will typically follow the steps below to develop an ASP.NET page that uses code-behind pages:
1.) Create the User Interface using the HTML and/or Web controls and store
them in an ASP.NET page with extension ".aspx".
2.) Create the code-behind file using the .NET runtime classes that mimic the entire ASP.NET page with the member variables that correspond to the HTML and/or Web controls used in the ASP.NET page. The extension of code behind files will vary depending on the language you use. Typical extensions will be .cs for C#, .vb for VB7 and .js for JScript .NET
Now, we know that there are two separate files: one for the UI and the other for the UI logic. But, how do we relate these two files so that the ASP.NET page compiler will locate the code-behind file for the ASP.NET page. The glue between these two files is provided through the Page Directives, which are used to specify optional settings at the page level.
Creating the "Content"
Enough theory said about code-behind files. Let us get our hands on to the actual coding of a code-behind file. The ASP.NET page that displays the Web UI can be seen below. It uses a post-back form to query the user for username and information:
The first line of the ASP.NET page is the page directive that specifies the
name of the code behind file and the actual class name inside that file. In
our example, the file name is
CodeBehind.cs and the class name is
The namespace that I used for this code behind page is
provide the naming scope for the classes and is very convenient way to
represent hierarchy of classes.
The rest of the code consists of HTML tags and Web Control declaration tags.
The main thing to note down in this page is the ID properties of the
controls. I will explain the importance of these IDs when we discuss the
In Part 2 we'll examine creating the complementing code for our .aspx page!