In my article "Page, how did you handle the sending 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 in AspAlliance. I saw an article about _ doPostBack (original article: Understanding the JavaScript _ doPostBack Function) to really understand the internal mechanism of 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.
Lookdomaindopostback.zip