Introduction to ASP. NET internal mechanism (III)

Source: Internet
Author: User

Introduction to ASP. NET internal mechanism (III)

When we include data in the form and send it to the server, what form does the data send? After the server receives the submitted data, how does the server read the data and decide to trigger the event? How can we enable custom controls to trigger events on the server side? Why do some server controls need to implement ipostbackeventhandler and ipostbackdatahandler ?.....

 

SeriesArticleLink:

Introduction to the internal mechanism of ASP. NET (I)

Introduction to the internal mechanism of ASP. NET (2)

Introduction to ASP. NET internal mechanism (III)

Introduction to ASP. NET internal mechanism (IV)

Introduction to ASP. NET internal mechanism (V)

Introduction to ASP. NET internal mechanism (6)

Introduction to ASP. NET internal mechanism (7)

Introduction to ASP. NET internal mechanism (8)

As you know, when developing an ASP. NET Website, each server control has its own ID. For better expansion in the future, let's take a simple look at this scenario: When we click a server control in the browser, such as a button, the page is sent back to the server, the server then triggers the click and other events of the control. This scenario is simple and cannot be simplified. Let's look at it further. We know that not all controls will trigger events on the server side, such as the submit button. This button can also upload form data to the server, however, this control cannot trigger events on the server side. In fact, after a page is submitted to the server, the server will check which control caused the Page Submission, then find the control ID, and then on the page we requested, such as default. aspx (if we click the button, the request is default. aspx) to check whether the server Control ID is the same as the ID of the submitted page at this time. If yes, an event is triggered when the page lifecycle is appropriate, return the processing result; if not, the server will not perform any special processing.

 

Note that the page (such as default. aspx) has been compiled into a class inherited from the page when you look for the Control ID.

Of course, the above is just a rough process. I hope you will have a general understanding. The following is a detailed explanation.

 

I will explain through a process:

1. First, we request a Server Page, such as http: // localhost/demo/default. aspx. For ease of interpretation, assume that the page has only three server controls: Textbox, dropdowmlist, And button.

Definition:

 

<% @ Page Language = "C #" %>
<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd" >
< Html Xmlns = "Http://www.w3.org/5o/xhtml" >
< Head Runat = "Server" >
< Title > Untitled page </ Title >
</ Head >
< Body >
< Form ID = "Form1" Runat = "Server" >
< ASP: textbox Runat = "Server" ID = "Textbox1" />

< ASP: dropdownlist Runat = "Server" ID = "Dropdownlist1" >
< ASP: listitem Text = "Text1" Value = "Value1" />
< ASP: listitem Text = "Text2" Value = "Value2" />
< ASP: listitem Text = "Text3" Value = "Value3" />
</ ASP: dropdownlist >

<ASP: buttonRunat= "Server"Text= "Submit"/>
</Form>
</Body>
</Html>

 

In the browser, we can see the following in "Source:

 

 

Code
<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en"
"Http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd" >
< Html Xmlns = "Http://www.w3.org/5o/xhtml" >
< Head >
< Title > Untitled page </ Title >
</ Head >
< Body >
< Form Name = "Form1" Method = "Post" Action = "Default7.aspx" ID = "Form2" >
< Div >
< Input Type = "Hidden" Name = "_ Viewstate" ID = "_ Viewstate"
Value = "/Wepdwukmtaxnzk2mjy2owrkxj + 0heo0c5n0xvutp03x6odaspw =" />
</ Div >
< Input Name = "Textbox1" Type = "Text" ID = "Textbox1" />
< Select Name = "Dropdownlist1" ID = "Dropdownlist1" >
< Option Value = "Value1" > Text1 </ Option >
< Option Value = "Value2" > Text2 </ Option >
< Option Value = "Value3" > Text3 </ Option >
</ Select >
< Input Type = "Submit" Name = "Ctl02" Value = "Submit" />
</ Form >
</ Body >
</ Html >

 

 

Indeed, the above htmlCodeThere is really nothing, but you should take a look at textbox and dropdownlist. Their IDs are the same on the ASPX page on the server side and in the local source.

2. Input "Xiaoyang" in the textbox of the browser, select "text2" in dropdowlist, and click the button to submit. In this case, the submitted data is included in the form and saved in the format "textbox1 = Xiaoyang & dropdownlist1 = value2". Needless to say, form data is separated by "&", and each separator string contains two parts: ID and value, that is, "key-Value Pair ".

3. ASP. net instantiate an httprequest class. This class has two attributes: forms and querystring. Their types are namevaluecollection (key-value pair, you can regard it as a hashtable), and then ASP.. Net parses form data and form data is parsed into key-value pairs, and then stored in forms (post submission) or querystring (get submission ). We used post submission as an example.

4. Previous events occurred before the lifecycle of the page. When the page calls its processrequest method, it enters the page lifecycle. At this time, the page will check all the controls on the page to see which of the following interfaces have implemented ipostbackdatahandler, then, add the controls that implement these interfaces to an arraylist, and then check which controls implement ipostbackeventhandler. If they are in another set, then, the collection of ipostbackdatahandler controls is developed and traversed, And the ipostbackdatahandler method is called: loadpostdata (string postdatakey, namevaluecollection value ).

As mentioned before, the values of the submitted forms are stored in forms (namevaluecollection type). Therefore, these values are passed in to this method, then check whether these values are the same as those before. We still remember that when we see the page in the browser, the textbox in the page has no value at the beginning, in addition, the dropdownlist selects text2, and now our value is changed to "Xiaoyang" and "text2 ". so the result of this method check is: the value has changed and true is returned. if true is returned, the next method of ipostbackdatahandler will call raisepostdatachangeevent (); this method has registered the event and will be triggered after the page lifecycle.

5. Therefore, if the custom control we developed wants to trigger an event when data changes, we must implement the ipostbackdatahandler interface. For example, you can refer to my other articles on control development.

6. Now we have talked about data changes. Next, let's take a look at how events are triggered. When the raisepostbackevent method is executed during page development, the page will traverse the set of controls that implement ipostbackeventhandler and check whether there is a control in the page: This control must implement ipostbackeventhandler, and the ID is the same as the ID of the control that submitted the page to the server. If the raisepostbackevent method of ipostbackeventhandler is used to trigger an event (different controls implement this method differently), such as a click event.

Therefore, if you want to trigger an event in a custom control, you must implement the ipostbackeventhandler interface. You can refer to my control development series, for example.

 

Today is here! Thank you.

Related Article

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.