Chris Pietschmann

husband, father, hacker, entrepreneur, futurist, innovator, autodidact

NAVIGATION - SEARCH

ASP.NET Client Callbacks inside a User Control

Implementing Client Callbacks (AJAX) in ASP.NET 2.0 is actually really simple to do as long as you know a little JavaScript. Heres is a small example of using a Client Callback from within a User Control. I've tested this example with IE6 and Firefox 1.5

1 <%@ Control Language="VB" ClassName="ClientCallbackControl"%> 2 <%@ Implements Interface="System.Web.UI.ICallbackEventHandler"%> 3 <script runat="server"> 4 ''This is the variable that holds the client callback 5 ''results that will be returned to the client. 6 Dim MyCallbackResult As String 7 8 ''This is the server-side function that is called when the 9 ''client callback results are returned to the browser. 10 Public Function GetCallbackResult() As String Implements System.Web.UI.ICallbackEventHandler.GetCallbackResult 11 Return MyCallbackResult 12 End Function 13 14 ''This is the server-side function that is called when the 15 ''client callback is fired off. 16 Public Sub RaiseCallbackEvent(ByVal eventArgument As String) Implements System.Web.UI.ICallbackEventHandler.RaiseCallbackEvent 17 MyCallbackResult = Now.ToString 18 End Sub 19 20 Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load 21 ''Get Client Side ID of this instance of the User Control 22 ''This will be used to Prefix all the JavaScript functions 23 ''this control uses so that you can have multiple instances 24 ''of this control on the same page. 25 Dim strJSCallbackPrefix As String = Me.ClientID 26 27 ''Get the JavaScript that will callback to the server 28 Dim cm As ClientScriptManager = Page.ClientScript 29 Dim cbReference As String 30 cbReference = cm.GetCallbackEventReference(Me, _ 31 "arg", _ 32 strJSCallbackPrefix & "ReceiveServerData", _ 33 "context") 34 35 ''Declare the function that will be called to fire off a 36 ''client callback to the server. 37 Dim callbackScript As String = _ 38 "function " & strJSCallbackPrefix & "CallServer(arg, context){" & cbReference & "; }" 39 cm.RegisterClientScriptBlock(Me.GetType(), strJSCallbackPrefix & "CallServer", callbackScript, True) 40 41 ''Declare the function that will recieve the client callback 42 ''results from the server. 43 Dim strReceiveServerData As String = _ 44 "function " & strJSCallbackPrefix & "ReceiveServerData(arg, context){context.innerHTML = arg;}" 45 cm.RegisterClientScriptBlock(Me.GetType, strJSCallbackPrefix & "ReceiveServerData", strReceiveServerData, True) 46 47 48 ''Set the JavaScript that is run when the button is clicked 49 ''This sends the context of lblMessage1 so that the 50 ''ReceiveServerData function can change its value to what is 51 ''returned from the server. 52 Button1.OnClientClick = _ 53 strJSCallbackPrefix & "CallServer(1, document.getElementById('" & lblMessage1.ClientID & "')); " & _ 54 "return false;" 55 56 57 ''Load lblMessage1 with it's value on initial load of the page. 58 RaiseCallbackEvent(1) 59 lblMessage1.Text = GetCallbackResult() 60 End Sub 61 </script> 62 <asp:Button ID="Button1" runat="server" Text="Get DateTime Stamp" /> 63 &nbsp;&nbsp; 64 <asp:Label runat="server" ID="lblMessage1"></asp:Label>
blog comments powered by Disqus