Microsoft Most Valuable Professional

Chris Pietschmann

An MVP From Wisconsin



ASP.NET 2.0 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>

Currently rated 3.3 by 3 people

  • Currently 3.333333/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:
Categories: General
Posted by crpietschmann on Saturday, December 10, 2005 2:10 PM
Permalink | Comments (11) | Post RSSRSS comment feed

Related posts

Comments

Eric Rupp

Thursday, March 30, 2006 7:34 PM

Eric Rupp

Thank you very much for this example. Finding this type of code that works and does not apply to the Beta version of VS2005 is difficult.

Larry K.

Tuesday, April 04, 2006 4:28 PM

Larry K.

Doesn't using an ASP:Button (runat="server") always trigger a PostBack? I think if you add some code to interrogate for a PostBack event, you will find one is occurring.

Christopher Pietschmann, MCSD, MCAD

Wednesday, April 05, 2006 4:15 AM

Christopher Pietschmann, MCSD, MCAD

Actually, the “return false;” located in the JavaScript that’s set to fire off for the onClick event of the button prevents the PostBack. You’ll see this on line 54 of the code example.

bdunn

Tuesday, May 02, 2006 8:09 AM

bdunn

This is nice, but it does not work if the user control is inside a master page.. any ideas on that?

DMC

Wednesday, May 24, 2006 9:00 AM

DMC

The callback works fine if all this code is in a standalone .ASPX page but as soon as it is in a UserControl (.ascx) the callback event does not fire

Sharbel

Thursday, May 25, 2006 6:01 AM

Sharbel

There shouldnt be a problem in having the UserControl in a master page.. your webform_doCallback should look something like this:

WebForm_DoCallback('_ctl0:contentMain:InstanceName',params,afterCallbackFunction,before,null,false);

HTHs

Chris Pietschmann

Friday, May 26, 2006 5:42 AM

Chris Pietschmann

I don't know what you mean that the callback event doesn't fire when this code is placed in a UserControl. The code sample I posted is a UserControl that implements a client callback.

Chun Chang

Monday, July 17, 2006 8:56 PM

Chun Chang

Thanks for the nice example. I wish I found your sample earlier. Then I don't have to go throughall the hassles myself.
My senario is like this: when the button is clicked, it goes to the server to do the validation through the client call back. If the validation is passed, I want the code in the button's event-handler (server-side) to be executed, otherwise, do run the code. I.e., I cannot simply have the return false; in OnClientClick.
How do you accomplish this?

Michael Shorten

Tuesday, August 29, 2006 7:09 PM

Michael Shorten

Unfortunately, the comments from 5/24 are correct. I have an ASCX inside of an ASPX. I do exactly what you have laid out, and at the point of the WebForm_DoCallback, the callback vanishes and is never consumed by the ASCX. It also doesn't seem to be consumed by the parent page either - as trying to develop the eventhandler reference to this.Page (or Me.Page for VB) still results in the callback vanishing.

Thanks in advance if you have any thoughts.

MS

Steve Hayles

Friday, September 15, 2006 2:45 AM

Steve Hayles

Hi,

Did anyone solve the problem of getting the callback to work when its inside a user control on an ASPX page. I have been wrestling with the problem for some time with no success

Palak Chokshi

Friday, October 27, 2006 9:24 PM

Palak Chokshi

I have got callbacks to work in an ASCX control. The code is similar to the one above except that I do the Page.ClientScript.GetCallbackEventReference and all the other code to implement callback in the Page's Page_Load event handler and not the ASCX control's Page_Load method. Doing this makes it work in IE but not in Firefox. Does anyone know why it wouldn't work in Firefox??

Comments are closed

About the author

I'm Chris Pietschmann, go to the About Me page to learn more about me.

Search

Sponsors

Web.Maps.VE - ASP.NET AJAX Virtual Earth Mapping Server Control

Recent comments

Disclaimer


This work is licensed under a Creative Commons Attribution 3.0 United States License, unless explicitly stated otherwise within the posted content.
© Copyright 2004 - 2008 Chris Pietschmann