ASP. NET in-depth introduction Series 3-page class

Source: Internet
Author: User

In. NET Framework, the page class is an ASP. NET application.ProgramAll objects built from the. aspx file provide basic behavior. This class is defined in the namespace system. Web. UI namespace. It is derived from templatecontrol and implements the ihttphandler interface:
Public class page: templatecontrol, ihttphandler
Templatecontrol is an abstract class that provides basic functions to ASP. NET pages and user controls. The control class is at the top of the hierarchy. It defines the attributes, methods, and events shared by all ASP. NET Server elements (pages, controls. The structure is as follows:

important attributes of the Page Object
clientquerystring: Obtain the query string of the request URL. This attribute can be used to process URL query strings in special formats.
visible: This is an attribute that overrides the control class. If the visible of the page is false, Asp. net does not generate any HTML Code for this page. only response is displayed. write output text.
isvalid: gets a value that indicates whether the page verification is successful. This attribute is often used when verification controls are used.
ispostback: gets a value indicating whether the page is being loaded in response to the client's sending back, or whether the page is being loaded and accessed for the first time. If the page is loaded in response to the client sending back, it is true; otherwise, it is false. This attribute is quite common. We often perform some initialization when accessing the page for the first time, and do not perform some initialization work when sending back, in this case, we need to use this attribute in the page_load event to determine whether it is sending back.
request: gets the system. Web. httprequest object of the request page.
response: gets the system. Web. httpresponse object associated with the system. Web. UI. Page Object. This object allows you to send HTTP response data to the client and contain information about the response.
session: gets the current session object provided by ASP. NET.
theme: gets or sets the topic name on the page.
title: gets or sets the page title. This attribute is useful if you need to dynamically change the browser page title.
controls: gets the system. Web. UI. controlcollection object, which indicates the child control of the server control specified in the UI hierarchy. This is an attribute that inherits from the control class. It is useful when you need to access controls in the page.

Important methods of page objects
Databind (): binds all the data binding controls on the page to the data source.
For example:
Int [] A = {1, 2, 4 };
Gridview1.datasource =;
Gridview2.datasource =;
This. databind ();
We can use the databind () method on the page to bind two gridview nodes to the data source.
Findcontrol (): Search for the server control with the specified identifier in the page naming container. Note that this method does not search itself as a sub-control of the named container. If you need to search all controls on the page, you can use the recursive method.
Setfocus (Control): sets the browser focus to the specified control. This is a very useful method. For example, if we want to focus on the textbox1 dialog box after loading the page, we can enter the following code in the page_load event:
This. setfocus (textbox1 );

Important events of the Page Object
See http://www.cnblogs.com/nuaalfm/archive/2008/09/11/1289325.html

We all know that all the aspx code separation classes inherit from this page class, but sometimes we may find that some of the page functions are repeated, we can abstract the repeated parts into a class so that this class inherits from the page class and then let those code separation classes inherit from this class. Here is a practical example:

Sometimes we may want our page to support entering a value in textbox. After clicking press enter, We can dynamically specify which button to call.
The base page class is:

Code
Namespace Sample
{
Public   Class Page: system. Web. UI. Page
{
Public Page ()
{

} // Internals
Public Void Tiebutton (control textboxtotie, control buttontotie)
{
// Init JScript
String Jsstring =   "" ;

// Check button type and get required JScript
If (Buttontotie Is Linkbutton)
{
Jsstring =   " If (event. Which & event. Which = 13) | (event. keycode & event. keycode = 13 )){ "
+   This . Clientscript. getpostbackeventreference (buttontotie, "" ). Replace ( " : " , " $ " ) +   " ; Return false;} else return true; " ;
}
Else   If (Buttontotie Is Imagebutton)
{
Jsstring =   " If (event. Which & event. Which = 13) | (event. keycode & event. keycode = 13 )){ "
+   This . Clientscript. getpostbackeventreference (buttontotie, "" ). Replace ( " : " , " $ " ) +   " ; Return false;} else return true; " ;
}
Else
{
Jsstring =   " If (event. Which & event. Which = 13) | (event. keycode & event. keycode = 13) {document. "
+   " Forms [0]. elements [' "   + Buttontotie. uniqueid. Replace ( " : " , " _ " ) +   " ']. Click (); Return false;} else return true; " ;
}

// Attach JScript to the onkeydown Attribute-we have to cater for htmlcontrol or webcontrol
If (Textboxtotie Is Htmlcontrol)
{
(Htmlcontrol) textboxtotie). Attributes. Add ( " Onkeydown " , Jsstring );
}
Else   If (Textboxtotie Is Webcontrol)
{
(Webcontrol) textboxtotie). Attributes. Add ( " Onkeydown " , Jsstring );
}
}
}
}

 

The page class is:

Code
Public   Partial   Class _ default: sample. page
{< P>

protected void page_load ( Object sender, eventargs e)
{< br> This . tiebutton (textbox1, button2);
}< BR >}

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.