.netCoders Contact Us
Search:

ASP.NET Pages

There are many things to know about creating ASP.NET pages, which you will learn while studying for the Web Applications exam. Here we'll cover directives in ASP.NET pages, and the code-behind method of separating the user interface from the business logic.

Directives

Directives are used to configure settings for an ASP.NET page (aspx) or control (ascx). They are identified using the <%@ ... > syntax. There are 8 different kinds of page directives, as indicated in the following table.

Page Defines page-specific attributes used by the ASP.NET page parser and compiler. Can only be included in .aspx files.
Control Defines control-specific attributes used by the ASP.NET page parser and compiler. Can only be included in .ascx files (user controls).
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.
Register Associates aliases with namespaces and class names, thereby allowing user controls and custom server controls to be rendered when included in a requested page or user control.
Assembly Declaratively links an assembly to the current page or user control.
OutputCache Declaratively controls the output caching policies of a page or user control.
Reference Declaratively links a page or user control to the current page or user control.

The following ASP.NET page shows these directives in use:

<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="DotnetCoders.WebForm1" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Register TagPrefix="DotnetCoders" TagName="Header" Src="~/Framework/Header.ascx" %>
<%@ Register TagPrefix="DotnetCoders" TagName="Footer" Src="~/Framework/Footer.ascx" %>

<html><body>Hello World</body></html>
You should familiarize yourself with the details of each directive by consulting the additional resources found at the end of this page.

Code-Behind

In classic ASP, you typically would write ASP code intermingled with the HTML content. In ASP.NET, the new recommended approach is to separate out the business logic into a C# class file (.cs) and leave the HTML code in the ASP.NET page (.aspx). This is termed "Code-Behind". How it works is that you create a class deriving from System.Web.UI.Page. Then, in your ASP.NET page, you use the Page directive and the Inherits attribute to indicate that the ASP.NET page derives from your class. Visual Studio.NET uses the Codebehind attribute in the page directive to match the source code file for your page. When you compile your application, the code-behind class is compiled into the application's assembly (ie. the dll). This increases performance, and keeps your business logic source code from prying eyes.

Let's look at what Visual Studio.NET does when you add a new Web Form to your project. You'll noticed in the aspx file below that the Page directive's codebehind and implements attributes indicate that this page is derived from DotnetCoders.Web.CodeBehind in the CodeBehind.aspx.cs class file.

<%@ Page language="c#" Codebehind="CodeBehind.aspx.cs" AutoEventWireup="false" Inherits="DotnetCoders.Web.CodeBehind" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<html>
    <head>
        <title>CodeBehind</title>
    </head>
    <body>
        <form id="CodeBehind" method="post" runat="server">
        </form>
    </body>
</html>
Now let's look at the C# file behind the scenes. To see the .cs files in the Solution Explorer, select Project->Show All Files. Expand the plus sign next to the web form, and you'll see the .cs file. Double-click it to open it. You'll see that the class referred to in the ASP.NET page is defined here.
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace DotnetCoders.Web
{
    /// <summary>
    /// Summary description for CodeBehind.
    /// </summary>
    public class CodeBehind : System.Web.UI.Page
    {
        private void Page_Load(object sender, System.EventArgs e)
        {
            // Put user code to initialize the page here
        }

        #region Web Form Designer generated code
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            InitializeComponent();
            base.OnInit(e);
        }
        
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {    
            this.Load += new System.EventHandler(this.Page_Load);
        }
        #endregion
    }
}

Additional Resources

  • Page Directive Syntax