[Original] ASP. NET resend details
I believe that many friends who have developed Ajax have encountered the issue of sending and callback. Today, we will talk about sending and returning independently with Ajax, and explain it through a simple set of examples. First, add a client Link Control on the page and set its ID to "alink". It looks like this:
1:<aid="aLink"onclick="__doPostBack('aLink','[aLink_EventArg]')>aLink</a>
Save and view it in the browser. After you click the link, the system will prompt a script error. The cause is very simple. Check the page source code and you will know that the _ dopostback callback function does not exist currently, this is the same as calling a JavaScript function that does not exist.
Put this problem aside first. Next we add a server button control on the page, set its ID to "btn1", and set its onclientclick attribute to a sending back function, it should look like this:
1:<asp:ButtonID="btn1"runat="server"Text="Btn1"OnClientClick="__doPostBack('btn1','[btn1_EventArg]')"/>
Save it and check it in the browser. After you click it, the same script error is displayed. The cause is already explained. NET Framework does not automatically generate a sending back function for the server control.
We also put this problem aside, and then add a server button control on the page. This time, no additional attributes need to be set on the front end, so it should look like this:
1:<asp:ButtonID="btn2"runat="server"Text="Btn2"/>
This is not complete yet. We need to switch to the Code view and rewrite the page rendering method. It should look like this:
1:protectedoverridevoidRender(HtmlTextWriter writer){
2: this.btn2.OnClientClick+=ClientScript.GetPostBackEventReference(this.btn2,"[btn2_EventArg]",true);
3: base.Render(writer);
4:}
The getpostbackeventreference method returns a string used for client events to trigger sending back to the server. The first parameter received is the control used to process sending back, the second is the string format parameter used to process additional event information, and the third is whether to register the event reference for verification. When we trace the string returned by this method, we will find that it is the same as the return string we wrote for btn1. Save the changes and view them in the browser. We are pleasantly surprised to find that btn2 works normally (although the differences between btn1 and btn2 cannot be seen from the page source code). what's even more surprising is that, if you Debug on IE (the author's IE version is IE8), you will find that the two controls that cannot work before run well. The reason is that getpostbackeventreference not only returns a string used to trigger the sending back, but also registers a sending back event on the server, in the source code of the page, the system automatically generates the _ dopostback function. However, if you have other browsers (chrome or Firefox) installed, use them to open the page and you will find that clicking btn1 will throw an exception for. NET Framework. The details are as follows:
. NET framework needs to register the sending and callback events of the server-side controls for security considerations. Because btn1 has not performed similar registration (btn2 registered using getpostbackeventreference), an exception is thrown, this is exactly why alink does not cause exceptions (because it is a client control ). You can manually disable the verification for sending back and callback by page or web. the enableeventvalidation attribute in config is set to false (if there is a conflict between the two settings, the page prevails). It should look like this:
1:<%@PageLanguage="C#"AutoEventWireup="true"CodeBehind="ClientPostback.aspx.cs"Inherits="WebApplication1.ClientPostback" EnableEventValidation="false" %>
Or:
1:<system.web>
2: <pagesenableEventValidation="false"></pages>
3:</system.web>
However, this method of ignoring Security is not recommended. Unless the event of sending or callback is unpredictable, it should be registered for every sending or callback event. To register for btn1, you need to go back to the Code view and add registration in the previous page rendering method (registration of this line can only be written on the page. in render, you must also note that the unique identifier and parameter of the registered control must be consistent with that of the front-end.) The modified render should look like this:
1:protectedoverridevoidRender(HtmlTextWriter writer){
2: Page.ClientScript.RegisterForEventValidation(this.btn1.UniqueID,"[btn1_EventArg]");
3: this.btn2.OnClientClick+=ClientScript.GetPostBackEventReference(this.btn2,"[btn2_EventArg]",true);
4: base.Render(writer);
5:}
Now, even if we trigger the send-back event in a non-IE browser, it can run normally. At present, the codes contained in btn1 and btn2 are equivalent, but there is still a simpler method for sending back.
Add a server button control on the page, set its ID to "btn3", and set its usesubmitbehavior attribute to false. It looks like this:
1:<asp:ButtonID="btn3"runat="server"Text="Btn3"UseSubmitBehavior="False"onclick="btn3_Click"/>
The default attribute of usesubmitbehavior is true, which means that clicking the button will trigger the submitted operation of the browser by default. setting this attribute to false means that clicking the button will trigger. net Framework, please note that the triggering source and triggering behavior are different. After the usesubmitbehavior attribute is set, the system will automatically register it. The unique identifier of the registered control is the uniqueid of the control, and the parameter is an empty string.
Next, we will add a server button in the page and set its ID to "btn4" (well, I admit that the IDS sorted by numbers will become confusing in a short time, but as a small example, I want to focus more on the smoothness of the narration, instead of racking my brains to give them an elegant name ), and set its onclientclick to "_ dopostback ('btn3','') ". It looks like this:
1:<asp:ButtonID="btn4"runat="server"Text="Btn4"OnClientClick="__doPostBack('btn3','')"/>
Save and view it in the browser. Click btn4 and you will find that although we haven't registered it, it runs very well. This indicates that the event registration is irrelevant to the ID of the server control, as long as the uniqueid In the send-back event is registered (btn3 is automatically registered by the system, as mentioned earlier), the call can be successful.
If you capture the sender in btn3_click and are careful enough, you will find that the maximum difference between sending and submitting is: If you enter through submission, then sender will be the button that triggers the submission; if you enter through sending back, sender will be the button corresponding to the uniqueid in the sending back function. In other words, in the click method, the backend cannot know who actually triggered it, because you crossed the browser and directly sent the request to the server.
Reprinted. Please keep the author's name and original website address. Click here to open the original article. Ji Wei's blog http://www.cnblogs.com/jiaowei/archive/2010/08/16/1800989.html
I believe that many friends who have developed Ajax have encountered the issue of sending and callback. Today, we will talk about sending and returning independently with Ajax, and explain it through a simple set of examples. First, add a client Link Control on the page and set its ID to "alink". It looks like this:
1:<aid="aLink"onclick="__doPostBack('aLink','[aLink_EventArg]')>aLink</a>
Save and view it in the browser. After you click the link, the system will prompt a script error. The cause is very simple. Check the page source code and you will know that the _ dopostback callback function does not exist currently, this is the same as calling a JavaScript function that does not exist.
Put this problem aside first. Next we add a server button control on the page, set its ID to "btn1", and set its onclientclick attribute to a sending back function, it should look like this:
1:<asp:ButtonID="btn1"runat="server"Text="Btn1"OnClientClick="__doPostBack('btn1','[btn1_EventArg]')"/>
Save it and check it in the browser. After you click it, the same script error is displayed. The cause is already explained. NET Framework does not automatically generate a sending back function for the server control.
We also put this problem aside, and then add a server button control on the page. This time, no additional attributes need to be set on the front end, so it should look like this:
1:<asp:ButtonID="btn2"runat="server"Text="Btn2"/>
This is not complete yet. We need to switch to the Code view and rewrite the page rendering method. It should look like this:
1:protectedoverridevoidRender(HtmlTextWriter writer){
2: this.btn2.OnClientClick+=ClientScript.GetPostBackEventReference(this.btn2,"[btn2_EventArg]",true);
3: base.Render(writer);
4:}
The getpostbackeventreference method returns a string used for client events to trigger sending back to the server. The first parameter received is the control used to process sending back, the second is the string format parameter used to process additional event information, and the third is whether to register the event reference for verification. When we trace the string returned by this method, we will find that it is the same as the return string we wrote for btn1. Save the changes and view them in the browser. We are pleasantly surprised to find that btn2 works normally (although the differences between btn1 and btn2 cannot be seen from the page source code). what's even more surprising is that, if you Debug on IE (the author's IE version is IE8), you will find that the two controls that cannot work before run well. The reason is that getpostbackeventreference not only returns a string used to trigger the sending back, but also registers a sending back event on the server, in the source code of the page, the system automatically generates the _ dopostback function. However, if you have other browsers (chrome or Firefox) installed, use them to open the page and you will find that clicking btn1 will throw an exception for. NET Framework. The details are as follows:
. NET framework needs to register the sending and callback events of the server-side controls for security considerations. Because btn1 has not performed similar registration (btn2 registered using getpostbackeventreference), an exception is thrown, this is exactly why alink does not cause exceptions (because it is a client control ). You can manually disable the verification for sending back and callback by page or web. the enableeventvalidation attribute in config is set to false (if there is a conflict between the two settings, the page prevails). It should look like this:
1:<%@PageLanguage="C#"AutoEventWireup="true"CodeBehind="ClientPostback.aspx.cs"Inherits="WebApplication1.ClientPostback" EnableEventValidation="false" %>
Or:
1:<system.web>
2: <pagesenableEventValidation="false"></pages>
3:</system.web>
However, this method of ignoring Security is not recommended. Unless the event of sending or callback is unpredictable, it should be registered for every sending or callback event. To register for btn1, you need to go back to the Code view and add registration in the previous page rendering method (registration of this line can only be written on the page. in render, you must also note that the unique identifier and parameter of the registered control must be consistent with that of the front-end.) The modified render should look like this:
1:protectedoverridevoidRender(HtmlTextWriter writer){
2: Page.ClientScript.RegisterForEventValidation(this.btn1.UniqueID,"[btn1_EventArg]");
3: this.btn2.OnClientClick+=ClientScript.GetPostBackEventReference(this.btn2,"[btn2_EventArg]",true);
4: base.Render(writer);
5:}
Now, even if we trigger the send-back event in a non-IE browser, it can run normally. At present, the codes contained in btn1 and btn2 are equivalent, but there is still a simpler method for sending back.
Add a server button control on the page, set its ID to "btn3", and set its usesubmitbehavior attribute to false. It looks like this:
1:<asp:ButtonID="btn3"runat="server"Text="Btn3"UseSubmitBehavior="False"onclick="btn3_Click"/>
The default attribute of usesubmitbehavior is true, which means that clicking the button will trigger the submitted operation of the browser by default. setting this attribute to false means that clicking the button will trigger. net Framework, please note that the triggering source and triggering behavior are different. After the usesubmitbehavior attribute is set, the system will automatically register it. The unique identifier of the registered control is the uniqueid of the control, and the parameter is an empty string.
Next, we will add a server button in the page and set its ID to "btn4" (well, I admit that the IDS sorted by numbers will become confusing in a short time, but as a small example, I want to focus more on the smoothness of the narration, instead of racking my brains to give them an elegant name ), and set its onclientclick to "_ dopostback ('btn3','') ". It looks like this:
1:<asp:ButtonID="btn4"runat="server"Text="Btn4"OnClientClick="__doPostBack('btn3','')"/>
Save and view it in the browser. Click btn4 and you will find that although we haven't registered it, it runs very well. This indicates that the event registration is irrelevant to the ID of the server control, as long as the uniqueid In the send-back event is registered (btn3 is automatically registered by the system, as mentioned earlier), the call can be successful.
If you capture the sender in btn3_click and are careful enough, you will find that the maximum difference between sending and submitting is: If you enter through submission, then sender will be the button that triggers the submission; if you enter through sending back, sender will be the button corresponding to the uniqueid in the sending back function. In other words, in the click method, the backend cannot know who actually triggered it, because you crossed the browser and directly sent the request to the server.
Reprinted. Please keep the author's name and original website address. Click here to open the original article. Ji Wei's blog http://www.cnblogs.com/jiaowei/archive/2010/08/16/1800989.html