Common_Callback_VBNet\App_Code\CustomTool.vb
' 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. ' Imports Microsoft.VisualBasic Imports System Public Class CustomTool Implements ESRI.ArcGIS.ADF.Web.UI.WebControls.Tools.IMapServerToolAction #Region "IMapServerToolAction Members" Private Sub ServerAction(ByVal toolEventArgs As ESRI.ArcGIS.ADF.Web.UI.WebControls.ToolEventArgs) Implements ESRI.ArcGIS.ADF.Web.UI.WebControls.Tools.IMapServerToolAction.ServerAction ' Get a reference to the Web ADF Map Control that was clicked via the tool event ' arguments Dim adfMap As ESRI.ArcGIS.ADF.Web.UI.WebControls.Map = CType(toolEventArgs.Control, ESRI.ArcGIS.ADF.Web.UI.WebControls.Map) Try ' Cast the tool event arguments to a MapPointEventArgs object, which allows direct ' access to the map point clicked Dim mapPointEventArgs As ESRI.ArcGIS.ADF.Web.UI.WebControls.MapPointEventArgs = CType(toolEventArgs, ESRI.ArcGIS.ADF.Web.UI.WebControls.MapPointEventArgs) ' Center the map at the map point clicked. Note that initiating the callback and ' handling callback results is done automatically, since we are inheriting from ' IMapServerToolAction. Clicking the map initiates the callback, which in turn ' throws to an ICallbackEventHandler implementation embedded in the Web ADF logic. ' This then fires IMapServerToolAction.ServerAction, which we are implementing here. ' The Web ADF callback implementation then automatically returns any callback results ' on the map control to the client. adfMap.CenterAt(mapPointEventArgs.MapPoint) Catch exception As System.Exception Dim errorCallbackResult As ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult = ErrorHandling.GetErrorCallback(exception) adfMap.CallbackResults.Add(errorCallbackResult) End Try End Sub #End Region End Class Public Class ErrorHandling ' 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 End Class