Compartilhar via


ASP.NET Page Class Overview

When an ASP.NET page is requested and renders markup to a browser, ASP.NET creates an instance of a class that represents your page. That class is composed not only of the code that you wrote for the page, but also code that is generated by ASP.NET. This topic provides an overview of the code that is generated by ASP.NET.

Generating and Running the Page Class Code

An ASP.NET page runs as a unit, combining the server-side elements in a page, such as controls, with the event-handling code you have written. If you use a Web site project, you do not have to precompile pages into assemblies. ASP.NET dynamically compiles pages and runs them the first time they are requested by a user. If there are any changes to the page or resources the page depends on, the page is automatically recompiled.

Web site projects also support precompilation of a Web project to enhance performance (for the first time a page request is received) and perform error checking as well as to support site deployment. For more information, see ASP.NET Web Site Precompilation Overview

ASP.NET Web application projects must be explicitly compiled before they are deployed. For more information about differences in compilation between Web site projects and Web application projects, see Web Application Projects versus Web Site Projects in Visual Studio.

The class or classes that the compiler creates depends on whether the page uses the single-file model or the code-behind model.

Single-File Pages

In a Web site project, you can create single-file pages. In a single-file page, the markup, server-side elements, and event-handling code are all in a single .aspx file. When the page is compiled, the ASP.NET generates and compiles a new class that derives from the base Page class or from a custom base class defined with the Inherits attribute of the @ Page directive. For example, if you create a new ASP.NET Web page named SamplePage1 in your application's root directory, a class named ASP.SamplePage1_aspx is generated that derives from the Page class. For pages in application subfolders, the subfolder name is used as part of the generated class. The generated class contains declarations for the controls in the .aspx page and contains your event handlers and other custom code.

The generated class is compiled into an assembly, and when the page is requested, the assembly is loaded into the application domain, and then the page class is instantiated and executed to render output to the browser.

For a Web site project, if you make changes to the page that would affect the generated class—whether by adding controls or modifying your code—the compiled class code is invalidated and a new class is generated. For more information on compilation in Web site projects and in Web application projects, see ASP.NET Compilation Overview and Web Application Projects versus Web Site Projects in Visual Studio.

The following illustration shows the inheritance model for the page class in a single-file ASP.NET Web page:

Class hierarchy, single-file page

Code-Behind Pages

Code-behind pages are the default in Web application projects and are optional in Web site projects. In the code-behind model, the page's markup and server-side elements, including control declarations, are in an .aspx file, while your page code is in a separate code file. The code file contains a partial class—that is, a class declaration with the keyword partial (Partial in Visual Basic) indicating that it contains only some of the total code that makes up the full class for the page. In the partial class, you add the code that your application requires for the page. This typically consists of event handlers, but can include any methods or properties that you need.

The inheritance model for code-behind pages is slightly more complex than that for single-file pages. The model is this:

  1. The code-behind file contains a partial class that inherits from a base page class. The base page class can be the Page class, or it can be another class that derives from Page.

  2. The .aspx file contains an Inherits attribute in the @ Page directive that points to the code-behind partial class.

  3. When the page is compiled, ASP.NET generates a partial class based on the .aspx file; this class is a partial class of the code-behind class file. The generated partial class file contains declarations for the page's controls. This partial class enables your code-behind file to be used as part of a complete class without requiring you to declare the controls explicitly.

  4. Finally, ASP.NET generates another class that inherits from the class generated in Step 3. This second generated class contains the code required to build the page. The second generated class and the code-behind class are compiled into an assembly that runs to render output to the browser.

The following illustration shows the inheritance model for the page class in a code-behind ASP.NET Web page:

Class hierarchy, code-behind page

See Also

Concepts

ASP.NET Web Forms Page Code Model