Back to Asp.net series-basics (1)

Source: Internet
Author: User

 

Web Page lifecycle and Postback reference http://www.bkjia.com/kf/201111/110866.html

 

Understanding the lifecycle of ASP. NET Web pages is necessary for a WEB Development Engineer, especially when writing his own control.

 

The lifecycle of ASP. NET Web pages can be divided into the following stages:

 

1. Page request

When a page is requested before the web page lifecycle, ASP. NET engine to determine whether the page needs to be compiled or directly sent to the requester through the buffer version of the page.

 

2. Start

In this phase, the Page's Request, Response, IsPostBack, and UICulture attributes are set as appropriate.

 

3. Page initialization

In this phase, the UniqueID attribute of each control is set, and the topic of the Page is also applied. If the Page is a return Page, that is, the IsPostBack attribute of the Page is true, the new values and ViewState of each control on the page have not been restored yet.

 

4. Load

In this phase, if the page is a return page, the new values and ViewState of each control in the page will be restored or set.

 

5. Validation

In this phase, the verification control on the page calls its own Validate Method for verification to set its own IsValid attribute, because the verification control must be verified on both the client and server.

6. Postback event handling

At this stage, all delegate events will be called, and all our event processing code will be called at this time.

 

7. Rendering

In this phase, Page calls the Render method of each control to write the html text stream corresponding to each control to the OutputStream attribute of Response. Before this phase, each control will save its own ViewState again, therefore, we have the last chance to change the ViewState attribute of the control in the PreRender method of the Page.

 

8. Unload

In this phase, the objects created for the Page and each control will be deleted and the resources will be released.

 

 

 

Postback

 

From HTML Render to Client, to clicking a button, the input is Postback to Server, and two events are triggered, and the Event Handler is executed to print the related Message.

 

ASP.. NET-designed Page Render to the Client-side HTML: __eventtarget represents the Unique name of the Control that triggers the Event; __eventargument represents the additional parameter defined for Event Handler; __ VIEWSTATE: viewstate. Defines a _ doPostBack function to complete the Postback operation. This function only has three lines of code. The first two lines assign values to the two previously defined hiddden fields through parameters, then, submit the form to the Server.

 

For a System. web. UI. webControls. button. By default, <input type = "submit"> is used for submission. You can set the UseSubmitBehavior attribute (ASP. NET 2.0 added, 1x does not have the corresponding settings), changes the button form submission behavior.

 

Let's take a look at how the Server handles this Postback. We will not detail the Web Page lifecycle here. The Server finds the corresponding Control through the value of the hidden field of _ EVENTTARGET, and determines whether the Control implements the System. Web. UI. IPostBackEventHandler Interface through Reflection. If the Control does implement this Interface, call the RaisePostBackEvent method of Page, which is a Virtual method and can be overwritten. Let's look at the definition of this method.

 

[EditorBrowsable (EditorBrowsableState. Advanced)]

Protected virtual void RaisePostBackEvent (IPostBackEventHandler sourceControl, string eventArgument)

{

SourceControl. RaisePostBackEvent (eventArgument );

}

We can see that this method directly calls the RaisePostBackEvent of the sourceControl and passes in an eventArgument parameter. In this example, sourceControl is the Web Control: Button2 corresponding to _ EVENTTARGET, eventArgument is the value of _ EVENTTARGET: An empty string. The Button2 type is System. Web. UI. WebControls. Button. Let's take a look at how the RaisePostBackEvent method in System. Web. UI. WebControls. Button is defined:

 

Protected virtual void RaisePostBackEvent (string eventArgument)

{

Base. ValidateEvent (this. UniqueID, eventArgument );

If (this. CausesValidation)

{

This. Page. Validate (this. ValidationGroup );

}

This. OnClick (EventArgs. Empty );

This. OnCommand (new CommandEventArgs (this. CommandName, this. CommandArgument ));

}

This method is also very simple. First, verify, and then start two events: OnClick and OnCommand, and then call the corresponding Event handler, which is consistent with our output.

 

This is basically the whole process of executing the Postback program. Now we make some interesting changes to our Page to verify it:

 

Form Submitting is the way for the Client to interact with the Server, but we now have two ways to submit the Form: Through the <input type = "submit"> control; by calling javascript :__ doPostBack. Based on this, we added the following javascript in Html:

 

<Script type = "text/javascript">

Function postback ()

{

_ DoPostBack ('button1 ','');

}

Document. getElementById ("Button2"). onclick = postback;

Document. getElementById ("Button3"). onclick = postback;

</Script>


 

The author stays up late

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.