ArcGIS Simple Edit service
ArcGIS_SimpleEdit_CSharp\ArcGIS_SimpleEdit_WebAppCSharp\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
{
    /// <summary>
    /// Constructs a callback result that will display a javascript alert with an error message 
    /// based on the passed-in exception
    /// </summary>
    /// <param name="exception">The exception from which the error message will be derived</param>
    /// <returns></returns>
    public static ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult CreateErrorCallbackResult(
        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;
    }

    /// <summary>
    /// Constructs the syntax to display a javascript alert with an error message based on the 
    /// passed-in exception
    /// </summary>
    /// <param name="exception">The exception from which the error message will be derived</param>
    /// <returns></returns>
    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 void DisplayOrHideSelectionTable(ESRI.ArcGIS.ADF.Web.UI.WebControls.Map adfMap,
            ESRI.ArcGIS.ADF.Web.Display.Graphics.GraphicsLayer resultsGraphicsLayer, bool showTable)
        {
            try
            {

                // Get the GridView control that will be used to display selection results
                System.Web.UI.WebControls.GridView gridViewResults =
                    (System.Web.UI.WebControls.GridView)adfMap.Page.FindControl("grdIdentifyResults");

                System.Data.DataTable resultsDataTable = resultsGraphicsLayer;

                // Check whether to show selection results tabularly
                if (showTable)
                {
                    // Make sure results were actually found
                    if (resultsDataTable.Rows.Count > 0)
                    {
                        // Load the graphicsLayer into a new data table and remove the IS_SELECTED column
                        System.Data.DataTable newDataTable = new System.Data.DataTable();
                        newDataTable.Load(resultsGraphicsLayer.CreateDataReader());
                        newDataTable.Columns.Remove("IS_SELECTED");

                        // Bind the results table to the GridView control
                        gridViewResults.DataSource = newDataTable;
                        gridViewResults.DataBind();

                        // Use the StringWriter and HtmlTextWriter objects to get the HTML of the 
                        // GridView control as a string
                        string gridViewHTMLString = null;
                        System.IO.StringWriter stringWriter = new System.IO.StringWriter();
                        System.Web.UI.HtmlTextWriter htmlTextWriter =
                            new System.Web.UI.HtmlTextWriter(stringWriter);
                        gridViewResults.RenderControl(htmlTextWriter);
                        htmlTextWriter.Flush();
                        gridViewHTMLString = stringWriter.ToString();

                        // Create a callback result to update the content of the selection results div
                        // on the client with the HTML of the GridView control.  Add the callback result 
                        // to the map so it is processed with the map's other callback results.
                        ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult gridViewCallbackResult =
                            ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult.CreateSetInnerContent(
                            "identifyResultsDiv", gridViewHTMLString);
                        adfMap.CallbackResults.Add(gridViewCallbackResult);
                    }
                    else
                    {
                        // Create a callback result to update the content of the identify results div
                        // with a message informing the user that no features were selected.
                        ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult noFeaturesFoundCallbackResult =
                            ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult.CreateSetInnerContent(
                            "identifyResultsDiv", "No selected features");
                        adfMap.CallbackResults.Add(noFeaturesFoundCallbackResult);
                    }

                    // Create a JavaScript callback result to make the div containing the GridView control
                    // visible
                    string jsSetDivVisibility =
                        "document.getElementById('identifyResultsDiv').style.visibility = 'visible';";
                    ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult setVisibilityCallbackResult =
                        ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult.CreateJavaScript(jsSetDivVisibility);
                    adfMap.CallbackResults.Add(setVisibilityCallbackResult);
                }
                else
                {
                    string jsSetDivVisibility =
                        "document.getElementById('identifyResultsDiv').style.visibility = 'hidden';";
                    ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult setVisibilityCallbackResult =
                        ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult.CreateJavaScript(jsSetDivVisibility);
                    adfMap.CallbackResults.Add(setVisibilityCallbackResult);
                }
            }
            catch (System.Exception exception)
            {
                ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult errorAlertCallbackResult =
                    Utility.CreateErrorCallbackResult(exception);
                adfMap.CallbackResults.Add(errorAlertCallbackResult);
            }
        }
    }