ASP. NET 3.5 core programming learning notes (2): page class

Source: Internet
Author: User

The system. Web. UI. Page class provides the basic behavior of all objects created by ASP. NET applications from the aspx file. This class is derived from templatecontrol and implements the ihttphandler interface.

Templatecontrol is an abstract class that provides a set of basic functions for ASP. NET pages and user controls. The previous level of this class is the control class. The control class defines the properties, methods, and events shared by ASP. NET Server elements (pages, controls, and user controls.

The templatecontrol class implements the inamingcontainer interface. Therefore, the page class is also the "named container" of all its child controls ". The control naming container is the first parent control that implements the inamingcontainer interface. Inamingcontainer is a symbolic interface without any methods. ASP. NET creates additional namespaces for the pages or controls that implement this interface to name the pages or controls. The page class is the naming container for all page child controls.

Page class attributes

The attributes of a page object are divided into three types: internal objects, worker attributes, and page-specific attributes.

Internal object of the page class

  

Worker attributes

  

In the context of ASP. NET applications, the page object is the root node of the tree structure. Therefore, attributes such as namingcontainer and parent always return null. In addition, the page attribute returns its own instance (that is, this in C ).

Context attributes

  

The three ID-related attributes (ID, clientid, and uniqueid) of the page object always return an empty string because it is only used for server controls.

Page Class Method

Method used for rendering

  

On the ASP. NET page, any control whose runat attribute is server cannot be placed outside the <form> label. The Web and HTML controls are correctly rendered using the verifyrenderinginserverform method. In theory, this method should also be called when the custom control is rendered. However, in many cases, custom controls are embedded or derived from web or HTML controls that can be checked by themselves.

The page class does not directly expose the getwebresourceurl method of the clientscriptmanager class. This method provides a feature that developers have long hoped. When developing controls, we often need to add embedded static resources (such as or script files ). These files can be downloaded separately, but this method is not elegant. Vs2003 and later versions allow us to embed resources into the control assembly. But how can we obtain and bind resources to the control by encoding? For example, bind an image stored in the Assembly to the tag. The getwebresourceurl method is used to return the URL of a specific resource. This URL points to a new resource service (webresource. axd), which can obtain and return requested resources from the Assembly.

Example:

IMG. imageurl = page. getwebresourceurl (typeof (thecontrol), gifname );

The first parameter type object is used to locate the Assembly containing resources, and the second parameter is the name of the embedded resource. The returned URL is as follows:

Webresource. axd? A = assembly & R = resourcename & t = Timestamp

The timestamp value is the timestamp of the Assembly. It enables the browser to download the updated resource again if the assembly is modified.

Controls

  

In ASP. in net, the runat attribute is very important, but in fact, its function is nothing more than marking the surrounding markup text and providing reference for parsing and instantiation. It does not contain the information required for instantiating the control.

Script-related methods

  

The method shown in the preceding table enables you to add JavaScript code to the client page. When using the method, we actually notify the current page object and insert the script code when the page is rendered. Therefore, after these methods are executed, the script-related information is only cached in an internal structure and will be used later when the page object generates HTML text.

Page events

  

When a page is requested, its class and its server control are responsible for processing the request, rendering the HTML output, and then sending it back to the client. Due to the inherent characteristics of the HTTP protocol, communication between the client and the server is stateless and non-connected. The actual application needs the status to maintain subsequent calls to the same page. ASP. NET provides a built-in architecture that can store and restore page States in a transparent manner. Although it is based on the stateless protocol, in this way, the client experiences a continuous execution process.

View status overview

This kind of continuity is related to the design of pages and work methods, and ASP. NET page view status. Server-side controls also play an important role. Before the page is rendered as HTML, the page stores the status information of itself and its contained controls in the persistent medium (generally an implicit field. After the page is sent back, its status is deserialized from the hidden field to initialize the Server Control Instance declared in the page layout.

Each page instance has its own view status because the information is embedded in HTML. In this way, the control initializes the view status value created at the previous time (that is, the status value that was last presented to the client. In addition, there is a stage in the page cycle to merge the stored status with the updates made by the client. After sending the message back, when the page is executed, it will find a stateful and updated context.

Two assumptions are made here. The first assumption is that the page is always shipped to itself and carries status information. The second assumption is that the server-side control must have the runat = server attribute, in order to have "Vitality" after page sending back ".

Single Form Model

The ASP. NET page supports only one form label on the server. All controls to interact with the server must be placed in the form. Both the form and control must have the runat attribute. Otherwise, the form and control are regarded as plain text and output word by word. On the server side, the form is an instance of the htmlform class. The htmlform class does not expose any action attributes that are equivalent to the HTML <form> tag. The reason is that ASP. NET pages are always delivered to themselves.

Pages that do not contain server forms and use HTML forms (<form> tags without runat attributes) are also valid ASP. NET pages. On the ASP. NET page, HTML and Server Forms can coexist. However, you can only set the runat attribute of one <form> label to server. HTML forms can be shipped to any page in the program just like normal forms. However, the problem is that the status information is not automatically stored. In other words, the ASP. NET web form model works only when the form uses a server <form> element.

Asynchronous page

The ASP. NET page is processed by an HTTP handler as an instance of the page class. Each request occupies a thread in the ASP. NET thread pool and is released only after the request is completed. What if the requested page frequently starts external and time-consuming tasks? The problem is that although the ASP. NET process is idle, there is no idle thread in the pool to process requests from other new pages. To alleviate this problem, ASP. NET supports asynchronous processing programs (through the ihttpasynchandler interface ).

Asynchronous ASP. NET page construction involves two steps: defining the async attribute of the @ page command to true and registering several asynchronous tasks. Asynchronous tasks can be registered in two ways: Define the asynchronous Processing Program begin/end for the prerendercomplete event or create a pageasynctask object representing the asynchronous task. It can be defined before the prerender event is triggered, and is generally placed in the page_load event.

Async attributes

When this attribute is set to true, the page parser implements the ihttpasynchandler interface in the dynamic generation class of the aspx resource, and enables the page to register an asynchronous handler for the prerendercomplete event.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.