O'Reilly logo

Programming ASP .NET by Dan Hurwitz, Jesse Liberty

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required