Explain in detail how to implement the most basic AJAX framework

Source: Internet
Author: User

Asp. Net 2.0 has been released. 2.0 has many new features, and client callback is one of them. Client callback allows us to call methods on the server without sending back. It is consistent with the functions provided by AJAX, but it is not as flexible as AJAX. AJAX can customize the called methods, the callback function provided by 2.0 does not work. To use the client callback function, you must implement the System. Web. UI. IcallbackEventHandler interface.

This interface contains two methods

// This method is fixed during client callback.
Public void RaiseCallbackEvent (String eventArgument)

// This method will be called after RaiseCallbackEvent is executed. The return value of this method will be sent back to the client.

Public string GetCallbackResult ()

Example:

. Cs:

String cbReference = Page. ClientScript. GetCallbackEventReference (

This, "arg", "ReceiveServerData", "context ");

String callbackScript;

CallbackScript = "function CallServer (arg, context)" + "{" + cbReference + "};";

Page. ClientScript. RegisterClientScriptBlock (

This. GetType (), "CallServer", callbackScript, true );

Javascript:
 
AJAX Introduction

AJAX is not a new technology, but an organic combination of existing technologies, including XmlHttp and Reflect. An AJAX framework basically includes a custom HttpHandler and a piece of JavaScript code.

AJAX Running Mechanism

In the past, when we used XmlHttp to implement a refreshing page, we used XmlHttp to request a hidden page. net) comes with HttpHandler, and in AJAX, we request a hidden page. The difference is that HttpHandler of this page is implemented by ourselves.

Build your own AJAX:

1. First, we need to implement an Http handler (HttpHandler) to respond to client requests:

To implement a custom HttpHandler, you must implement the IHttpHandler interface. This interface contains an attribute and a method:

  

Bool IHttpHandler. IsReusable
Void IHttpHandler. ProcessRequest (HttpContext context)

Example:

Bool IHttpHandler. IsReusable

{

Get {return true ;}

}

Void IHttpHandler. ProcessRequest (HttpContext context)

{

Context. Response. Clear (); // obtain the method to be called

String methodName = context. Request. QueryString ["me"];

// Obtain the assembly information.

// Czhenq. AJAX. Class1.Dencode is a custom string encoding method.

String AssemblyName = Czhenq. AJAX. Class1.Dencode (context. Request. QueryString ["as"]);

// Obtain method parameters

String Arguments = context. Request. QueryString ["ar"]; // start to call the Method

Type type = Type. GetType (AssemblyName );

MethodInfo method = type. GetMethod (methodName,

BindingFlags. NonPublic | BindingFlags. Public | BindingFlags. Static | BindingFlags. Instance );

If (method! = Null)

{

// Use "," to separate parameters

String [] args = Arguments. Split (",". ToCharArray ());

ParameterInfo [] paras = method. GetParameters ();

Object [] argument = new object [paras. Length];

For (int I = 0; I <argument. Length; I ++)

{

If (I <args. Length ){

// Because all parameters passed by XmlHttp are of the String type, they must be converted.

// Only convert the parameter to Int32.

Argument [I] = Convert. ToInt32 (args [I]);

}

}

Object value = method. Invoke (Activator. CreateInstance (type, true), argument );

If (value! = Null) context. Response. Write (value. ToString ());

Else context. Response. Write ("error ");

}

// Processing ends

Context. Response. End ();
 

  • 2 pages in total:
  • Previous Page
  • 1
  • 2
  • Next Page

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.