In my essay, Page, how did you handle the postback incident? Has raised the question of how to get controls that cause page postback? By reading the page of the source code, stumbled, inadvertently saw the __eventtarget and __eventargument the two constants of the definition, and through the Debugging Analysis page, know through the request.form["__eventtarget"] The event source (the ID of the control) that triggers the page postback can be obtained. For a generic control, this is OK, only button and ImageButton triggered postback cannot get their IDs in this way, and at first thought it was the difference between the interfaces they implemented that produced the postback way. Just in Aspalliance. See an article about __doPostBack (original: "Understanding the JavaScript __dopostback Function"), To really understand the page postback internal mechanism, the mystery has finally been solved. Here is a simple look at the principle of page postback, and Button,imagebutton postback particularity.
__doPostBack is a pure and very simple JavaScript function, and most of the page postback are triggered by it. Note that this is "most" because only two web Server control triggers the postback of the page, and the other controls are postback that trigger the page through the __doPostBack function, let's look at the definition of this function first:
Copy Code code as follows:
<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 ();
}
}
As you can see from the above code, __doPostBack has two parameters, Eventtarget is the control id,eventargument parameter that identifies the page postback that will be thrown, providing additional parameters that are taken when the page postback event is raised. Of course, when this function is a function, the values of these two parameters will be assigned to the page's two implied variables __eventtarget and __eventargument, and then the page's Submit method is called for submitting the page form. That's why we can get the control ID that raised the page postback by request.form["__eventtarget".
Having learned the __doPostBack function, we can easily use it to trigger our own custom postback events very easily. That said, most of the controls are called this method to draw the page's postback, only two controls are exceptions, Button and ImageButton, precisely because they are not through the call __doPostBack events, so through the form implied variable __ Eventtarget and __eventargument are not able to get the ID and parameter values of the button or ImageButton that are causing the postback, only the following way to get their instance, And then determine which control is causing the postback: C #
Copy Code code as follows:
foreach (String str in Request.Form)
{
Control C = Page.findcontrol (str);
if (c is Button)
{
control = C;
Break
}
}
Why is it possible to find the postback event source by enumerating the key values of the Request.Form collection? There are some different button and ImageButton here. The postback that is thrown by the button control, will be the ID of the button itself as a key request.form, its value is the button's Text property value, back to the server, so that the server can enumerate Request.Form key value, to find out the control instance, to determine whether the button control, and then get Is the postback event that is raised by which control. And the difference between ImageButton is that It is not just using the ImageButton ID as the Request.Form key, it is using the ImageButton ID Plus. x and. Y, as the key, adding two key-value pairs in Request.Form, which should be the size of the image that identifies the ImageButton. In the same way, after understanding this rule, we can still get whether the postback is caused by ImageButton.
Conclusion: Understanding and Mastering the __doPostBack principle is very helpful for us to know more about page's event model, and it is also an important basis for our postback events that make good use of the page. In the postback model for the entire ASP.net page, only the button and ImageButton are the exception, and the other controls are the same, using the __doPostBack function. This is especially noticeable when we need to get to the event source control via __eventtarget.
Note: __doPostBack (', ') is preceded by two "_", not one.