MS Ajax client programming learning notes (3)

Source: Internet
Author: User
Prevents multiple submissions

Ajax is asynchronous in nature, and the features of background operations makeProgramDuring execution, users cannot be informed about the waiting status. Therefore, users often submit multiple times to avoid server losses and reduced user experience due to repeated submission, it is necessary to prevent users from submitting the same operation multiple times.

In Ajax, you can use pagerequestmanager to determine whether to submit events in various stages of asynchronous delivery. The event trigger sequence in normal pagerequestmanager is as follows:

1. the user clicks the button in updatepanel to trigger an asynchronous operation.

2. The pagerequestmanager object triggers the initializerequest event.

3. The pagerequestmanager object triggers the beginrequest event.

4. send requests to the server

5. The client successfully receives the response from the service.

6. The pagerequestmanager object triggers the pageloading event.

The 7pagerequestmanager object triggers the pageloaded event.

8. The application object triggers the load event.

9pagerequestmanager object endrequest triggering time.

 

Example:

Aspx:

<% @ Page Language = "C #" autoeventwireup = "true" codefile = "multirequest. aspx. cs" inherits = "multirequest" %>

<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> No title page </title>
<SCRIPT type = "text/JavaScript">
VaR PRM;
VaR btnsubmitid = '<% = button1.clientid %>'
Function pageload (){
PRM = SYS. webforms. pagerequestmanager. getinstance ();
PRM. add_initializerequest (oninitializerequest );
PRM. add_pageloaded (onpageloaded );
}
Function oninitializerequest (sender, argS)
{
If (PRM. get_isinasyncpostback () & args. get_postbackelement (). Id = btnsubmitid)
{
Args. set_cancel (true );
Showwaitinginfo (true );
SetTimeout ("showwaitinginfo (false)", 1500 );
}
}
Function showwaitinginfo (visible)
{
VaR id = $ get ("messagepanel ");
Id. style. Display = visible? "Block": "NONE ";
}
Function onpageloaded (sender, argS)
{
Showwaitinginfo (false );
}
</SCRIPT>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<Div>
<Asp: scriptmanager id = "scriptmanager1" runat = "server"/>
<Asp: updatepanel id = "updatepanel1" runat = "server">
<Contenttemplate>
<Asp: Label id = "label1" runat = "server" text = "name"> </ASP: Label> <asp: textbox id = "textbox1" runat = "server"> </ASP: textbox>
<Br/>
MSG: <asp: textbox id = "textbox2" runat = "server"> </ASP: textbox>
<Asp: button id = "button1" runat = "server" onclick = "button#click" text = "OK"
Width = "55px"/>
<Br/>
<Br/>
<Asp: Label id = "label2" runat = "server" text = "Result:"> </ASP: Label>
<Asp: Label id = "lblresult" runat = "server"> </ASP: Label>
<Div id = "messagepanel" style = "display: none;">
Still processing, please be patient.
</Div>
</Contenttemplate>
</ASP: updatepanel>
<Br/>
</Div>
</Form>
</Body>
</Html>

CS:

Protected void page_load (Object sender, eventargs E)
{

}
Protected void button#click (Object sender, eventargs E)
{
This. lblresult. Text = This. textbox1.text + "&" + this. textbox2.text + "time:" + system. datetime. Now. tolocaltime ();
System. Threading. thread. Sleep (3000 );
}

 

Note:

Function pageload (){
// Obtain the pagerequestmanager object
PRM = SYS. webforms. pagerequestmanager. getinstance ();
// Set event triggering
PRM. add_initializerequest (oninitializerequest );
PRM. add_pageloaded (onpageloaded );
}
Function oninitializerequest (sender, argS)
{
// Determine whether asynchronous sending is in progress and the same button is triggered for two operation sending.
If (PRM. get_isinasyncpostback () & args. get_postbackelement (). Id = btnsubmitid)
{
// Cancel the asynchronous operation.
Args. set_cancel (true );
// Prompt the user
Showwaitinginfo (true );
// Hide the prompt information after 1.5s
SetTimeout ("showwaitinginfo (false)", 1500 );
}
}

 

 

Stop ongoing asynchronous operations

You can use the abortpostback () method of the pagerequestmanager object to stop ongoing asynchronous operations.

Related code:

Function btncancel_onclick ()
{

If (PRM. get_isinasyncpostback ())
{

PRM. abortpostback ();
}

}

Prompt to the user when performing asynchronous delivery

In the onbeginrequest of pagerequestmanager, determine whether the get_postbackelement (). Id of pagerequestmanager is the currently submitted button. If yes, the waiting information is displayed and the page is forbidden.

When onpageloaded is enabled, the page is restored.

 

Exception Handling During asynchronous execution

Process the onendrequest event of pagerequestmanager, for example:

Function onendrequest (sender, argS)
{

VaR error = args. get_error ();

If (error)
{

// Display exception.

$ Get ("messagepanel"). innerhtml = Error. message ';

// Notify Asp.net Ajax client framework that the error has been handled.

Args. set_errorhandled (true );
}
}

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.