ASP. NET asynchronous Trigger usage (AJAX)

Source: Internet
Author: User

In today's project, because of the asynchronous trigger, when the text box loses focus, it goes to the database to check it once, and then reminds of three ways.

A brief introduction to its usage:

I. Use of AJAXPRO

1. Add http://www.php.cn/wiki/231.html "target=" _blank "> References in the project, browse to find Ajaxpro.2.dll file

2. Write the following code in the system.web in Web. config
</configuration>
<system.web>
<add verb= "*" path= "*.ashx" type= "ajaxpro.ajaxhandlerfactory,ajaxpro.2"/>
</system.web>
<//configuration>

3. In the Load event, add the
AjaxPro.Utility.RegisterTypeForAjax (typeof (class name));

4. Write the method to use
[Ajaxpro.ajaxmethod] begins, and then writes the method

5. Must be written clearly when calling
The name of the namespace. Class name. Method, Example: Webui._default.getdata ();

6. Call can be divided into two Chinese laws (synchronous invocation, asynchronous invocation)

Non-parametric method written in the background
[Ajaxpro.ajaxmethod]
public string Getstr ()
{
Return "Hello my Friends";
}
There's a way to write in the background.
[Ajaxpro.ajaxmethod]
public string getString (String str)
{
return str + "Say:hello my Friends";
}

A. Synchronous invocation
(1). Drag into the HTML control button
(2). Double-click to automatically display in the. aspx script
(3). Write what you want to enter in the inside
Cases:
------------------synchronous call without a parameter-----------
function Button1_onclick ()
{
var res=webui._default.getstr ();
alert (Res.value);
}

------------------synchronous call has a parameter------------
function Button2_onclick ()//textbox1 is a server control
{
var Str=document.getelementbyid ("<%=TextBox1.ClientID%>"). Value;
var res=webui._default.getstr (str);
alert (Res.value);
}

B. Asynchronous invocation
(1). Drag into the HTML control button
(2). Double-click to automatically display in the. aspx script
(3). Write what you want to enter in the inside
Cases:
-----------------an asynchronous call without a parameter-----------------
function Button3_onclick () {
Webui._default.getstr (Getstrcallback);
}
function Getstrcallback (res)
{
alert (Res.value);
}
-----------------an asynchronous call to a parameter-----------------
function Button4_onclick () {
var Str=document.getelementbyid ("<%=textbox1.clientid%>"). Value;
Webui._default.getstring (Str,getstringcallback);
}
function Getstringcallback (res)
{
alert (Res.value);
}

7. Calling objects

Object
[Ajaxpro.ajaxmethod]
Public Class GetClass ()
{
Class CLA = new Class ();
Cla. c_id = 100;
Cla. C_name = "Class 34";
Cla. Count = 20;
return CLA;
}
------------------a synchronous call to an object-----------
function Button5_onclick () {
var res=webui._default.getclass (). value;
Alert ("Class number:" +res. c_id+ "Name:" +res. c_name+ "Number of people:" +res. Count);
}
------------------an asynchronous call to an object-----------
function Button6_onclick () {
Webui._default.getclass (Getclasscallback);
}
function Getclasscallback (clas)
{
var Res=clas.value;
Alert ("Class number:" +res. c_id+ "Name:" +res. c_name+ "Number of people:" +res. Count);
}

8. Use of data sets
Method
[Ajaxpro.ajaxmethod]
Public DataSet GetInfo ()
{
return WebUI.GetDataSet.getList ();
}

--------------------Call the dataset asynchronously--------------
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 number </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 a <p id= "Thedata" ></p> thedata
}



9. Use of verification codes

----------------------the use of verification codes (synchronous calls must be used)----------------------

Use of verification codes
[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 file Online A lot of, under their own, if not find it, send me a message, I sent you mailbox



Second, call directly:
javascript: <%= Background method%>
function says ()
{
Alert ("<%=say ()%>");
}
Function del ()
{
Alert ("<%=deletebyid (8)%>");//deletebyid (8) Background method name
}

Third, use ICallbackEventHandler callback

/**//*
* To declare the ICallbackEventHandler interface, to invoke the service-side code on the client without postback, you must declare the interface and implement its two methods:
* RaiseCallbackEvent (), GetCallbackResult ()
* RaiseCallbackEvent () parameter is transmitted from the front desk, according to the parameters to execute different code and the results with GetCallbackResult () back to the foreground
*/

The System.Web.UI.ICallbackEventHandler interface must be declared
Public partial class _default:system.web.ui.page, System.Web.UI.ICallbackEventHandler
{
Defines the return value of a callback
private string Result;
Define two variables to receive the page passed over to the operand
private string Num1;
private string Num2;
protected void Page_Load (object sender, EventArgs e)
{

}

/**////<summary>
The method is a callback execution method that handles the contents of the callback in this method based on the parameter, and the method does not return a value
</summary>
<param name= "eventargument" > This parameter is sent from the client </param>
public void RaiseCallbackEvent (String eventargument)
{
Eventargumeng is the parameter that JavaScript passes from the client, this example passes over three parameters to take each parameter out into the array with "/" split
string[] Pagparams = eventargument.split ('/');
NUM1 = pagparams[1];
Num2 = pagparams[2];
Depending on the first parameter (the selected operator), different execution functions are called
Switch (Pagparams[0])
{
Case "0":
Result = Add (); Break
Case "1":
Result = Sub (); Break
Case "2":
Result = multi (); Break
Case "3":
Result = Pision (); Break
}
}

/**////<summary>
The method is to return the result of the callback to the client
</summary>
<returns></returns>
public string GetCallbackResult ()
{
return Result;
}

Here are four functions that are called by the RaiseCallbackEvent method, calling the callback to perform the operation of the function
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 Pision ()
{
Double Addresult = Double. Parse (NUM1)/double. Parse (NUM2);
Return Addresult. ToString ();
}
}

Related Article

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.