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 as follows: <input type = "submit" value = "Submit" name = "Submit1">. This is the Form submission button, after you click it, it will be sent as a parameter to the server. The parameter is as follows:
The name attribute of the control is the value of the control. The example above is: Submit1 = Submit. The server will know that the button is 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 side.
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, it is the name of the control -->
<Input type = "hidden" 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" value = ""/>
2. The server generates a jscript method to handle the sending of all these events. This code is:
<Script language = "javascript" type = "text/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" 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 type = "text" 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.
Chapter II 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 all 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:
<Input type = "hidden" value = ""/>
<Input type = "hidden" 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
Chapter 3 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
<Input type = "hidden" value = ""/>
<Input type = "hidden" 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:
For example, the front-end 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
Protected string PostBack ()
{
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:
If (Request ["_ EVENTARGUMENT"] = "haha ")
{
Response. Write ("this is the linked PostBack ");
}
Else
{
Response. Write ("this is not the linked PostBack ");
}
Bug:
Problem: "using _ doPostBack will cause 』
The callback parameter is invalid. Use <pages enableEventValidation = "true"/> in the configuration, or use <% @ Page EnableEventValidation = "true" %> On the Page to enable event verification. For security purposes, this function verifies whether the parameters of the send-back or callback events come from the server controls that initially present these events. If the data is valid and is expected, use the ClientScriptManager. RegisterForEventValidation method to register the sending back or callback data for verification.
Problem Analysis and Solution: "Source Network 』
This should be analyzed in detail. Originally, asp. net2.0 was used to prevent the client from being "fraudulent" on the server. For example, the command "_ doPostback ('ctl01 $ abc', 'user _ 1')" needs to be sent back when an event originally output to the client is triggered, if you change the sending-back parameter from user_1 to user_5 by means, the server will re-check whether the output is user_5 and find that it is inconsistent with the output script on this page, this exception occurs.
However, programs written by many programmers do not consider this issue or ignore the possibility of fraud according to past habits (or more advanced and flexible design, such as some Ajax components, the program may modify parameters or the target control.