ASP.NET Page Object Model
In this tutorial you will learn about Page Object Model, the Attributes of the @Page directive, the properties of the Page Class, Methods of the Page Class, Page Class Events and the Page Scripting Object Model.
The page class is represented by the .aspx file in ASP.NET 2.0. It provides the basic behaviour for all pages. A class is dynamically created when the .aspx file is parsed and this class inherits from the page class. In ASP.NET 2.0, the page class allows code separation. It invokes the IHTTPHandler interface to service the requests for the page. The page class also inherits from the TemplateControl class and so functions like a control. Since it implements the IPaginationContainer interface the page is paginated by the new Pager control.
The Attributes of the @Page directive
It is interesting to note that the behaviour of each page can be controlled using the attributes of the @Page directive. ASP.NET has not revolutionized the Page class, but it has enhanced it by adding new features to it.
The @Page directive is the point of control for the developer. The new attributes that have been added to this directive are:
1. Async: If this is set to true, the page class that is generated derives from IHttpAsyncHandler. It adds some built in asynchronous capabilities to the page.
2. CompileWeb: This attribute specifies the name of the referenced code-beside file to use for the page.
3. EnablePersonalization: Indicates whether profile information should be used to build the page.
4. MasterPageFile: This attribute specifies the path of the master to use for building the current page.
5. PersonalizationProvider: this attribute specifies a valid provider defined in the application’s configuration file.
6. Theme: specifies the name of the theme to be used.
If Boolean Async directive is used ASP.NET serves up the page in a Asynchronous way. The page executes the custom code Asynchronously while runtime progresses on the page life cycle. The request threads are synchronized and the output is generated in the browser by setting a single unwind point on the page between PreRender and PreRenderComplete events.
The properties of the Page Class
The page object has two kinds of properties–the intrinsic objects and the page specific properties. Environmental stand alone objects such as cache, User, Trace and HTTP objects such as Session, Application, Request etc fall within the Intrinsic object properties of the page class. Page specific properties would include IsPostBack, EnableViewState, and SmartNavigation.
Some new properties that have been added to the intrinsic objects are:
2. The Header is an instance of HtmlHead class and represents the contents of the page’s < head > block if it is marked as runat=server.
3. Master is the attribute that gets the master page and determines the overall look of the page.
4. Pager is an instance of the pager control and it paginates the contents of the current page.
5. SiteCounters is an instance of the SiteCounters class and it represents the built in service to trackpage usage within the application.
The other new properties of the Page class include:
1. EnablePersonalization which sets and gets any profile information required to build the page.
2. EnableTheming which helps configure the page to ignore themes.
3. IsAsync indicates whether a page is processed asynchronously.
4. IsCrossPagePostBack indicates whether the page is being loaded in response to a client postback requested by a different page. ASP.NET 2.0 pages no longer has the need to post to themselves. The unique HTML form contents of the pages can be automatically posted to other pages. Using the IsPostBack and IsCrossPostBack properties the pages distinguish between the page and the cross-page postback. In the latter case the values are retrieved from the original controls and the reference to the posting page is returned by the PreviousPage.
5. IsPagePersonalized provides details of profile information for the page and indicates whether this is being currently used.
6. MasterPageFile is used to get or set the file name of the master page for the current page.
7. MaximumWeight is used to get or set the maximum size of each page of content when the pager control is used to paginate the .aspx page.
8. PersonalizationMode defines the mode of personalization by using a value taken from the enumeration.
9. PreviousPage is used to return the object to the previously visited page object when cross page posting is done.
10. Title is used to get and set the string that represents the title of the page.
Methods of the Page Class:
A few new methods have been added to the page class. Most of them have been inherited from the Control class.
1. EnsureID provides the current page object with an id.
2. Focus is intended to give the page an input focus.
4. GetValidation returns the collection of all validator controls that belong to a group.
5. GetWebResourceControlState registers the specified control for control state management
6. SetFocus sets the input focus of the control on the page
7. TestDeviceFilter checks whether the current browser is of the specified type.
Page Class Events
Events are written by developers to dynamically modify the page output and the controls on the page. ASP.NET helps developers by defining a number of new events so that developers can follow the request processing closely.
The new events added to the page class are:
1. InitComplete is called when the page is initialized and the process is complete.
2. LoadComplete as the name suggests is called at the end of the load stage of the page’s life cycle.
3. PreInit is called before the page is initialized
4. PreLoad is called before the loading of the page is complete.
5. PreRenderComplete is called when the prerendering phase is complete and all the child controls have been created. After the event, the personalization data and the view state are saved and the page is rendered to HTML.
The new methods, events and properties of the page class have streamlined the developer’s control over the page class. The ASP.NET 2.0 developer is now presented with an environment that makes for rapid application development and provides very good control over the output from the application.
The Page Scripting Object Model
In this section of the tutorial we have focused on the Page class, its properties, methods and events. In the next section we shall examine the Server Controls that are offered up by ASP.NET 2.0.