Ajaxpro usage, learn about background code for JS synchronous and asynchronous calls

Source: Internet
Author: User
I. ajaxpro usage 1. add a reference to the project and navigate to the ajaxpro.2.dll file. Note: Download ajaxpro.2.dll online. 2. in the web. system. write the following code into the web: <configuration>
<System. Web>
<Httphandlers>
<Add verb = "*" Path = "*. ashx" type = "ajaxpro. ajaxhandlerfactory, ajaxpro.2"/>
</Httphandlers>
</System. Web>
</Configuration> 3. Add
Ajaxpro. utility. registertypeforajax (typeof (Class Name); 4. the background method should use [ajaxpro. ajaxmethod. the name of the namespace must be clearly written during the call. class Name. method, for example:
Ajaxtest. _ default. getdata ();

6. The call can be divided into two methods (synchronous call and asynchronous call)

// No parameter method written in the background
[Ajaxpro. ajaxmethod]
Public String getstr ()
{
Return "Hello my friends ";
}
// The method with parameters written in the background
[Ajaxpro. ajaxmethod]
Public String getstring (string Str)
{
Return STR + "say: Hello my friends ";
}

 

A. synchronous call
(1) drag the HTML control button
(2) double-click to automatically display it in the. aspx script.
(3). Write the content you want to enter in it
Example: // ------------------ synchronous call without parameter -----------
Function button1_onclick ()
{
VaR res = webui. _ default. getstr ();
Alert (res. value );
}

// ------------------ Synchronous call with parameter ------------
Function button2 _ // textbox1 is the Server Control
{
VaR STR = Document. getelementbyid ("<% = textbox1.clientid %>"). value;
VaR res = webui. _ default. getstr (STR );
Alert (res. value );
}

B. asynchronous call
(1) drag the HTML control button
(2) double-click to automatically display it in the. aspx script.
(3). Write the content you want to enter in it
For example: // ----------------- The asynchronous call has no parameter -----------------
Function button3 _{
Webui. _ default. getstr (getstrcallback );
}
Function getstrcallback (RES)
{
Alert (res. value );
}
// ----------------- Asynchronous call with parameter -----------------
Function button4 _{
VaR STR = Document. getelementbyid ("<% = textbox1.clientid %>"). value;
Webui. _ default. getstring (STR, getstringcallback );
}
Function getstringcallback (RES)
{
Alert (res. value );
}

Note: During asynchronous calling, the background method cannot be reloaded; otherwise, an error occurs.

2. Direct call
Javascript: <% = background method %>

Function says ()
{
Alert ("<% = say () %> ");
}
Function del ()
{
Alert ("<% = deletebyid (8) %>"); // ebyid (8) Background method name
}


Iii. Adopt icallbackeventhandler callback

/**//*
* To declare the icallbackeventhandler interface, you must declare the interface and implement it by calling the server code on the client without sending it back:
* Raisecallbackevent () and getcallbackresult ()
* The raisecallbackevent () parameter is transmitted from the foreground. Different codes are executed based on the transmitted parameters and the result is returned to the foreground using getcallbackresult ().
*/

// The system. Web. UI. icallbackeventhandler interface must be declared.
Public partial class _ default: system. Web. UI. Page, system. Web. UI. icallbackeventhandler
{
// Define the return value of a callback
Private string result;
// Define two variables to receive page-passed operands
Private string num1;
Private string num2;
Protected void page_load (Object sender, eventargs E)
{

}
/// <Summary>
/// This method is the method for callback execution. The callback content is processed according to parameters in this method, and no return value is returned for this method.
/// </Summary>
/// <Param name = "eventargument"> this parameter is transmitted from the client. </param>

Public void raisecallbackevent (string eventargument)
{
// Eventargumeng is the parameter passed by JavaScript from the client. In this example, the three parameters are separated by "/" and each parameter is retrieved and saved to an array.
String [] pagparams = eventargument. Split ('/');
Num1 = pagparams [1];
Num2 = pagparams [2];
// Call different execution functions based on the first parameter (selected operator)
Switch (pagparams [0])
{
Case "0 ":
Result = add (); break;
Case "1 ":
Result = sub (); break;
Case "2 ":
Result = multi (); break;
Case "3 ":
Result = Division (); break;
}
}

/** // <Summary>
/// This method returns the callback result to the client.
/// </Summary>
/// <Returns> </returns>
Public String getcallbackresult ()
{
Return result;
}

// The following four functions use the raisecallbackevent method. The called callback is the function to perform the operation.
Private string add ()
{
Double addresult = double. parse (num1) + double. parse (num2 );
Return addresult. tostring ();
}

Private string sub ()
{
Double addresult = double. parse (num1)-double. parse (num2 );
Return addresult. tostring ();
}

Private string multi ()
{
Double addresult = double. parse (num1) * double. parse (num2 );
Return addresult. tostring ();
}

Private string Division ()
{
Double addresult = double. parse (num1)/double. parse (num2 );
Return addresult. tostring ();
}
}

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.