Call server side methods asynchronously without having a postback using callback in asp.net

Callback is another fabulous way to communicate with server for light weight tasks. While working in ASP.NET, sometimes we need to call server side methods asynchronously without having a postback. Sometimes it is either a full page postback or partial page postback.

To Implement callback in asp.net following interfaces to me implement in the asp.net page

  • ICallback
  • ICallbackEventHandler

Let us first implement ICallbackEventHandler to call the server side method asynchronously step-by-step. The following are the definition of two methods which we need to implement. RaiseCallbackEvent method invoke through JavaScript function.


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;

}

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().

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 + "; }";        

cm.RegisterClientScriptBlock(this.GetType(),"CallServer", callbackScript, true);

}

Client side ICallback Implementation

<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()" />

I have used 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.

public class Customer

{

  public string Name;

  public int Age;

}

using above steps we can get Asynchronously output within a millisecond and without Postback.
 

Comments

Popular posts from this blog

Implement Logging in CSV file using Nlog in .net core MVC application- part 2

Implement Nlog in .Net core MVC application part 1

Angular User Session Timeout example step by step

Restore the lost focus of Auto post back controls in asp.net update Panel control

Disable backspace key using Jquery