Chapter 6. Programming Web Forms

In Chapter 5, you learned many of the details about using ASP server controls in Web Forms. In this chapter, you will learn techniques to help you utilize the full power of ASP.NET in creating Web Forms, including:

  • Using code-behind to segregate the presentation code from the logic

  • Understanding the control lifecycle of a web page

  • Managing state in ASP.NET

  • Using Visual Studio .NET as a development tool


In traditional ASP, the interweaving of script with HTML can produce source control nightmares and difficult-to-maintain ASP pages. ASP.NET addresses this problem by giving programmers the ability to separate the executable code from the presentation code. You write the HTML in a page file (with a .aspx extension), and you write the C# or VB.NET code in the code-behind file (with a .cs or .vb extension, depending on its language), which is another way of saying the “code file behind the form.”

In the code-behind file, you create a class (which can be any class derived from the Page class) that serves as the base class for the web page you create in the .aspx file. This relationship between your class and the web page is established by a Page directive at the top of the .aspx file:

<%@ Page inherits="CodeBehindDemo" %>

The inherits attribute identifies the class created in the code-behind file from which this .aspx file will derive.

When a web form is compiled, its page is parsed and a new class is generated and compiled. This new class derives ...

Get Programming ASP.NET, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.