After a page is sent to the client browser, the user's operations on the page only trigger client events. For example, a user clicks<Input type = 'button '/>Button, triggeredClickThe event is actually a client button.ClickEvent, how does this event "Infect" the server and trigger the Server Control event?
Let's take a look at the client generated by the server control.CodeFor exampleLinkbuttonThe rendering code is as follows:
< A ID = "Lbtnserver" Href = "Javascript :__ dopostback ('lbtnserver ','')" > OK </ A >
In the code, we see"_ Dopostback ()"What is this JavaScript function?
On the page to be returned, Asp.net registers two hidden form fields and a Javascript script to support the return of all controls:
< Input Type = "Hidden" Name = "_ Eventtarget" ID = "_ Eventtarget" Value = "" />
< Input Type = "Hidden" Name = "_ Eventargument" ID = "_ Eventargument" Value = "" />
< Script Type = "Text/JavaScript" >
// <! [CDATA [
VaR Theform = Document. Forms [ ' Form1 ' ];
If ( ! Theform ){
Theform = Document. form1;
}
Function _ Dopostback (eventtarget, eventargument ){
If ( ! Theform. onsubmit | (Theform. onsubmit () ! = False )){
Theform. _ eventtarget. Value = Eventtarget;
Theform. _ eventargument. Value = Eventargument;
Theform. Submit ();
}
}
// ]>
</ Script >
In combination with the previous code snippet, we can see that the control that needs to trigger server events actually calls the _ dopostback function and assigns the event source and event parameters to the two hidden fields in the form:
_ Eventargument and _ eventtarget, and then submit the form to the server.
In the lifecycle of a page, there is a stage dedicated to handling page return events: raisepostbackevent. At this stage, the page will find the event source control based on the value of _ eventtarget,
Then, according to the raisepostbackevent () method of the control_ Eventargument is used as a parameter to pass to the control.
In this case, the page requires that the event source control have the raisepostbackevent () method, that is, the control must implement the ipostbackeventhandler interface.
Reference: not far from the road: in-depth analysis of ASP. net2.0 control development