Dopostback in-depth research

Source: Internet
Author: User
In my essay 《 Page. How do you handle the send-back event?I raised a question about how to get the control that caused the page PostBack? By reading the source code of the page class, we accidentally saw the definitions of the two constants _ eventtarget and _ eventargument. Through the debugging Analysis page, we learned the request. form ["_ eventtarget"] can obtain the event source (Control ID) of the trigger page PostBack ). For general controls, only the PostBack triggered by button and imagebutton cannot obtain their IDs in this way, at first, they thought they implemented different interfaces and produced different PostBack methods. Just now Aspalliance.See an article about _ dopostback (original article :《 Understanding the Javascript _ dopostback Function), To truly understand the internal mechanism of the page PostBack, and finally solve the doubts. Next, let's take a brief look at the principles of page PostBack and the particularity of Button and imagebutton PostBack.

_ Dopostback is a pure and very simple JavaScript function. Most page PostBack is triggered by it. Note: Here is the "majority", because only two Web Server Control will trigger the PostBack of the page, and other controls will trigger the PostBack of the page through the _ dopostback function, let's take a look at the definition of this function:

Code1:

<Input type = "hidden" name = "_ eventtarget" id = "_ eventtarget" value = ""/>

<Input type = "hidden" name = "_ eventargument" id = "_ eventargument" value = ""/>

Function _ dopostback (eventtarget, eventargument ){

If (! Theform. onsubmit | (theform. onsubmit ()! = False )){

Theform. _ eventtarget. value = eventtarget;

Theform. _ eventargument. value = eventargument;

Theform. Submit ();

}

}

From the code above, we can see that __dopostback has two parameters. eventtarget is the ID of the control that will mark the page PostBack. The eventargument parameter provides additional parameters when the page PostBack event is triggered. Of course, when this function is called, the values of these two parameters will be assigned to the two implicit variables _ eventtarget and _ eventargument on the page, and then the page form will be submitted by calling the submit method on the page. This is why we can use request. Form ["_ eventtarget"] to obtain the Control ID that triggers the page PostBack.

After learning about the _ dopostback function, we can easily use it to easily trigger custom PostBack events. As mentioned above, most of the controls call this method to introduce the page's PostBack. Only two controls are available, such as button and imagebutton, it is precisely because they do not send events back and forth by calling _ dopostback, therefore, the hidden variables _ eventtarget and _ eventargument in the form cannot obtain the IDs and parameter values of the buttons or imagebutton that trigger the PostBack operation. Only the following methods can be used to obtain their instances, then determine which control triggers the PostBack:

Code2:

Foreach (string STR in request. Form)

{

Control C = page. findcontrol (STR );

If (C is button)

{

Control = C;

Break;

}

}

Why can I find the event source by enumerating the key value of the request. Form set? Here, the button and imagebutton are different. The PostBack triggered by the button control uses the ID of the button as the request. A key of form. Its value is the text attribute value of the button and is returned to the server. This way, the server can enumerate the request. form key value to find the control instance and determine whether it is a button control, and then obtain the PostBack event triggered by the control. The difference between imagebutton is that it uses not only the ID of imagebutton as the request. form key, which is added with the ID of imagebutton. X and. y, as the key, in the request. form adds two key-value pairs. The value of these two key-value pairs should be the image size that identifies imagebutton. After understanding this rule, we can still determine whether it is a PostBack caused by imagebutton in a certain way.

Conclusion: Understanding and understanding the _ dopostback principle is very helpful for us to better understand the page event model. It is also an important basis for us to further utilize the page PostBack event. In the PostBack model of the entire Asp.net page, only the button and imagebutton are exceptions, and other controls are the same, that is, the _ dopostback function is used. This is especially important when we need to get the event source control through _ eventtarget.

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.