Home >Backend Development >C#.Net Tutorial >asp.net asynchronous trigger usage (AJAX)

asp.net asynchronous trigger usage (AJAX)

怪我咯
怪我咯Original
2017-03-31 13:20:351758browse

In today's project, because it needs to be triggered asynchronously, when the text box loses focus, it goes to the database to check once, and then I thought of three ways.

A brief introduction to its usage:

1. Use of AjaxPro

1. Add Quote, browse to find the AjaxPro.2.dll file

2. Write the following code in system.web in Web.config




 




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

4. All methods written must start with
[AjaxPro.AjaxMethod], and then write the method

5. You must write clearly when calling
Namespace Name.Class name.Method, for example: WebUI._Default.getData();

6. The call can be divided into two Method (synchronous call, asynchronous call)

//No parameter method written in the background
[AjaxPro.AjaxMethod]
public string getStr()
{
return "hello my friends";
}
//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 into the html controlbutton
(2). Double-click and it will automatically display in. In the aspx script
(3). Write the content you want to enter
Example:
//------------------Synchronous call No parameters -----------
function Button1_onclick()
{
var res=WebUI._Default.getStr();
alert(res.value);
}

//------------------Synchronous call has parameters------------
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 call
(1). Drag into the html control button
(2). Double-click and it will automatically display in In the .aspx script
(3).Write the content you want to enter
Example:
//-----------------Asynchronous call No parameters-----------------
function Button3_onclick() {
WebUI._Default.getStr(getStrCallBack);
}
function getStrCallBack(res )
{
alert(res.value);
}
//-----------------Asynchronous call has parameters----- ------------
function Button4_onclick() {
var str=document.getElementById("<%=TextBox1.ClientID %>").value;
WebUI ._Default.getString(str,getStringCallBack);
}
function getStringCallBack(res)
{
alert(res.value);
}

7.CallObject

//Object
[AjaxPro.AjaxMethod]
public Class getClass()
{
Class cla = new Class();
cla .C_Id = 100;
cla.C_Name = "Class 34";
cla.Count = 20;
return cla;
}
//--------- ---------Synchronous call 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);
}
//---------------- ------Asynchronous call 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 set
//Method
[AjaxPro.AjaxMethod]
public DataSet getInfo()
{
return WebUI.GetDataSet.getList();
}

//--------------------Asynchronously call the data set------ --------
function Button8_onclick() {
WebUI._Default.getInfo(getDataSetCallBack);
}
function getDataSetCallBack(res)
{
var dataset= res.value;
var strHtml="";
   strHtml +='

';
   strHtml +='        ';
   strHtml +='            ';
   strHtml +='            ';
   strHtml +='            ';
   strHtml +='        ';

   for(var i=0;i    {
       strHtml +='        
';
       strHtml +='            ';
       strHtml +='            ';
       strHtml +='            ';
       strHtml +='        ';
   }
   strHtml +='    
学生编号名称年龄
'+ dataset.Tables[0].Rows[i].stu_id +''+ dataset.Tables[0].Rows[i].stu_name +''+ dataset.Tables[0].Rows[i].stu_age +'
';
   thedata.innerHTML=strHtml;//thedata是一个

中的thedata
}



9.验证码的使用

//----------------------验证码的使用(必须采用同步调用)----------------------

//验证码的使用
[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文件网上很多的,自己下,如果找不到呢,给我发个留言,我发你邮箱



二,直接调用:
javascript中:<%=后台方法%>
function says()
   {
       alert("<%=Say()%>");
   }
   function del()
   {
        alert("<%=DeleteByID(8)%>");//DeleteByID(8)后台方法名
   }

三,采用ICallbackEventHandler回调

/**//*
* Declare the ICallbackEventHandler interface . To call the server code on the client without postback, you must declare the interface and implement its two methods:
* RaiseCallbackEvent( ), GetCallbackResult()
* The parameters of RaiseCallbackEvent() are passed from the front desk. Different codes are executed according to the passed parameters and the results are returned to the front desk with GetCallbackResult()
*/

//必须声明System.Web.UI.ICallbackEventHandler接口
public partial class _Default : System.Web.UI.Page, System.Web.UI.ICallbackEventHandler
{
   //定义一个回调的返回值
   private string Result;
   //定义两个变量,用来接收页面传过来到操作数
   private string Num1;
   private string Num2;
   protected void Page_Load(object sender, EventArgs e)
   {

    }

   /**////
   /// 该方法是回调执行的方法,根据参数在这个方法中处理回调的内容,该方法没有返回值
   ///

   /// 此参数是从客户端传过来的
   public void RaiseCallbackEvent(string eventArgument)
   {
//eventArgumeng is the parameter passed by javascript from the client. In this example, the three parameters are passed and separated by "/". Each parameter is taken out and stored in the array
string[] PagParams = eventArgument.Split( '/');
Num1 = PagParams[1];
Num2 = PagParams[2];
//According to the first parameter (selected operator), call Different execution function
# Switch (pagparams [0])
{## case "0":
result = add (); ## case "1" :
Result = sub(); break;
case "2":
Result = multi(); break;
case "3":
Result = pision(); break;
    }
    }

  /**////
  /// This method returns the result of the callback to the client
  ///  
///
public string GetCallbackResult()
{
  return Result;
}

//Four at a time The function is the function that calls the callback to perform the operation through the RaiseCallbackEvent method
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 ();
}
}

asp.net asynchronous trigger usage (AJAX)

The above is the detailed content of asp.net asynchronous trigger usage (AJAX). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn