Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

ICallbackEventHandler

0.00/5 (No votes)
5 Aug 2009 1  
By using ICallbackEventHandler we can make asynchronous calls to server so that we can avoidpost back of the web page.Firstly we have to implement

This articles was originally at wiki.asp.net but has now been given a new home on CodeProject. Editing rights for this article has been set at Bronze or above, so please go in and edit and update this article to keep it fresh and relevant.

By using ICallbackEventHandler we can make asynchronous calls to server so that we can avoid post back of the web page.

Firstly we have to implement the interface ICallbackEventHandler,
public partial class _Default : System.Web.UI.Page, ICallbackEventHandler
then we have to implement two methods in the interface ICallbackEventHandler,
  • GetCallbackResult
  • RaiseCallbackEvent
  #region ICallbackEventHandler Members

public string GetCallbackResult()
{return result;}
public void RaiseCallbackEvent(string eventArgument)
{
result = "Call Back Result";
}

#endregion
Here for simplicity i am returning a static data.You can return dynamic data also.Then you need to Get Call back Event Reference through Page.ClientScript,
string callback = ClientScript.GetCallbackEventReference(this, "arg", "GetName", "context");
string script = "function CallBack(arg,context){" + callback + ";}";
ClientScript.RegisterClientScriptBlock(this.GetType(), "CB", script, true);
so now our class looks like this,
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class _Default : System.Web.UI.Page, ICallbackEventHandler
{
public string result;
protected void Page_Load(object sender, EventArgs e)
{
string callback = ClientScript.GetCallbackEventReference(this, "arg", "GetName", "context");
string script = "function CallBack(arg,context){" + callback + ";}";
ClientScript.RegisterClientScriptBlock(this.GetType(), "CB", script, true);
}

#region ICallbackEventHandler Members
public string GetCallbackResult()
{
return result;
}

public void RaiseCallbackEvent(string eventArgument)
{
result = "Call Back Result";
}
#endregion
}
Now add one textbox and one HTML Button to the designer.For call back we should use HTML Button.In the onclick event of HTML Button call the javascript method GetValue();

Our Javascript code looks like,
  function GetValue()
{
CallBack();
}
function GetName(arg,context)
{
document.getElementById('txtPercentage').value=arg;
}
Build and Run the application.Now while clicking HTML Button the textbox is populated with the value from the server without posting the page.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here