Common_Callback_CSharp\App_Code\CustomTool.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 CustomTool : ESRI.ArcGIS.ADF.Web.UI.WebControls.Tools.IMapServerToolAction { #region IMapServerToolAction Members void ESRI.ArcGIS.ADF.Web.UI.WebControls.Tools.IMapServerToolAction.ServerAction( ESRI.ArcGIS.ADF.Web.UI.WebControls.ToolEventArgs toolEventArgs) { // Get a reference to the Web ADF Map Control that was clicked via the tool event // arguments ESRI.ArcGIS.ADF.Web.UI.WebControls.Map adfMap = (ESRI.ArcGIS.ADF.Web.UI.WebControls.Map)toolEventArgs.Control; try { // Cast the tool event arguments to a MapPointEventArgs object, which allows direct // access to the map point clicked ESRI.ArcGIS.ADF.Web.UI.WebControls.MapPointEventArgs mapPointEventArgs = (ESRI.ArcGIS.ADF.Web.UI.WebControls.MapPointEventArgs)toolEventArgs; // 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 (System.Exception exception) { ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult errorCallbackResult = ErrorHandling.GetErrorCallback(exception); adfMap.CallbackResults.Add(errorCallbackResult); } } #endregion } public class ErrorHandling { // 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; } }