Example for using CallbackEventHandler

The following two examples are very simple examples for AJAX in asp.net. The functions of the two examples are the same.

ref: "how to use CallbackEventHandler, the example", http://ramones.blogsome.com/2007/06/08/how-to-use-callbackeventhandler-the-example/

Example1:

Use only single file.

<%@Page Language="VB" %>

<%@Implements Interface="System.Web.UI.ICallbackEventHandler" %>

<script runat="server">

Dim result as string= string.Empty

' Define method that processes the callbacks on server.

'receive the argument transfered from client-side

Public Sub RaiseCallbackEvent(ByVal eventArgument As String) Implements System.Web.UI.ICallbackEventHandler.RaiseCallbackEvent

result = eventArgument & " and more"

End Sub

' Define method that returns callback result.

' return the result to the client-side function which is specified in the GetCallbackEventReference

Public Function GetCallbackResult() As String Implements System.Web.UI.ICallbackEventHandler.GetCallbackResult

return result

End Function

</script>

<!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 language="javascript" type="text/javascript">

// the <% %> code will becomes a js function when the page is displayed at the browser.

function jscall(arg,context)

{

<%= Page.ClientScript.GetCallbackEventReference(me, "arg", "receiveFunction", "context")%>;

}

// result from the server-side is passed to the "result" argument

function receiveFunction(result,context)

{

form1.test.value=result ;

}

</script>

</head>

<body>

<form id="form1" runat="server">

<input name="test" value='I love kerry more' size="100"/>

<input type="button" value="call" onclick="jscall(form1.test.value,'none')" />

</form>

</body>

</html>

Example2:

Use Code behide.

file: test_vb1.aspx

<%@Page Language="VB" CodeFile="test_vb1.aspx.vb" Inherits="test_vb1" %>

<!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 id="Head1" runat="server">

<title>Untitled Page</title>

<script language="javascript" type="text/javascript">

// the <% %> code will becomes a js function when the page is displayed at the browser.

function jscall(arg,context)

{

<%= Page.ClientScript.GetCallbackEventReference(me, "arg", "receiveFunction", "context")%>;

}

// result from the server-side is passed to the "result" argument

function receiveFunction(result,context)

{

form1.test.value=result ;

}

</script>

</head>

<body>

<form id="form1" runat="server">

<input name="test" value='I love kerry more' size="100"/>

<input type="button" value="call" onclick="jscall(form1.test.value,'none')" />

</form>

</body>

</html>

file: test_vb1.aspx.vb

Partial Class test_vb1

Inherits System.Web.UI.Page

Implements System.Web.UI.ICallbackEventHandler

Dim result As String = String.Empty

' Define method that processes the callbacks on server.

'receive the argument transfered from client-side

Public Sub RaiseCallbackEvent(ByVal eventArgument As String) Implements System.Web.UI.ICallbackEventHandler.RaiseCallbackEvent

result = eventArgument & " and more"

End Sub

' Define method that returns callback result.

' return the result to the client-side function which is specified in the GetCallbackEventReference

Public Function GetCallbackResult() As String Implements System.Web.UI.ICallbackEventHandler.GetCallbackResult

Return result

End Function

End Class