Control return event Principle

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.