A brief introduction to its usage:
I. Use of ajaxpro
1. Add references to the project and browse to the ajaxpro.2.dll file.
2. Write the following in system. Web in Web. config:Code
</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. All write methods must be used.
[Ajaxpro. ajaxmethod]
5. Make it clear when calling
Namespace name. Class Name. method, for example, webui. _ 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 ()
{< br> return "Hello my friends ";
}< br> // method with parameters written in the background
[ajaxpro. ajaxmethod]
Public String getstring (string Str)
{< br> return STR + "say: Hello my friends";
}
. synchronous call
(1 ). drag the HTML control button
(2 ). double-click. in the aspx script
(3 ). write the content you want to enter in it.
example:
// ---------------- synchronous call without parameters -----------
function button1_onclick ()
{< br> var res = webui. _ default. getstr ();
alert (res. value);
}< br>
// ---------------- synchronous call with parameter ------------
function button2_onclick () // textbox1 is the Server Control
{< br> 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
Example:
// ----------------- Asynchronous call without parameter -----------------
Function button3_onclick (){
Webui. _ default. getstr (getstrcallback );
}
Function getstrcallback (RES)
{
Alert (res. value );
}
// ----------------- Asynchronous call with parameter -----------------
Function button4_onclick (){
VaR STR = Document. getelementbyid ("<% = textbox1.clientid %>"). value;
Webui. _ default. getstring (STR, getstringcallback );
}
Function getstringcallback (RES)
{
Alert (res. value );
}
7. Call object
// object
[ajaxpro. ajaxmethod]
public class getclass ()
{< br> class linoleic = new class ();
linoleic. c_id = 100;
linoleic. c_name = "Class 34";
linoleic. count = 20;
return linoleic;
}< br> // ------------------ synchronous call object -----------
function button5_onclick () {
var res = webui. _ default. getclass (). value;
alert ("class No.:" + Res. c_id + "name:" + Res. c_name + "count:" + Res. count);
}< br> // ------------------ asynchronous call object -----------
function button6_onclick () {
webui. _ default. getclass (getclasscallback);
}< br> function getclasscallback (CLAS)
{< br> var res = Clas. value;
alert ("class No.:" + Res. c_id + "name:" + Res. c_name + "count:" + Res. count);
}
8. Use of datasets
// Method
[Ajaxpro. ajaxmethod]
Public dataset getinfo ()
{
Return webui. getdataset. getlist ();
}
// -------------------- Asynchronously call the dataset --------------
Function button8_onclick (){
Webui. _ default. getinfo (getdatasetcallback );
}
Function getdatasetcallback (RES)
{
VaR dataset = res. value;
VaR strhtml = "";
Strhtml + = '<Table Style = "border-collapse: collapse; border-color: Gray;" border = "1px"> ';
Strhtml + = '<tr> ';
Strhtml + = '<TD> Student ID </TD> ';
Strhtml + = '<TD> name </TD> ';
Strhtml + = '<TD> age </TD> ';
Strhtml + = '</tr> ';
For (VAR I = 0; I <dataset. Tables [0]. Rows. length; I ++)
{
Strhtml + = '<tr> ';
Strhtml + = '<TD>' + dataset. Tables [0]. Rows [I]. stu_id + '</TD> ';
Strhtml + = '<TD>' + dataset. Tables [0]. Rows [I]. stu_name + '</TD> ';
Strhtml + = '<TD>' + dataset. Tables [0]. Rows [I]. stu_age + '</TD> ';
Strhtml + = '</tr> ';
}
Strhtml + = '</table> ';
Thedata. innerhtml = strhtml; // thedata is thedata in <Div id = "thedata"> </div>
}
9. Use of the Verification Code
// ---------------------- Use of the Verification Code (synchronous call is required )----------------------
// Use the verification code
[Ajaxpro. ajaxmethod]
Public bool validcodedata (string code)
{
Return (httpcontext. Current. session ["checkcode"]. tostring () = Code );
}
Function button9_onclick (){
VaR code = Document. getelementbyid ("<% = textbox2.clientid %>"). value;
VaR bool = webui. _ default. validcodedata (CODE). value;
If (bool = true)
{
Alert ("OK ");
} Else
{
Alert ("no ");
}
}
Ajaxpro. DLL files are stored on the Internet many times. If you cannot find them, send me a message and I will send you an email.
2. Direct call:
Javascript: <% = background method %>
Function says ()
{
Alert ("<% = say () %> ");
}
Function del ()
{
Alert ("<% = deletebyid (8) %>"); // ebyid (8) Background method name
}
3. 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 ();
}
}