Client callbacks that come with Asp.Net 2.0
Asp.Net 2.0 has been released. There are many new features in 2.0, and client callbacks are one of them. Client-side callbacks allow us to call server-side methods without postbacks, which is consistent with the functions provided by AJAX, but not as flexible as AJAX. AJAX can customize the methods that are called. The callback function that comes with 2.0 is not. To use the client callback function, you must implement the System.Web.UI.IcallbackEventHandler interface.
This interface contains two methods
// This method is always called when the client callback
Public void RaiseCallbackEvent (String eventArgument)
// This method will be called after executing RaiseCallbackEvent. 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 some existing technologies, including: XmlHttp, Reflect. An AJAX framework basically includes: a custom HttpHandler, a piece of JavaScript code.
AJAX operation mechanism
When we used XmlHttp to implement a non-refresh page, we used XmlHttp to request a hidden page and used the (Asp / Asp.Net) HttpHandler. In AJAX, we requested a hidden page. It is the HttpHandler of this page that is implemented by ourselves.
Build your own AJAX
1. First we need to implement an HttpHandler (HttpHandler) to respond to the client's request:
Implement a custom HttpHandler to implement the IHttpHandler interface.
The interface contains a property 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 (); // Get the method to call
String methodName = context.Request.QueryString ["me"];
// Get assembly information.
//Czhenq.AJAX.Class1.Dencode is a custom string encoding method
String AssemblyName = Czhenq.AJAX.Class1.Dencode (context.Request.QueryString ["as"]);
// Get method parameters
String Arguments = context.Request.QueryString ["ar"]; // Start calling the method
Type type = Type.GetType (AssemblyName);
MethodInfo method = type.GetMethod (methodName,
BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance);
If (method! = Null)
{
// parameters are separated by ","
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 the parameters passed by XmlHttp are String type, it must be converted
// Here only the parameters are converted to Int32, no other considerations are made.
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");
}
// End of processing
Context.Response.End ();
2. Client-side Javascript code:
Function CallMethod (AssemblyName, MethodName, Argus)
{
Var args = "";
For (var i = 0; i
Args + = Argus [i] + ",";
If (args.length> 0) args = args.substr (0, args.length-1);
Var xmlhttp = new ActiveXObject ('Microsoft.XMLHTTP');
Url = "AJAX / AJAX.czhenq? As =" + AssemblyName + "& me =" + MethodName + "& ar =" + args;
Xmlhttp.open ("POST", url, false);
Xmlhttp.send ();
Alert (xmlhttp.responseText);
}
3. A simple AJAX framework has been implemented. Now write some code to test.
Use your own AJAX
1. Create a new website and apply the HttpHandler you just wrote. And register your HttpHandler in the Web.config of the website, stating that those requests will be processed using the Handler you write. The following explains: All requests ending with "czq" will be processed using "Czhenq.HttpHandlerFactory".
Type = "Czhenq.HttpHandlerFactory, Czhenq.AJAX" />
2. Add a web page, copy the script just into the page, and add a method you want to call.
Private string Add (int i, int j)
{
Return TextBox1.Text;
}
3. Place a HiddenField control on the page and name it AssemblyName. And add the following code in Page_Load:
String assemblyName = Czhenq.AJAX.Class1.Encode (
Typeof (_Default) .AssemblyQualifiedName);
AssemblyName.Value = assemblyName;
4. Add the following script to the page:
Var assemblyName = document.getElementById ("AssemblyName");
Var argus = new Array ();
Argus.push ("100");
Argus.push ("200");
CallMethod (assemblyName, "Add", argus)
Summary AJAX is not a new technology, it is just an organic combination of some existing technologies. We can simply understand AJAX as: AjAx is a wrapper for JavaScript calling XmlHttp, it changes the way code is written.
Enclosed with Encode and Dencode:
Public static string Encode (string value)
{
Byte [] bytes = ASCIIEncoding.ASCII.GetBytes (value);
Return Convert.ToBase64String (bytes);
}
Public static string Dencode (string value)
{
Byte [] bytes = Convert.FromBase64String (value);
Return ASCIIEncoding.ASCII.GetString (bytes);
}