An overview of Ajax patterns under ASP.net

Source: Internet
Author: User

Article content:

I. INTRODUCTION

Second, Xmlhttp+webform mode

Three, Xmlhttp+httphandler mode

Four, asp.net 2.0/3.5 callback mode

Five, AJAX framework model

I. INTRODUCTION

In this article, we will introduce several common Ajax patterns under the ASP.net link. For example: Xmlhttprequest+webforms,xmlhttprequest+httphandlers,callbacks, and the use of Ajax frameworks, and so on, the following is a combination of examples to expand the introduction of this series of common patterns. Sample code downloads are provided later in this article.

Second, Xmlhttp+webform mode

Perhaps we all remember that Xmlhttp+webform mode is the most original Ajax mode under ASP.net. In this mode, it is through JavaScript to manipulate XMLHttpRequest objects, sending asynchronous requests to the form of Web Forms on the server side. On the other hand, creating Web Forms on the server side can be used directly to accept XMLHTTP requests. In this way, the browser's XMLHttpRequest object becomes a good means of communication between the server side and the client.

In this section, we will combine a simple example to help you understand the Ajax pattern of the xmlhttp+webform pattern in a more intuitive way.

(1), client

The client creates the XMLHttpRequest object and initiates the asynchronous request we encapsulate its JavaScript in a ajax.js file. This part of the method can be reused, in ajax.js there are three methods, Newxmlhttpreqeust,sendrequest and Onsuccesscallback methods, detailed as follows:

1//Create XMLHttpRequest Object
2var xmlHttp;
3function Newxmlhttprequest () {
4
5 if (window. XMLHttpRequest) {
6 xmlHttp = new XMLHttpRequest ();
7} else if (window. ActiveXObject) {
8 try {
9 xmlHttp = new ActiveXObject ("Msxml2.xmlhttp");
Ten} catch (E1) {
One try {
XmlHttp = new ActiveXObject ("Microsoft.XMLHTTP");
' Catch (E2) {
14}
15}
16}
return xmlHttp;
18}
19
20//initiates an asynchronous request
21function SendRequest () {
Newxmlhttprequest ();
The Var url= "ajaxform.aspx?name=" +document.getelementbyid ("Txtname"). Value;
Xmlhttp.open ("Get", url,true);
Xmlhttp.onreadystatechange=onsuccesscallback;
Xmlhttp.send (NULL);
27}
28
29//Callback handler function
30function Onsuccesscallback () {
if (xmlhttp.readystate = 4)
32 {
if (Xmlhttp.status = 200)
34 {
document.getElementById ("Result"). InnerHTML = Xmlhttp.responsetext;
36}
Panax Notoginseng Else
38 {
document.getElementById ("Result"). Innerhtml=result.status;
40}
41}
42}

The client invokes the method of creating the XMLHttpRequest object first before invoking the asynchronous initiation request, and finally updates the page display with the callback function.

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.