oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button

Writing ASP.NET Web Forms with C#
Pages: 1, 2, 3, 4, 5, 6

Web Forms Programming

Every ASP.NET page, by default, inherits the Page class from the System.Web.UI namespace. If you don't supply the Inherits part in the following page directive in your page, the compiler will assume it.

<%@ Page language="C#" Inherits="System.Web.UI.Page" %>

Your page is compiled as a new class when the Web Form is compiled. The page will be recompiled after you edit it. As a result, the first call to a page is always much slower. Subsequent calls to the same page will use the compiled version and will be much faster because there is no time spent on compilation.

Because the class is derived from the ASP.NET Page class, the new class inherits the properties, methods, and events of the Page class. The Page class has events that become the processing stages of a Web Form page. If you want to understand ASP.NET thoroughly, you should study the Page class.

The Page Class

The Page class extends the System.Web.UI.TemplateControl class and implements the System.Web.IHttpHandler interface. This interface defines the contract that developers must implement to synchronously process HTTP web requests. The Page class has a number of important properties and methods that you should understand.

The Page Class' Properties

Among the properties are the Request, Response, Server, Application and Session properties that allow you to retrieve, respectively, the Request, Response, Server, Application and Session objects from the HTTP Runtime. If you are an ASP programmer, you should be familiar with these five ASP built-in objects. For instance, the Response object allows you to output a string to the browser, as you have seen at the beginning of the article.

Also important is the IsPostBack property. This property returns false if the current request is the first request from the user. Otherwise, it returns true. You can query the value of this property and later do a one-time initialization if the property returns false.

If you are an ASP programmer, this use of the IsPostBack property is equivalent to writing code in the Session_OnStart event procedure in the global.asa file. Another property is the IsUplevel property, which returns true if the request comes from a fourth generation browser, i.e., Internet Explorer 4.0 or above and Netscape Navigator 4.0 or above. Utilizing this property, you can have two sets of HTML pages, the standard pages and browser-specific pages. If you know for sure that the user is using IE 5.0, for example, you might want to send dynamic HTML to make your sites more attractive.

The Page Class' Methods

The Page class provides useful methods to perform tasks that in classic ASP are done using methods of the built-in objects. For example, to redirect the user to a different URL, you can use the Redirect method of the Response object. However, in ASP.NET you have a choice; you can also use the Navigate method to do the same thing. For example, the following redirects you to the MSDN Web site.


Note that you must write the "http" part if you are redirecting to an external Web site. Also, the MapPath method replaces the same method in the Server object. You can use this method to assign a virtual path, either relative or absolute, to a physical path.

Web Forms Processing Stages

There are a few stages that every Web Form page goes through. For each stage, the Page class provides one or more methods that you can use to work with the contents of the form.

The most frequently used stages are as follow.

  • Page_Load event -- This event is raised when the page loads. Use this event to read and restore the previously-stored values. Also, you can use the IsPostBack property to do one-time initialization for every user. This property is true if this is the first time a user requests this page.

  • Event Handling -- If the page was called in response to a form event, the corresponding event-handling method in the page is called during this stage. For example, the code in Listing 3 runs the Button1_Click event procedure during this stage.

  • Page_Unload event -- Use this event to do cleanup, such as closing files, closing database connections, or discarding objects. It is important that resource-hungry objects, such as database connections, are explicitly closed. This way, the objects do not remain in memory until the next garbage collection cycle.

The following example modifies the code in Listing 3 to greet the user when the user first comes to the page.

Listing 4: Writing code for the Page_Load event procedure

<title>Web Forms Login Application</title>
<script language="C#" runat="Server">

public void Page_Load() {
  if (!IsPostBack)
    message.InnerHtml="Welcome. " +
    "Please enter your user name and password in the boxes above";

void Button1_Click(object Source, EventArgs e) {
  if (userName.Value.Equals("kittyhawk") &&
    message.InnerHtml="Welcome Kitty Hawk.";
    message.InnerHtml="Login failed. Please try again.";

<form runat=server>
UserName: <input id="userName" runat=server>
<BR>Password: <input id="password" type=password runat=server>
<BR><input type=submit OnServerClick="Button1_Click" runat=server>
<p><span id="message" runat=server/>

Working with Server Controls

One of the benefits of ASP.NET is the availability of server controls that make Web Forms programming easy. These controls are specifically designed to work in the ASP.NET Framework and can be grouped into two general categories, HTML controls and Web controls. As you will see, each control has a base class representation of the .NET Framework type libraries. Every server control has the id attribute to allow you to refer to it programmatically. In a Web Form, this id must be unique.

Pages: 1, 2, 3, 4, 5, 6

Next Pagearrow