This article is based on ASP. net3.5 core programming. The following content applies to ASP. net2.0 and later versions.
The page class provides the basic behavior for creating all objects by the. aspx file. (This includes <asp:Control/> The process of generating C # description-type objects by TAG is invisible, which is automatically completed by the compiler .) Page can implement this function because it implements the ihttphandler interface. The ihttphandler interface can be used to process HTTP requests of the corresponding type (the. aspx file here ).Program. The ihttphandler interface's key method processrequest completes the process of creating all objects from the. aspx file.
The parent class templatecontrol of page defines a set of basic functions of the page (or control. The control class at the upper layer of templatecontrol defines the properties, methods, and events shared by ASP. NET Server elements (including pages, controls, and user controls. The inamingcontainer interface implemented by templatecontrol enables page to act as the control container.
PageClass attributes
Page attributes are classified into three types: built-in objects, worker attributes, and page-specific attributes. The following sections describe:
Internal object
These attributes are the secondary objects inside the page. They are the basic elements that enable normal page execution.
Genus |
Description |
Application |
Returns an instance of the httpapplicationstate class. It indicates the status of the current application. |
Cache |
An instance of the cache class is returned, which implements caching for ASP. NET applications. It supports task priority and expiration (expiration), and is more efficient and more powerful than application. |
Profile |
Returns an instance of the profilecommon class. It represents the user-specific data set associated with the request |
Request |
Returns an instance of the httpresponst class. It represents the current HTTP request. |
Response |
Returns an instance of the httpresponse class. It is used to send HTTP response data to the client. |
Server |
Returns an instance of the httpserverutility class. It provides auxiliary methods for processing Web requests. |
Session |
Returns an instance of the httpsessionstate class. It is used to manage user-specific data. |
Trace |
Returns an instance of the tracecontext class. It is used to track pages. |
User |
Returns an object of the iprincipal type. It indicates the user who sends the request |
Worker attributes
These attributes are used to carry information and provide basic functions. These attributes are essential for page programming.
Genus |
Description |
Clientscript |
Obtains the clientscriptmanager object, which contains the client script used by the page. |
Controls |
Returns the word control set on the current page. |
Errorpage |
Indicates the attribute of the error page. In the event of an unprocessed page exception, the browser that sends the request will be redirected to the page. |
Form |
Returns the htmlform object of the current page. |
Header |
Returns the object representing the current page header. This object implements the ipageheader interface. |
Isasync |
Indicates whether the current page is called by an asynchronous processing program. |
Iscallback |
Indicates whether the currently loaded page is the result of client script callback. |
Iscrosspagepostback |
Indicates whether the currently loaded page is a response to another page. |
Ispostback |
Indicates whether the currently loaded page is a response to the client sending back or loading for the first time. |
Isvalid |
Used to indicate whether the page verification is successful |
Master |
Returns an instance of the masterpage class. It indicates the master page that determines the appearance of the current page. |
Masterpagefile |
Indicates the object name attribute of the master page on the current page. |
Namingcontainer |
Returns null. |
Page |
Returns the current page object (this is in C) |
Pageadapter |
Returns the adapter object of the current page object. |
Parent |
Returns null. |
Previouspage |
Return the reference of the main call page in cross-page sending. |
Templatesourcedirectory |
Obtain the virtual path of the current page |
Validators |
Returns a set of all verification controls on the page. |
Viewstateuserkey |
A string type attribute that represents a user-specific Identifier. It is used to hash the content of the view State ). This is one of the defense lines for one-click attack attacks. |
Explanation: Because the page object is in the root node of the layered structure. Therefore, the namingcontainer and parent attributes always return null. Viewstateuserkey can accept user-specific information, such as the verified session ID. This attribute can be used to encrypt viewstate and so on. In addition, viewstateuserkey must be set in page_init.
Context attributes
These attributes represent the visual and non-visual attributes of the page)
Genus |
Description |
Clientid |
Always Returns an empty string |
Clientquerystring |
Obtain the query string of the requested URL. |
Clienttarget |
The default value is an empty string, which allows developers to specify the browser type to generate correct HTML. Setting this property will disable the browser's Automatic Detection Function |
Enableviewstate |
Indicates whether view status information is managed on the current page. The function is equivalent to the enableviewstate attribute in @ page. |
Enableviewstatemac |
Indicates whether ASP. NET needs to generate a computer-specific authentication (MAC) and append it to the view status of the page. |
Enabletheming |
Indicates whether a topic is supported on the current page. |
ID |
Always Returns an empty string |
Maintainscrollpositiononpostback |
Used to indicate whether to restore the client browser's scroll bar position after sending back |
Smartnavigation |
Indicates whether to enable intelligent navigation. Smart navigation leverages a large number of unique browser functions to enhance users' experience on pages. |
Stylesheettheme |
The name of the style sheet applied to the current page. |
Theme |
The name of the topic applied to the page. Note: If the topic is set in encoding mode, it can only be performed in the preinit event. |
Title |
Indicates the attributes of the page title. |
Traceenabled |
Turn on or off the tracking feature for the current page |
Tracemodevalue |
Get or set the tracing Mode |
Uniqueid |
Always Returns an empty string |
Viewstateencryptionmode |
Used to indicate whether the view status and encryption method are encrypted |
Visible |
Indicates whether ASP. NET displays the page. ASP. NET does not generate any HTMLCodeAnd only the text written using response. Write will be sent to the client. |
Three ID-related properties of the Page Object-ID, clientid, and uniqueid total return NULL strings, because these are only used for server controls and do not make sense in the page.
PageClass Method
Methods of the page class can be divided into the following categories by function. They are summarized as follows:
Method used to present (generate tag)
Method |
Description |
Databind |
This interface is used to bind all data-binding controls on the page to the corresponding data source. The databind method itself does not generate code, but paves the way for subsequent presentation. |
Rendercontrol |
Outputs the current page as HTML text. If tracking is enabled, the result will contain the tracking information. |
Verifyrenderinginserverform |
Controls call this method when being generated to ensure that the body of the server form contains them. This method does not return values, but an exception is thrown when an error occurs. |
Note: All controls whose runat attribute is server in ASP. NET must be placed in the <form> label so that they can be correctly presented by verifyrenderinginserverform.
Controls
Method |
Description |
Designerinitialize |
When the page is in Visual Studio designer mode, this method is used to initialize the page class instance. |
Findcontrol |
Use the Control ID to search for it in the named container on the page. The search will not enter the sub-control that is also named container. |
Gettypehashcode |
Obtains the hash code generated by the ASP. xxx_aspx page class at runtime. In the base class page, the implementation of this method only returns 0, and the actual page class returns a large number |
Getvalidators |
Returns the set of control validators for the specified validation group. |
Hascontrols |
Check whether the current page contains child Controls |
Loadcontrol |
Compile and load the user control in the. ascx file, and a control object is returned. If the user control supports caching, partialcachingcontrol is returned. |
Loadtemplate |
Compile and load the user control in the. ascx file and return an instance of the internal class implementing the itemplate interface. It encapsulates the control. This internal class is named simpletemplate |
Mappath |
Obtain the complete physical address mapped to an absolute or relative virtual path. |
Parsecontrol |
Parses the input valid string and returns the control instance corresponding to the specified text tag. If the string contains multiple controls, only the first one is valid. The runat attribute is ignored. This method returns a control-type instance and must be converted to a more specific type. |
Registerrequirescontrolstate |
Register the control as a persistent control. |
Registerrequirespostback |
Registers the specified control so that it can receive the sending and receiving notifications (even if its ID does not match any ID in the returned dataset ). This control must implement the ipostbackdatahandler Interface |
Registerrequiresraiseevent |
Registers a specified control so that it can process input sending events. This control must implement the ipostbackeventhandler Interface |
Registerviewstatehandler |
This method is used to set an internal flag to maintain the current page view status. In most cases, it is used internally. If this method is not called in the pre-rendering stage, no view status is written. Generally, this method is called only by the HTML form Server Control on the page. You do not need to call |
Resolveurl |
Parses a relative URL into an absolute URL Based on the templatesourcedirectory property value. |
Validate |
Enable all verification controls on the page to verify the information they are granted. ASP. NET 2.0 supports verification groups to logically classify the controls to be verified and do not interfere with each other. |
Among these methods, it is worth noting that the loadcontrol and loadtemplate Methods return control objects in addition to the return value type, the latter returns itemplate type objects, and the internal code is the same. The other difference between the two methods is that the latter does not trigger compilation, but only parses strings and infers control information. This information is used to create and initialize a control instance.
Script-related methods
The following page class methods are used to insert HTML and script code to the client page.
Method |
Description |
Getcallbackeventreference |
Obtains the reference of a client function. When this function is called, a callback event from the client to the server is triggered. |
Getpostbackclientevent |
Call getcallbackeventreference |
Getpostbackclienthyperlink |
Append "javascript:" to the start of the string returned by getpostbackeventreference, for example, JavaScript :__ dopostback ('ctlid ','') |
Getpostbackeventreference |
Returns the prototype of the client script function. When the script function is called, it will cause sending back execution. It accepts a control object and a parameter. The returned string format is __dopostback ('ctlid ','') |
Isclientscriptblockregistered |
Determines whether the specified client script is registered on the page. This method has been marked as obsolete |
Isstartupscriptregistered |
Determines whether the specified client STARTUP script is registered on the page. This method has been marked as obsolete |
Registerarraydeclaration |
This method is used to add the ecmascript array to the client page. It accepts an array name and a string representing the array body. For example, if you call this method using thearray and 'A', 'B' as parameters, you will get the following JavaScript code: var thearray = new array ('A ', 'B '); This method has been marked as obsolete |
Registerclientscriptblock |
ASP. NET pages use this method to open the HTTP <form> element of the client page and add a script code block. This method has been marked as obsolete |
Registerhiddenfield |
Used to automatically register implicit fields with the page. This method has been marked as obsolete |
Registeronsubmitstatement |
This method is used to add client script code for processing onsubmit events. The script should be a JavaScript function that calls the script code registered somewhere. This method has been marked as obsolete |
Registerstartupscript |
ASP. NET pages use this method to add a script code block before the HTTP <form> element of the client page is closed. This method has been marked as obsolete |
Setfocus |
Set the browser focus on the specified control. |
Many of these methods are marked as obsolete. We should use the related methods in the clientscript of page to operate the client script. When we use the registerxxx method to register a script, the script is cached in a structure and produced to the final HTML document after page rendering.
PageClass events
Some column events are triggered during the page lifecycle. These events are generally labeled as a key stage for handling reporters.
Event |
Description |
Aborttransaction |
If the ASP. NET page is involved in an automatic transaction, this event is triggered when the transaction is aborted. |
Committransaction |
If the ASP. NET page is involved in an automatic transaction, this event is triggered when the transaction is committed. |
Databinding |
Triggered when the page's databind method is called. This method binds all child controls on the page to their corresponding data sources. |
Disposed |
Triggered when the page is released from memory. This event marks the final stage of the page Lifecycle |
Error |
Thrown when an unhandled exception is thrown |
Init |
Triggered when the page is initialized. This event marks the first stage of the page lifecycle. |
Initcomplete |
Triggered after the page and all its child controls are fully initialized. ASP. NET 1.x does not support this event |
Load |
After page Initialization is complete |
Loadcomplete |
This operation is triggered when the page loading is complete and the server event is triggered. ASP. NET 1.x does not support this event |
Preinit |
Triggered at the beginning of page initialization. ASP. NET 1.x does not support this event |
Preload |
Triggered at the beginning of page loading. ASP. NET 1.x does not support this event |
Prerender |
Triggered when the page is about to be rendered |
Prerendercomplete |
Triggered at the beginning of the page pre-rendering phase. ASP. NET 1.x does not support this event |
Savestatecomplete |
This is triggered when the view status of the page is stored in persistent media. ASP. NET 1.x does not support this event |
Unload |
It is triggered after the page is detached from the memory but not released |