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 +' | ';
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
/// summary>
///
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 ();
}
}

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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Title: Methods and code examples to resolve 403 errors in jQuery AJAX requests. The 403 error refers to a request that the server prohibits access to a resource. This error usually occurs because the request lacks permissions or is rejected by the server. When making jQueryAJAX requests, you sometimes encounter this situation. This article will introduce how to solve this problem and provide code examples. Solution: Check permissions: First ensure that the requested URL address is correct and verify that you have sufficient permissions to access the resource.

jQuery is a popular JavaScript library used to simplify client-side development. AJAX is a technology that sends asynchronous requests and interacts with the server without reloading the entire web page. However, when using jQuery to make AJAX requests, you sometimes encounter 403 errors. 403 errors are usually server-denied access errors, possibly due to security policy or permission issues. In this article, we will discuss how to resolve jQueryAJAX request encountering 403 error

Build an autocomplete suggestion engine using PHP and Ajax: Server-side script: handles Ajax requests and returns suggestions (autocomplete.php). Client script: Send Ajax request and display suggestions (autocomplete.js). Practical case: Include script in HTML page and specify search-input element identifier.

How to solve the problem of jQueryAJAX error 403? When developing web applications, jQuery is often used to send asynchronous requests. However, sometimes you may encounter error code 403 when using jQueryAJAX, indicating that access is forbidden by the server. This is usually caused by server-side security settings, but there are ways to work around it. This article will introduce how to solve the problem of jQueryAJAX error 403 and provide specific code examples. 1. to make

Using Ajax to obtain variables from PHP methods is a common scenario in web development. Through Ajax, the page can be dynamically obtained without refreshing the data. In this article, we will introduce how to use Ajax to get variables from PHP methods, and provide specific code examples. First, we need to write a PHP file to handle the Ajax request and return the required variables. Here is sample code for a simple PHP file getData.php:

Ajax (Asynchronous JavaScript and XML) allows adding dynamic content without reloading the page. Using PHP and Ajax, you can dynamically load a product list: HTML creates a page with a container element, and the Ajax request adds the data to that element after loading it. JavaScript uses Ajax to send a request to the server through XMLHttpRequest to obtain product data in JSON format from the server. PHP uses MySQL to query product data from the database and encode it into JSON format. JavaScript parses the JSON data and displays it in the page container. Clicking the button triggers an Ajax request to load the product list.

Ajax is not a specific version, but a technology that uses a collection of technologies to asynchronously load and update web page content. Ajax does not have a specific version number, but there are some variations or extensions of ajax: 1. jQuery AJAX; 2. Axios; 3. Fetch API; 4. JSONP; 5. XMLHttpRequest Level 2; 6. WebSockets; 7. Server-Sent Events; 8, GraphQL, etc.

In order to improve Ajax security, there are several methods: CSRF protection: generate a token and send it to the client, add it to the server side in the request for verification. XSS protection: Use htmlspecialchars() to filter input to prevent malicious script injection. Content-Security-Policy header: Restrict the loading of malicious resources and specify the sources from which scripts and style sheets are allowed to be loaded. Validate server-side input: Validate input received from Ajax requests to prevent attackers from exploiting input vulnerabilities. Use secure Ajax libraries: Take advantage of automatic CSRF protection modules provided by libraries such as jQuery.
