Click to Rate and Give Feedback
MSDN
MSDN Library
Visual Studio 2008
Visual Studio
 ASP.NET Page Class Overview

  Switch on low bandwidth view
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
ASP.NET
ASP.NET Page Class Overview

When an ASP.NET page is requested and renders markup to a browser, the code that runs is not solely the code that you created for your page. Instead, at run time, ASP.NET generates and compiles one or more classes that actually perform the tasks required to run the page. This topic provides an overview of the code that is generated at run time.

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. 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. The class or classes that the compiler creates depends on whether the page uses the single-file model or the code-behind model.

ASP.NET also supports precompilation of a Web site to enhance performance and perform error checking as well as to support site deployment. For more information, see ASP.NET Web Site Precompilation Overview. Precompilation works for both single-file and code-behind page models, and the compiler output is the same for both models.

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 compiler generates and compiles a new class that derives from the base Page class or 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 then a new class named ASP.SamplePage1_aspx is derived from the Page class. For pages inside of 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.

After page generation, the generated class is compiled into an assembly, the assembly is loaded into the application domain, and then the page class is instantiated and executed to render output to the browser. 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 ASP.NET, see ASP.NET Compilation Overview.

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

Class hierarchy, single-file page

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
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
More about the page code model and events      CSharpUniversity.com   |   Edit   |   Show History
To read more about the ASP.NET page code model and importants ASP.NET events go to: http://www.csharpuniversity.com/category/beginning-web-programming/
Tags What's this?: Add a tag
Flag as ContentBug
Custom Page Classes      bmains   |   Edit   |   Show History

The ASP.NET page architecture is extensible too. It's possible to create an intermediary page that sits between the base System.Web.UI.Page class, and the final ASPX page. By creating the following class:

public abstract class PageBase : System.Web.UI.Page
{
}

And having all of the ASPX pages inherits from:

public partial class MyPage : PageBase

{
}

It becomes easy to interject custom code specificly for your application. You can provide central access to objects, properties, or other information to all of the pages within your site. For instance, suppose we modified the PageBase class as:

public abstract class PageBase : System.Web.UI.Page
{
public string ApplicationName
{
get { return "My App"; }
}

public abstract string PageName
{
get;
}
}

In this example, the base class exposes the ApplicationName property to all of the pages that inherit from it, while requiring pages to implement the page name property. Our new ASPX page code-behind class now looks like the following.

public partial class MyPage : PageBase
{
public override string PageName
{
get { return "My Page"; }
}

protected override void OnInit(EventArgs e)
{
base.OnInit(e);
Response.Write(this.ApplicationName);
}
}

So the new code-behind class makes use of these properties of the PageBase class. As long as all of the pages from an ASP.NET application use this class, this class provides a central place to put global objects and information.
Tags What's this?: Add a tag
Flag as ContentBug
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker