Using Ajax request in C #

Time:2021-1-25

Introduction to Ajax

AJAX is“Asynchronous Javascript And XML”(asynchronous JavaScript and XML) is a web page development technology to create interactive, fast and dynamic web page applications, which can update some web pages without reloading the whole web page.

Ajax can make web pages update asynchronously by exchanging a small amount of data with the server in the background. This means that a part of the page can be updated without reloading the entire page.

 

How to use ajax

1. Download first ajax.dll , a Baidu have to download! Find out for yourself.

2 ajax.dll Import to the project. Right click Project > Add Reference > Browse to find the downloaded project ajax.dll File, click OK. At this time, there is a bin folder in the project directory, which contains ajax.dll Document, which proves the introduction ajax.dll succeed.

3. Setting configuration file web.config .

stay Web.config Under the fileAdd the following code to the node:

4. Demonstration: 4.1 first of all, register Ajax. stay aspx.cs Page in code_ Load method to register Ajax. The registration method is Ajax.Utility.RegisterTypeForAjax (typeof (namespace. Class name)), if there is no namespace, you can write the class name directly. The code is as follows:

public partial class ObjManage : System.Web.UI.Page  
{  
    protected void Page_Load(object sender, EventArgs e)  
    {  
        Ajax.Utility.RegisterTypeForAjax(typeof(ObjManage));  
    }  
}

4.2 write the method of CS for JavaScript to call. CS method front end must have[ Ajax.AjaxMethod ]Then the method must be public and static. For example:

[Ajax.AjaxMethod]  
 public static string getString(string str)  
 {  
     string strResult = "The string is " + str;  
     return strResult;  
 }  

4.3 JavaScript calls CS method. The calling format is: class name. Method name (parameter), for example:

function alertString() {  
            var str = ObjManage.getString("myAjax").value;  
            alert(str);  
        }  

That’s it. This is through the test, if you have any questions, you can leave a message. The complete source code is given below Web.config Do not give the code, install the third step to set the configuration file web.config It’s OK to set it. CS Code:

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Web;  
using System.Web.UI;  
using System.Web.UI.WebControls;  
  
public partial class ObjManage : System.Web.UI.Page  
{  
    protected void Page_Load(object sender, EventArgs e)  
    {  
        Ajax.Utility.RegisterTypeForAjax(typeof(ObjManage));  
    }  
  
    [Ajax.AjaxMethod]  
    public static string getString(string str)  
    {  
        string strResult = "The string is " + str;  
        return strResult;  
    }  
}

————————————————–

Aspx Code:

function alertString() {  
            var str = ObjManage.getString("myAjax").value;  
            alert(str);  
        }

Link to the original text: http://blog.csdn.net/jony07/article/details/8080066

Recommended Today

Practice of query operation of database table (Experiment 3)

Following the previous two experiments, this experiment is to master the use of select statements for various query operations: single table query, multi table connection and query, nested query, set query, to consolidate the database query operation.Now follow Xiaobian to practice together!Based on the data table (student, course, SC, teacher, TC) created and inserted in […]