Depth understanding __doPostBack

In my essay " Page, How do you handle postback events? "I have raised a question, how to get control causes the page PostBack? By reading the source code of the Page class, cock, accidentally saw __EVENTTARGET and __EVENTARGUMENT two constants defined, and through debug analysis page, you can get to know the trigger page through Request.Form [ "__ EVENTTARGET"] PostBack event source (ID controls). For general controls, so it, PostBack Only Button and ImageButton triggered unable to obtain their ID in this way, at first thought it was different interfaces they implement and produce different ways of PostBack. Just . AspAlliance saw an article about __doPostBack the (original: " Understanding __doPostBack at The JavaScript Function "), really understand the inner mechanism of the page PostBack, the mystery finally solved. Let's look at the simple principle of page PostBack, and Button, ImageButton PostBack particularity.

   __doPostBack is a pure and very simple javascript function, most of it from the page PostBack are triggered. Note that this is the "most" because there are only two Web Server Control will trigger its own page PostBack, so other controls are triggered by __doPostBack page PostBack function, define the function of that first look at it:

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();

}

}

   You can see by the above code, __doPostBack with two parameters, eventTarget is going to lead to the identification page PostBack the control ID, eventArgument parameter provides an additional argument which is triggered when the page PostBack event. Of course, when this function is a function both of the value of the parameter assignment of two hidden variables to the page __EVENTTARGET and __EVENTARGUMENT, then submit the form page calling page submit method. This is why we can [ "__ EVENTTARGET"] Gets the reason the lead to get the control ID of the page PostBack through Request.Form.

   __DoPostBack understand the function, we can easily use it very easy to trigger their own custom PostBack event. That said above, most of the controls are call this method to lead the PostBack page, only two controls are the exception, Button and ImageButton, precisely because they are not made by calling the event __doPostBack back and forth, so by the implicit form with variable __EVENTTARGET and __EVENTARGUMENT it is unable to obtain the PostBack get triggered Button or ImageButton ID and parameter values ​​can only be obtained through the following examples of their way, and then determine which control caused the PostBack:

CODE2:

foreach (string str in Request.Form)

    {

    Control c = Page.FindControl(str);

if (c is Button)

{

control = c;

break;

}

}

   Why can enumerate Key value Request.Form collection look to it postback event source? Here Button and ImageButton there are some differences. Button control raises the PostBack, Button will ID itself as a Key Request.Form, its Value is the value of the Text property of the Button, back to the server so that the server can Request.Form Key value by enumerating the go Find out the control instance, to determine which control caused PostBack event is a Button control, and then get yes. ImageButton different is that, it is not only with the Request.Form ImageButton ID as a Key, which is combined with the ID of ImageButton .x and .y, as Key, the two key-value pair is added in Request.Form, this value should be the two key-value pairs to identify ImageButton image size. Also, understand this rule, we can still get PostBack whether it is caused by the ImageButton a certain way.

Summary: __doPostBack understand and grasp a better understanding of the principles of our Page event model has a very big help, and is also an important basis for us to further exploit good PostBack event page. Throughout the asp.net page PostBack model only Button and ImageButton is an exception, other controls are the same, that is, the use of __doPostBack function. In the event when we need to get to the source control, then by __EVENTTARGET, this point is of particular attention.

LookIntoDoPostBack.zip

Abu http://hjf1223.cnblogs.com

Reproduced in: https: //www.cnblogs.com/zhangchenliang/archive/2013/05/23/3094285.html

Guess you like

Origin blog.csdn.net/weixin_33713350/article/details/93495381