How to trigger server control events in Asp.net is described in the following two cases.
In Asp.net, server events triggered by clients are divided into two situations:
1. The buttons in webcontrols and the type in htmlcontrols are the htmlinputbutton of submit.
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.
2. htmlinputbutton with the type of button in htmlcontrols and all other control events, such as linkbutton click and textbox change events:
After these events are generated on the client, they are sent to the server through a unified mechanism.
1. First, the Asp.net page framework uses two den domains to store the events triggered by the Control and event parameters:
<! -Indicates the control that triggers the event. Generally, the control name -->
<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 a JScript method to handle the sending of all these events. This code is:
| <ScriptLanguage = "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. after the client triggers an event, it calls the _ dopostback method. The eventtarget and eventargument parameters of the triggered control source are respectively paid to the two hidden domains _ eventtarget and _ eventargument, and then submits the form, on the server side, determine which control event is triggered Based on _ eventtarget and _ eventargument.