JSON is a lightweight, data-interchange format. ASP.NET
gives good support for JSON as well. It is rapidly adopting because it is easy
read by human and machine as well.
ICallback Server Side Implementation
Let us first implement ICallbackEventHandler to call the
server side method asynchronously step-by-step
Implement Server Side (C#) Page/Control class by
System.Web.UI.ICallbackEventHandler.
The following are the definition of two methods which we
need to implement.
RaiseCallbackEvent method invoke through JavaScript function.
Listing 1
public void RaiseCallbackEvent(string eventArgument)
{
//to do code here
}
GetCallbackResult method invokes itself when the processing
of RaiseCallbackEvent method is completed.
Listing 2
public string GetCallbackResult()
{
return "";
}
In Page_Load or Page_Init event the following statements are
used to register client side methods.
CallServer(arg, context), as the name implies, would use call/raise
server side method which was RaiseCallbackEvent string eventArgument).
ReceiveServerData(arg, context) would get the result through
arg parameter by GetCallbackResult().
Listing 3
protected void Page_Load(object sender, EventArgs e)
{
ClientScriptManager scriptMgr = Page.ClientScript;
String cbReference = scriptMgr.GetCallbackEventReference(this, "arg",
"ReceiveServerData", "");
String callbackScript = "function CallServer(arg, context) {" + cbReference + "; }";
scriptMgr.RegisterClientScriptBlock(this.GetType(),"CallServer", callbackScript, true);
}
ICallback Client Side Implementation
Listing 4
<script language=javascript type=text/javascript>
function ReceiveServerData(arg, context)
{
alert(arg); //just to show output
}
function CallSrv()
{
CallServer('get customer', '');
}
</script>
<input type="button" value="get customer" onclick="CallSrv()" />
That is it. These are the steps which you need to use to
call and get results from the server side code using ICallback.
Now we will go ahead with some very easy steps for JSON
based JavaScript serialization to return results in JavaScript's easily
parseable format.
Suppose we have the following class whose object we need to
return to JavaScript function through JavaScript serialization.
Sample Class:
Listing 5
public class Customer
{
public string Name;
public int Age;
}
JSON Code
Declare string jsonResult at the class level, which would be
used to contain final results and returns.
After some sample code in both methods, the code will look
like the following:
Listing 6
public void RaiseCallbackEvent(string eventArgument)
{
//populate Customer object to return
Customer customer = new Customer();
customer.Name = "Muhammad Adnan";
customer.Age = 24;
//javascript serialization of Customer object
System.Web.Script.Serialization.JavaScriptSerializer jss;
jss = new System.Web.Script.Serialization.JavaScriptSerializer();
//stringbuilder to contain serialized customer object
System.Text.StringBuilder sbCustomer = new System.Text.StringBuilder();
jss.Serialize(customer, sbCustomer);
jsonResult = sbCustomer.ToString();
}
public string GetCallbackResult()
{
return jsonResult;
}
Asynchronously output would be within a millisecond and
without Postback.
Figure 1