ArcGIS Add Dynamic Data
// Copyright 2009 ESRI
// All rights reserved under the copyright laws of the United States
// and applicable international laws, treaties, and conventions.
// You may freely redistribute and use this sample code, with or
// without modification, provided you include the original copyright
// notice and use restrictions.
// See the use restrictions.

public class Utility
    // Constructs a callback result that will display an error message based on the passed-in
    // exception
    public static ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult GetErrorCallback(
        System.Exception exception)
        // Create a callback result to display an error message
        string jsAlertErrorMessage = GetJavaScriptErrorString(exception);
        ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult alertCallbackResult =
        return alertCallbackResult;

    // Constructs JavaScript necessary to display an error message based on the passed-in exception.
    public static string GetJavaScriptErrorString(System.Exception exception)
        // Get the website's configuration file
        System.Configuration.Configuration webConfig =

        // Get the "compilation" section of the config file
        System.Web.Configuration.CompilationSection compilationSection =
            webConfig.GetSection("system.web/compilation") as

        // If the config file's compilation section specifies debug mode, include 
        // stack trace information in the error message.  Otherwise, just return 
        // the exception message.
        string errorMessage = null;
        if ((compilationSection != null) && (compilationSection.Debug))
            string stackTrace = exception.StackTrace.Replace("\\", "\\\\");
            stackTrace = stackTrace.Replace("\n", "\\n");
            stackTrace = stackTrace.Replace("\r", "\\r");
            stackTrace = stackTrace.Replace("'", "\\'");
            errorMessage = exception.Message.Replace("\\", "\\\\");
            errorMessage = errorMessage.Replace("\n", "\\n");
            errorMessage = errorMessage.Replace("\r", "\\r");
            errorMessage = errorMessage.Replace("'", "\\'");

            errorMessage = errorMessage + "\\n\\n" + stackTrace.Trim();
            errorMessage = exception.Message;

        // Create a callback result to display an error message
        string jsAlertException = "alert('" + errorMessage + "')";
        return jsAlertException;

    public static ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResultCollection CreateErrorCallback(
        string message)
        // Create a string containing the JavaScript necessary to display an alert with the passed-in 
        // message as text
        string jsAlertException = "alert('" + message + "')";
        // Create a callback result encapsulating the JavaScript
        ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult alertCallbackResult =

        // Create a Web ADF CallbackResultCollection and add the callback results created above to it
        ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResultCollection callbackResultCollection =
            new ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResultCollection();

        return callbackResultCollection;

    public static System.Web.UI.Control GetPostBackControl(System.Web.UI.Page page)
        string postBackControlID = null;
        if (page.IsCallback)
            postBackControlID = page.Request.Params["__CALLBACKID"];
            System.Web.UI.ScriptManager scriptManager = System.Web.UI.ScriptManager.GetCurrent(page);
            postBackControlID = scriptManager.AsyncPostBackSourceElementID;

        return FindControl(postBackControlID, page);

    internal static System.Web.UI.Control FindControl(string controlID, System.Web.UI.Page page)
        // Make sure the passed-in page and control are not null
        if (page == null || controlID == null) return null;

        // Try getting a reference to the control being sought via the page's FindControl function
        System.Web.UI.Control webControl = page.FindControl(controlID);

        // Check whether the control was found
        if (webControl == null)
            // Call method to traverse the Page's controls and find the unique id of the control
            // having the passed in control ID
            string uniqueControlID = GetControlUniqueID(controlID, page.Controls);
            if (uniqueControlID != null)
                webControl = page.FindControl(uniqueControlID);
                webControl = page.FindControl(controlID);
        return webControl;

    internal static string GetControlUniqueID(string controlID, System.Web.UI.ControlCollection controls)
        // Declare a Control object to store references to controls in the passed-in ControlCollection
        System.Web.UI.Control control;
        // Declare a string variable to store references to the UniqueIDs of controls in the passed-in
        // collection
        string uniqueID = null;

        // Iterate through the controls in the passed-in collection
        for (int i = 0; i < controls.Count; ++i)
            // Get a reference to the current control
            control = controls[i];

            // Check whether the current control's ID matches the passed in control ID
            if (control.ID == controlID)
                // The control's ID matches, so get a reference to its UniqueID and exit the loop
                uniqueID = control.UniqueID;

            // Check whether the current control contains any child controls
            if (control.Controls.Count > 0)
                // Recursively call GetControlUniqueID with the passed-in control ID and the current
                // control's collection of child controls
                uniqueID = GetControlUniqueID(controlID, control.Controls);
                // Check whether the ID was found.  If so, exit the loop
                if (uniqueID != null)
        return uniqueID;