To read the article online, visit http://www.4GuysFromRolla.com/webtech/100500-1.shtml

ASP.NET Code Behind Pages

By 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:

<%@ Page language="c#" Codebehind="CodeBehind.cs" Inherits="ASPPlus.CodeBehind" %>

<HTML><BODY>
<form action="CodeBehind.aspx" method=post runat="server">

  <p align=center>Demonstration of Code Behind File</P>
  <p align=center><asp:Label id=Message runat="server"></asp:Label></P>
  <p>

  <h5>Sign-In Form</h5>
  Login: <asp:TextBox id=txtLogin Width=200 Runat=server />
  <asp:RequiredFieldValidator ControlToValidate="txtLogin"
        Display="Static" Font-Name="verdana,arial,sans-serif"
        Font-Size="9pt" ErrorMessage="You must enter a Login."
        runat="server" ID="RFVLogin" />

  <P>
  Password: <asp:TextBox id=txtPassword Width=200 Runat=server
               TextMode="Password" />
  <asp:RequiredFieldValidator ControlToValidate="txtPassword" 
        Display="Static" Font-Name="verdana,arial,sans-serif"
        Font-Size="9pt" ErrorMessage="You must enter a password."
        runat=server ID="RFVPassword"> </asp:RequiredFieldValidator>

  <P>
  <asp:Button id=btnSignIn Runat="server" 
              Text="Sign In" onClick="btnSignIn_Click" />
  </FORM>
</body></html>

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 ASPPlus.CodeBehind.

<%@ Page language="c#" Codebehind="CodeBehind.cs" Inherits="ASPPlus.CodeBehind" %>

The namespace that I used for this code behind page is ASPPlus. Namespaces 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 code-behind file.

In Part 2 we'll examine creating the complementing code for our .aspx page!

  • Read Part 2


  • Article Information
    Article Title: ASP.NET Code Behind Pages
    Article Author: Mani Raja
    Published Date: Thursday, October 05, 2000
    Article URL: http://www.4GuysFromRolla.com/webtech/100500-1.shtml


    Copyright 2017 QuinStreet Inc. All Rights Reserved.
    Legal Notices, Licensing, Permissions, Privacy Policy.
    Advertise | Newsletters | E-mail Offers