Monday, August 10, 2009

Use ICallbackEventHandler in Asp.Net 2.0

Use ICallbackEventHandler in Asp.Net 2.0
==================================

For Example, you have page Layout is like this :

<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
</HEAD>

<BODY>
<div id="divResult" runat="server">
</div>
<br/>
<input type="button" value="Text ICallBack" onclick="return GetPop()" />

</BODY>
</HTML>

Steps to Implement ICallbackEventHanler:
-------------------------------------------------

1. First define string variable in Common section of your code behind (.cs) file.

string _CallBackString;

2. Inherit ICallbackEventHandler in your page.

public partial class Customize : System.Web.UI.Page, ICallbackEventHandler

3. Now, write this code in your Page_Load() Event

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);

4. Now, Write some javascript code, in your design section of your page.

function GetPop()
{
var justExample = 'Hi..All..';

CallPopBack(justExample)
}
function ShowPop(result, context)
{

var strResult = new String();
strResult = result;

alert(result);
}

4. 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();
}

5. Run the page and test this page.

No comments:

Post a Comment