Home >Backend Development >C#.Net Tutorial >asp.net asynchronous trigger usage (AJAX)
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
/configuration>
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 +='
学生编号 | ';名称 | ';年龄 | ';
'+ dataset.Tables[0].Rows[i].stu_id +' | ';'+ dataset.Tables[0].Rows[i].stu_name +' | ';'+ dataset.Tables[0].Rows[i].stu_age +' | ';
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!