Implement the ICallbackEventHandler interface for any custom control that needs to receive callback events. For more information, see Implementing Client Callbacks Without Postbacks.
- First define string variable in Common section of your code behind (.cs) file.
string _CallBackString;
- Inherit ICallbackEventHandler in your page.
public partial class IcallBack : System.Web.UI.Page, ICallbackEventHandler
- Now, write this code in your Page_Load() Event
protected void Page_Load(object sender, EventArgs e)
{
ClientScriptManager cs = Page.ClientScript;
string cbRef = cs.GetCallbackEventReference(this, "arg", "ShowPop", "context");
string cbScript = "function CallPopBack(arg, context){" + cbRef + ";}";
cs.RegisterClientScriptBlock(this.GetType(), "CallPopBack", cbScript, true);
}
- Now, Write some javascript code, in your design section of your page.
<script type="text/javascript">
function GetPop()
{
var justExample = 'Hi..All..';
CallPopBack(justExample)
}
function ShowPop(result, context)
{
var strResult = new String();
strResult = result;
document.getElementById("txt").value=result;
}
</script>
5.Now, In your code behind (.cs) file, write code for your EventHandle Method.
public string GetCallbackResult()
{
return _CallBackString;
}
public void RaiseCallbackEvent(string eventArgument)
{
_CallBackString = eventArgument + " " + DateTime.Now.ToShortTimeString();
}
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="IcallBack.aspx.cs" Inherits="IcallBack" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
<script type="text/javascript">
function GetPop()
{
var justExample = 'Hi..All..';
CallPopBack(justExample)
}
function ShowPop(result, context)
{
var strResult = new String();
strResult = result;
document.getElementById("txt").value=result;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input type="button" value="Text ICallBack" onclick="return GetPop()"/>
<input type="text" name="txt" id="txt"/>
</div>
</form>
</body>
</html>
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class IcallBack : System.Web.UI.Page, ICallbackEventHandler
{
string _CallBackString;
protected void Page_Load(object sender, EventArgs e)
{
ClientScriptManager cs = Page.ClientScript;
string cbRef = cs.GetCallbackEventReference(this, "arg", "ShowPop", "context");
string cbScript = "function CallPopBack(arg, context){" + cbRef + ";}";
cs.RegisterClientScriptBlock(this.GetType(), "CallPopBack", cbScript, true);
}
public string GetCallbackResult()
{
return _CallBackString;
}
public void RaiseCallbackEvent(string eventArgument)
{
_CallBackString = eventArgument + " " + DateTime.Now.ToShortTimeString();
}
}