Common Select Buffer tool
Common_SelectBufferTool_VBNet\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
Namespace CustomComponents
  Public Class Utility
    ' Constructs a callback result that will display an error message based on the passed-in
    ' exception
    Public Shared Function GetErrorCallback(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

    ' Constructs JavaScript necessary to display an error message based on the passed-in exception.
    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("\", "\\")
        errorMessage = exception.Message & "\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

    ' Searches the passed-in control and all its child controls, returning that having the passed-in
    ' control ID, if found
    Public Shared Function FindControl(ByVal rootControl As System.Web.UI.Control, ByVal controlID As String) As System.Web.UI.Control
      ' Return the passed-in control if it has the passed-in ID
      If rootControl.ID = controlID Then
        Return rootControl
      End If

      ' 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.
      For Each childControl As System.Web.UI.Control In rootControl.Controls
        Dim foundControl As System.Web.UI.Control = FindControl(childControl, controlID)
        If Not foundControl Is Nothing Then
          Return foundControl
        End If
      Next childControl

      Return Nothing
    End Function

    ' Finds the first node with the specified text in the specified Toc Control.
    Public Shared Function FindNode(ByVal adfToc As ESRI.ArcGIS.ADF.Web.UI.WebControls.Toc, ByVal nodeText As String) As ESRI.ArcGIS.ADF.Web.UI.WebControls.TreeViewPlusNode
      ' 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.
      For Each tocTreeViewPlusNode As ESRI.ArcGIS.ADF.Web.UI.WebControls.TreeViewPlusNode In adfToc.Nodes
        Dim matchingTreeViewPlusNode As ESRI.ArcGIS.ADF.Web.UI.WebControls.TreeViewPlusNode = FindNodeRecursive(tocTreeViewPlusNode, nodeText)
        If Not matchingTreeViewPlusNode Is Nothing Then
          Return matchingTreeViewPlusNode
        End If
      Next tocTreeViewPlusNode

      Return Nothing
    End Function

    ' 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.
    Friend Shared Function FindNodeRecursive(ByVal treeViewPlusNode As ESRI.ArcGIS.ADF.Web.UI.WebControls.TreeViewPlusNode, ByVal nodeName As String) As ESRI.ArcGIS.ADF.Web.UI.WebControls.TreeViewPlusNode
      ' Check whether the text of the passed-in node matches the text sought.  Return the node if so.
      If treeViewPlusNode.Text = nodeName Then
        Return treeViewPlusNode
      End If

      ' Iterate through the passed-in node's child nodes, calling this function on each.
      For Each childTreeViewPlusNode As ESRI.ArcGIS.ADF.Web.UI.WebControls.TreeViewPlusNode In treeViewPlusNode.Nodes
        Dim childNode As ESRI.ArcGIS.ADF.Web.UI.WebControls.TreeViewPlusNode = FindNodeRecursive(childTreeViewPlusNode, nodeName)
        If Not childNode Is Nothing Then
          Return childNode
        End If
      Next childTreeViewPlusNode

      ' If the code reaches this point, no match was found. 
      Return Nothing
    End Function
  End Class
End Namespace