ArcGIS Simple Edit service
ArcGIS_SimpleEdit_VBNet\ArcGIS_SimpleEdit_WebAppVBNet\App_Code\Utility.vb
' 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.
' 

Imports Microsoft.VisualBasic
Imports System
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 Shared Function CreateErrorCallbackResult(ByVal exception As System.Exception) As ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult
    ' Create a callback result to display an error message
    Dim jsAlertErrorMessage As String = GetJavaScriptErrorString(exception)
    Dim alertCallbackResult As ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult = ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult.CreateJavaScript(jsAlertErrorMessage)
    Return alertCallbackResult
  End Function

  ''' <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 Shared Function GetJavaScriptErrorString(ByVal exception As System.Exception) As String
    ' Get the website's configuration file
    Dim webConfig As System.Configuration.Configuration = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(System.Web.HttpContext.Current.Request.ApplicationPath)

    ' Get the "compilation" section of the config file
    Dim compilationSection As System.Web.Configuration.CompilationSection = TryCast(webConfig.GetSection("system.web/compilation"), 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.
    Dim errorMessage As String = Nothing
    If (Not compilationSection Is Nothing) AndAlso (compilationSection.Debug) Then
      Dim stackTrace As String = exception.StackTrace.Replace("\", "\\")
      stackTrace = stackTrace.Replace(Constants.vbLf, "\n")
      stackTrace = stackTrace.Replace(Constants.vbCr, "\r")
      stackTrace = stackTrace.Replace("'", "\'")
      errorMessage = exception.Message.Replace("\", "\\")
      errorMessage = errorMessage.Replace(Constants.vbLf, "\n")
      errorMessage = errorMessage.Replace(Constants.vbCr, "\r")
      errorMessage = errorMessage.Replace("'", "\'")

      errorMessage = errorMessage & "\n\n" & stackTrace.Trim()
    Else
      errorMessage = exception.Message
    End If

    ' Create a callback result to display an error message
    Dim jsAlertException As String = "alert('" & errorMessage & "')"
    Return jsAlertException
  End Function


    Public Shared Sub DisplayOrHideSelectionTable(ByVal adfMap As ESRI.ArcGIS.ADF.Web.UI.WebControls.Map, ByVal resultsGraphicsLayer As ESRI.ArcGIS.ADF.Web.Display.Graphics.GraphicsLayer, ByVal showTable As Boolean)
      Try

        ' Get the GridView control that will be used to display selection results
        Dim gridViewResults As System.Web.UI.WebControls.GridView = CType(adfMap.Page.FindControl("grdIdentifyResults"), System.Web.UI.WebControls.GridView)

        Dim resultsDataTable As System.Data.DataTable = resultsGraphicsLayer

        ' Check whether to show selection results tabularly
        If showTable Then
          ' Make sure results were actually found
          If resultsDataTable.Rows.Count > 0 Then
            ' Load the graphicsLayer into a new data table and remove the IS_SELECTED column
            Dim newDataTable As System.Data.DataTable = 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
            Dim gridViewHTMLString As String = Nothing
            Dim stringWriter As System.IO.StringWriter = New System.IO.StringWriter()
            Dim htmlTextWriter As System.Web.UI.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.
            Dim gridViewCallbackResult As ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult = 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.
            Dim noFeaturesFoundCallbackResult As ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult = ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult.CreateSetInnerContent("identifyResultsDiv", "No selected features")
            adfMap.CallbackResults.Add(noFeaturesFoundCallbackResult)
          End If

          ' Create a JavaScript callback result to make the div containing the GridView control
          ' visible
          Dim jsSetDivVisibility As String = "document.getElementById('identifyResultsDiv').style.visibility = 'visible';"
          Dim setVisibilityCallbackResult As ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult = ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult.CreateJavaScript(jsSetDivVisibility)
          adfMap.CallbackResults.Add(setVisibilityCallbackResult)
        Else
          Dim jsSetDivVisibility As String = "document.getElementById('identifyResultsDiv').style.visibility = 'hidden';"
          Dim setVisibilityCallbackResult As ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult = ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult.CreateJavaScript(jsSetDivVisibility)
          adfMap.CallbackResults.Add(setVisibilityCallbackResult)
        End If
      Catch exception As System.Exception
        Dim errorAlertCallbackResult As ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult = Utility.CreateErrorCallbackResult(exception)
        adfMap.CallbackResults.Add(errorAlertCallbackResult)
      End Try
    End Sub
End Class