Implementing Callback Methods on Custom or User Controls

One powerful feature of the wwCallbackMethod control is the ability to call back to any control on a Page. This means it's possible to implement [CallbackMethod] handlers on any control with the control managing the routing of requests to the appropriate control methods. This makes it possible for custom control developers to have a consistent communications mechanism between the client page and the server.

To hook wwMethodCallback to a user or custom control:

Start by creating your control as usual and implement whatever features the control will have. The add your Callback methods that you want to expose to client callbacks and mark them up with [CallbackMethod] attribute as usual:

[CallbackMethod] public int Add(int x, int y) { return x + y; }

Then add the OnInit() code to add the wwMethodCallback control that will handle these callback methods:

protected override void OnInit(EventArgs e) { base.OnInit(e); // *** Add a MethodCallback control to this control // *** So we can make AJAX callbacks to this control wwMethodCallback Callback = new wwMethodCallback(); Callback.ID = this.ID + "_Callback"; Callback.TargetControlId = this.ID; this.Controls.Add(Callback); ... your OnInit() code this.RequestId = this.Context.Request.Form[this.ID + "_ReqId"]; if (this.RequestId == null) this.RequestId = ""; }

That's all it takes. The control takes care of the rest.

The control will generate a client side proxy of your class named with the same name as the control's ID (note it uses the ID not the UniqueID or ClientID - so you have to ensure uniqueness of your IDs!). You can then call the Proxy methods from your client side code. The control will automatically route the calls to the appropriate Callback methods and return the result to the client.

This mechanism works both with custom and user controls. For user controls you can also drop the wwMethodCallback control directly into the user control markup.

wwMethodCallback in the Page Cycle

Note that callbacks are handled in the wwMethodCallback.Load event at the end of which the current request is exited with a Response.End() after the control has generated its JSON output. This means that Page events will fire only up Page.Load() so you shouldn't count on any resources that get updated later in the page/control cycle.

  Last Updated: 12/29/2007 | © West Wind Technologies, 2008