User applications in ASP. NETProgramWhen you click a button, linkbutton, or change the value of a Textbox (enable aotupostback) on the displayed page, the page is submitted and the corresponding PostBack event is triggered on the server, you can imagine that the browser on the client displays HTML, but the HTML does not trigger the PostBack event. This shows ASP. the. NET application certainly provides a method to know what actions the user has done on the client, causing the page to be submitted, and then the server triggers the PostBack event of the corresponding server control based on this information, so this articleArticleTo discuss how ASP. NET applications can trigger the PostBack event.
First, we can view ASP in the most direct way. NET page triggers the PostBack event of the corresponding control based on the information. We know that the page inherits from the page class. When the server receives a page request, the server will perform a series of processing as expected. This is the lifecycle of the page, which we will not discuss here, because there are many articles about the page lifecycle, Here we only focus on the logic in the raisepostbackevent method of the page type. We can view the raisepostbackevent method of the page type through reflector.
Private void raisepostbackevent (namevaluecollection postdata)
{
If (this. _ registeredcontrolthatrequireraiseevent! = NULL)
{
This. raisepostbackevent (this. _ registeredcontrolthatrequireraiseevent, null );
}
Else
{
String STR = postdata ["_ eventtarget"];
Bool flag =! String. isnullorempty (STR );
If (flag | (this. autopostbackcontrol! = NULL ))
{
Control control = NULL;
If (FLAG)
{
Control = This. findcontrol (STR );
}
If (control! = NULL) & (control. postbackeventhandler! = NULL ))
{
String eventargument = postdata ["_ eventargument"];
This. raisepostbackevent (control. postbackeventhandler, eventargument );
}
}
Else
{
This. Validate ();
}
}
}
This sectionCodeTo do this, first determine whether there is a control registration on the page that triggers the PostBack event, usually through the page. the registerrequirespostback (Control) method is used for registration. If no control registration triggers a PostBack event, the system checks whether the post data on the return page contains the two key-value pairs, __eventtarget and _ eventargument. If the _ eventtarget key contains values. findcontrol finds the reference of the control and checks whether the postbackeventhandler of the control is null. If it is not null, The postbackevent of the control is triggered. This is ASP. net.
For the first method to register a control on the server, a callback event is required, this is usually required in the fully customized custom controls we developed (completely customized, that is, do not combine the existing server-side controls ), the callback and ipostbackdatahandler interfaces are implemented in custom controls. The ipostbackdatahandler interface must implement two methods: bool loadpostdata (string postdatakey, namevaluecollection postcollection) and void raisepostdatachangedevent (). in the loadpostdata method, we need to determine whether the returned data contains the information you want (the page commit will submit the information in Form to the server in the form of key-value, here we only need to determine whether the key-value set contains the name attribute of the corresponding control.) if it contains the information we want, this indicates that the callback is triggered by our custom control. In this way, you can register this control as a callback event to be triggered.
For the second method, such as the linkbutton control, the HTML code generated when it is rendered is not <input type = "Submit"/>, so it does not have the Page Submission function, therefore, the client must use JavaScript to submit pages. However, because the HTML code presented by linkbutton is a <A> tag, the <A> tag key-value is not packaged during Page Submission, therefore, after the page is submitted, the server cannot find out who triggered the page return. Therefore, Asp. NET provides two hidden input tags.
<Input type = "hidden" name = "_ eventtarget" id = "_ eventtarget" value = ""/>
<Input type = "hidden" name = "_ eventargument" id = "_ eventargument" value = ""/>
These two items are often seen. When you click a linkbutton, the client will store the name and corresponding parameters of the linkbutton you have clicked in the two hidden inputs through JavaScript, then, submit the page. The page will package the two hidden inputs and pass them back to the server in the form of key-value, the server can use postdata ["_ eventtarget"] To find out what triggered the page callback, locate the corresponding server control, and trigger the callback event.
The above is the inside story of the web form triggering event. I am going to write several examples to illustrate them one by one to facilitate communication with you. However, writing a simple custom control requires a lot of coding, I am too lazy, so I have to use all the words to talk about it. If there is something unclear, please forgive me.