Using the Callback control framework

Ryan Olson of the Web ADF development team contributed the following code sample that demonstrates how to use the callback framework from within the ESRI Web ADF for the Microsoft .NET Framework. 

This control is very useful for testing things during the context of a callback.  When called, it fires a server-side event in the context of a page callback so that you can attach to that event at design time or programmatically and act on the event.  You can return "javascript" as the event argument to execute a JavaScript function or you can refresh the contents of a non-AJAX-enabled control (like the Gridview) without having to write a line of JavaScript.  

The code is extremely simple and yet it shows how to use the Web ADF CallbackResults and the WebControl base class Callback implementation.

 

Here is some sample code to execute some javascript

    protected void CallbackButton1_Clicked(object sender, EventArgs args)

    {

        CallbackButton1.CallbackResults.Add(new ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult(CallbackButton1, "javascript", "alert('hello');"));

    }

Here is some sample code to refresh a control that doesn’t inherit from our webcontrols with the CallbackButton (like a gridview):

    protected void CallbackButton1_Clicked(object sender, EventArgs args)

    {

        CallbackButton1.CallbackResults.Add(RefreshControlHtml(GridView1));

    }

    private CallbackResult RefreshControlHtml(Control control)

    {

        System.IO.StringWriter sw = new System.IO.StringWriter();

        HtmlTextWriter writer = new HtmlTextWriter(sw);

        control.RenderControl(writer);

        string htmlContent = sw.ToString();

        sw.Close();

        return new CallbackResult(control, "content", htmlContent);

    }

 

Try out a sample application illustrating the usage here:  http://serverx.esri.com/callbackbuttonSample/ 

 

This entry was posted in Services and tagged , . Bookmark the permalink.

Leave a Reply

2 Comments

  1. Ryan Olson says:

    You can send back JSON, no problem. Send it as a parameter to a javascript function that you would like to execute. Use “javascript” as the event argument. We haven’t tested this with Prototype, YUI or jQuery, so I can’t comment on that.

  2. Bryan Baker says:

    The CallbackResult class is part of the Web ADF. It builds on the client callback framework that is part of ASP.NET 2.0. It makes transferring information between server and browser easier.

    The WebControl abstract class, in the ESRI.ArcGIS.Web.UI.WebControls assembly, extends the ASP.NET WebControl class by adding the CallbackResults property. So any control that extends the Web ADF’s WebControl can take advantage of the CallbackResults to send data.