Chapter 6. Programming Web Forms

In the previous chapter, you learned many of the details about using ASP server controls in Web Forms. In this chapter, you will learn techniques that will 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

Code-Behind

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

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

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.