Click here to Skip to main content
15,881,204 members
Articles / Web Development / HTML
Article

ICallbackEventHandler

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
11 Oct 2013CPOL 9.3K   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<br /><br />  public string GetCallbackResult()<br />  {return result;}<br />  public void RaiseCallbackEvent(string eventArgument)<br />  {<br />      result = "Call Back Result";<br />  }<br /><br />  #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");<br /> string script = "function CallBack(arg,context){" + callback + ";}";<br />  ClientScript.RegisterClientScriptBlock(this.GetType(), "CB", script, true);
so now our class looks like this,
using System;<br />using System.Configuration;<br />using System.Data;<br />using System.Linq;<br />using System.Web;<br />using System.Web.Security;<br />using System.Web.UI;<br />using System.Web.UI.HtmlControls;<br />using System.Web.UI.WebControls;<br />using System.Web.UI.WebControls.WebParts;<br />using System.Xml.Linq;<br /><br />public partial class _Default : System.Web.UI.Page, ICallbackEventHandler<br />{<br />  public string result;<br />  protected void Page_Load(object sender, EventArgs e)<br />  {<br />      string callback = ClientScript.GetCallbackEventReference(this, "arg", "GetName", "context");<br />      string script = "function CallBack(arg,context){" + callback + ";}";<br />      ClientScript.RegisterClientScriptBlock(this.GetType(), "CB", script, true);<br />  }<br /><br />  #region ICallbackEventHandler Members<br />  public string GetCallbackResult()<br />  {<br />      return result;<br />  }<br /><br />  public void RaiseCallbackEvent(string eventArgument)<br />  {<br />      result = "Call Back Result";<br />  }<br />  #endregion<br />}
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()<br />  {<br />      CallBack();      <br />  }<br />  function GetName(arg,context)<br />  {      <br />      document.getElementById('txtPercentage').value=arg;<br />  }
Build and Run the application.Now while clicking HTML Button the textbox is populated with the value from the server without posting the page.
This article was originally posted at http://wiki.asp.net/page.aspx/874/icallbackeventhandler

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
The ASP.NET Wiki was started by Scott Hanselman in February of 2008. The idea is that folks spend a lot of time trolling the blogs, googlinglive-searching for answers to common "How To" questions. There's piles of fantastic community-created and MSFT-created content out there, but if it's not found by a search engine and the right combination of keywords, it's often lost.

The ASP.NET Wiki articles moved to CodeProject in October 2013 and will live on, loved, protected and updated by the community.
This is a Collaborative Group

755 members

Comments and Discussions

 
-- There are no messages in this forum --