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