Analysis on event triggering of server controls in ASP. NET

Source: Internet
Author: User

First, there are two scenarios in which ASP. NET triggers server events on the client.:

A, Webcontrols In Button And Htmlcontrols In Type Is Submit Of Htmlinputbutton
The two buttons are displayed on the client:
<Input name = "submit1" id = "submit1" type = "Submit" value = "Submit">
This is the form submission button. After you click it, it will be sent to the server as a parameter. The parameter is like this: Name attribute of the control = value of the control. The example above is:
Submit1 = submit.
The server will know that the button has been clicked based on the key of the received control's name attribute, so that the click event of this button is triggered on the server.
B, Htmlcontrols In Type Is Button Of Htmlinputbutton And all other control events, such Linkbutton Click, Textbox Of Change Events and so on:
After these events are generated on the client, they are sent to the server through a unified mechanism.
1. First Asp.net The page framework uses two Hidden Field to store the events triggered by the control, and the event parameters:
<! -Indicates the control that triggers the event. Generally, it is the name of the control -->
<Input type = "hidden" name = "_ eventtarget" value = ""/>
<! -Indicates the parameter used to trigger an event. Generally, when a control has more than two events, it is used to identify which event -->
<Input type = "hidden" name = "_ eventargument" value = ""/>
2. The server generates JScript To handle the sending of all these events. Code Yes:

Code
< Script Language = " Javascript " >
<! --
Function _ Dopostback (eventtarget, eventargument ){
VaR Theform = Document. webform2;
Theform. _ eventtarget. Value = Eventtarget;
Theform. _ eventargument. Value = Eventargument;
Theform. Submit ();
}
// -->
< / SCRIPT>

3. Each control that will cause a server event will call the above Code in the client event in the response:
For example, in htmlcontrols, The Click Event of the htmlinputbutton whose type is button
<! -Call the client's Click Event _ dopostback. The eventtarget parameter is 'button2', indicating that the event is triggered by the 'button2' control with the name. The eventargument is empty, the htmlinputbutton with the type of button has only one server event triggered by the client -->
<Input language = "JavaScript" onclick = "_ dopostback ('button2 ','') "Name =" button2 "id =" button2 "type =" button "value =" button "/>
Another example is the change event of the Textbox Control.
<! -The onchange event of the client calls _ dopostback. The eventtarget parameter is 'textbox1', indicating the event triggered by the 'textbox1' control, the Textbox Control has only one server event textchanged triggered by the client, so the server will trigger the textchanged event of the textbox->
<Input name = "textbox1" type = "text" id = "textbox1" onchange = "_ dopostback ('textbox1','') "Language =" JavaScript "/>
4. called after the event is triggered by the client _ Dopostback Method. Eventtarget And event parameters Eventargument Two hidden Domains _ Eventtarget And_ Eventargument And then submit the form. _ Eventtarget And _ Eventargument To determine which control event is triggered.

2. Principles of PostBack:

_ Dopostback is a pure and very simple JavaScript function. Most page PostBack is triggered by it. Note: Here is the "majority", because only two Web Server Control will trigger the PostBack of the page, and other controls will trigger the PostBack of the page through the _ dopostback function, let's take a look at the definition of this function:
Code1:

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

From the code above, we can see that __dopostback has two parameters. eventtarget is the ID of the control that will mark the page PostBack. The eventargument parameter provides additional parameters when the page PostBack event is triggered. Of course, when this function is called, the values of these two parameters will be assigned to the two implicit variables _ eventtarget and _ eventargument on the page, and then the page form will be submitted by calling the submit method on the page. This is why we can use request. Form ["_ eventtarget"] to obtain the Control ID that triggers the page PostBack.
After learning about the _ dopostback function, we can easily use it to easily trigger custom PostBack events. As mentioned above, most of the controls are called.

Third, button PostBack practices

The PostBack of the page is cited, with only two controls, button and imagebutton, because they do not send events back and forth by calling _ dopostback, therefore, using the implicit variables _ eventtarget and _ eventargument in the form, you cannot obtain the ID and parameter value of the button or imagebutton that triggers the PostBack operation. This can be achieved through the following methods:
1) if linkbutton is added to the page, the page will load the JS required by PostBack in the page

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

2) use getpostbackeventreference to generate _ dopostback () for the client ()
For example, the foreground page
<Asp: button id = "button1" runat = "server" text = "button"> </ASP: button>
(1) <a href = "#" onclick = "document. getelementbyid ('button1'). Click ()"> trigger a server button event </a>
(2) Use getpostbackeventreference to generate _ dopostback () for the client ()
Front-end
<A href = "#" onclick = "<% = PostBack () %>"> trigger server button events </a>
Background Code

code highlighting produced by actipro codehighlighter (freeware)
http://www.CodeHighlighter.com/

--> protected string PostBack ()
{< br> return This . page. getpostbackeventreference ( This . button1, " Haha " );
}

Through _ eventargument = "Haha", you can judge whether the PostBack of the link is clicked and write the button event of button1 as follows: Code
If (Request [ " _ Eventargument " ] = " Haha " )
{
Response. Write ( " This is the link PostBack. " );
}
Else  
{
Response. Write ( " This is not the link PostBack " );
}

Statement:ThisArticleSome or all of the content comes from the Internet. If you infringe your rights, please email meMaple2884@126.com), I will delete this article in the first time!

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.