Common Select Buffer tool
Common_SelectBufferTool_CSharp\App_Code\Utility.cs
// Copyright 2011 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.
// 

namespace CustomComponents
{
    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("\\", "\\\\");
                errorMessage = exception.Message + "\\n\\n" + stackTrace.Trim();
            }
            else
                errorMessage = exception.Message;

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

        // Searches the passed-in control and all its child controls, returning that having the passed-in
        // control ID, if found
        public static System.Web.UI.Control FindControl(System.Web.UI.Control rootControl, string controlID)
        {
            // Return the passed-in control if it has the passed-in ID
            if (rootControl.ID == controlID)
            {
                return rootControl;
            }

            // Iterate through the control's child controls, recursively calling FindControl on each.
            // If at any point a control with the passed-in ID is found, return it.
            foreach (System.Web.UI.Control childControl in rootControl.Controls)
            {
                System.Web.UI.Control foundControl = FindControl(childControl, controlID);
                if (foundControl != null)
                {
                    return foundControl;
                }
            }

            return null;
        }

        // Finds the first node with the specified text in the specified Toc Control.
        public static ESRI.ArcGIS.ADF.Web.UI.WebControls.TreeViewPlusNode FindNode(
            ESRI.ArcGIS.ADF.Web.UI.WebControls.Toc adfToc, string nodeText)
        {
            // Iterate through the top-level nodes in the Toc, calling FindNodeRecursive on each.  If 
            // FindNodeRecursive returns a node, return this as the result of the function.
            foreach (ESRI.ArcGIS.ADF.Web.UI.WebControls.TreeViewPlusNode tocTreeViewPlusNode in adfToc.Nodes)
            {
                ESRI.ArcGIS.ADF.Web.UI.WebControls.TreeViewPlusNode matchingTreeViewPlusNode =
                    FindNodeRecursive(tocTreeViewPlusNode, nodeText);
                if (matchingTreeViewPlusNode != null)
                    return matchingTreeViewPlusNode;
            }

            return null;
        }

        // Finds the node having the passed-in name from among the input node or its child nodes. 
        // Useful for searching a Toc with group layers.
        internal static ESRI.ArcGIS.ADF.Web.UI.WebControls.TreeViewPlusNode FindNodeRecursive(
            ESRI.ArcGIS.ADF.Web.UI.WebControls.TreeViewPlusNode treeViewPlusNode, string nodeName)
        {
            // Check whether the text of the passed-in node matches the text sought.  Return the node if so.
            if (treeViewPlusNode.Text == nodeName)
                return treeViewPlusNode;

            // Iterate through the passed-in node's child nodes, calling this function on each.
            foreach (ESRI.ArcGIS.ADF.Web.UI.WebControls.TreeViewPlusNode childTreeViewPlusNode in treeViewPlusNode.Nodes)
            {
                ESRI.ArcGIS.ADF.Web.UI.WebControls.TreeViewPlusNode childNode =
                    FindNodeRecursive(childTreeViewPlusNode, nodeName);
                if (childNode != null)
                    return childNode;
            }

            // If the code reaches this point, no match was found. 
            return null;
        }
    }
}