Common Add graphics
Common_AddGraphics_CSharp\App_Code\Utility.cs
// Copyright 2010 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 =
            ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult.CreateJavaScript(jsAlertErrorMessage);
        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 =
        System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(
            System.Web.HttpContext.Current.Request.ApplicationPath);

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

        // 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();
        }
        else
            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 =
            ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult.CreateJavaScript(jsAlertException);

        // 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();
        callbackResultCollection.Add(alertCallbackResult);

        return callbackResultCollection;
    }

    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);
            else
                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;
                break;
            }

            // 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)
                    break;
            }
        }
        return uniqueID;
    }

    internal static string RenderControlHtml(System.Web.UI.Control webControl)
    {
        // Instantiate StringWriter and HtmlTextWriter objects to render the control to and
        // retrieve the control's HTML
        System.IO.StringWriter stringWriter = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter htmlTextWriter = new System.Web.UI.HtmlTextWriter(stringWriter);

        // Render the passed-in control to the HtmlTextWriter
        webControl.RenderControl(htmlTextWriter);
        // Retrieve the control's html as a string from the StringWriter.  The state
        // of the StringWriter is automatically updated via its reference to the HtmlTextWriter
        string htmlContent = stringWriter.ToString();
        // Since we are done with the StringWriter, close it
        stringWriter.Close();

        // Return the control's HTML as a string
        return htmlContent;
    }
}