Asp.Net 2.0's own client callback
Asp.Net 2.0 has been released. 2.0 has many new features, and client callbacks are one of them. Client callbacks allow us to call server-side methods without postbacks. They are consistent with the functions provided by AJAX, but are not as flexible as AJAX. AJAX can customize the calling methods, but the callback function that comes with 2.0 cannot. 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 calls back
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:
Introduction to AJAX
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 and a piece of JavaScript code.
AJAX operating mechanism
In the past, when we used XmlHttp to implement a non-refresh page, we used , the difference is that the 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 need to implement the IHttpHandler interface.
The interface contains one property and one 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 be called
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 of String type, they must be converted
//Here we only convert the parameters 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();
Welcome to the .NET community forum and interact with 2 million technical staff >> Enter
2. Client 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 a piece of code to test.
Use your own AJAX
1. Create a new website and use the HttpHandler you just wrote. And register your HttpHandler in the website's Web.config, indicating that those requests will be processed using the Handler you wrote. The following content 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 now to 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;
var assemblyName = document.getElementById("AssemblyName"); var argus = new Array();argus.push("100");argus.push("200");CallMethod(assemblyName,"Add",argus)
To summarize, AJAX is not a new technology. It is just an organic combination of some existing technologies. We can simply understand AJAX as: AjAx is an encapsulation of JavaScript calling XmlHttp. What it changes is the way of writing code.
Attached is the implementation of 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);
}