ArcGIS_AddDynamicData_CSharp\RemoveLayer_ADF.aspx.cs
// 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. // // Illustrates how to remove an existing layer in a pooled ArcGIS Server map service using Web ADF components // and ArcObjects. For illustration of how to add and remove a dynamic layer from a map service, see the // Default_ADF and CustomMapHandler_ADF pages. Note that this code bypasses use of the custom map handler (and // the accompanying increase in map performance) by setting the Map1.EnableMapHandler property to false. public partial class RemoveLayer_ADF : System.Web.UI.Page { #region Instance Variable Declarations private ServerObjectStateModifier _serverObjectStateModifier = null; private System.Collections.Generic.Dictionary<int, ESRI.ArcGIS.Carto.ILayer> _removedLayerDictionary = null; // Specify the name of the resource containing the layer to remove private string _targetResourceName = "MapResourceItem0"; // Specify the name of the layer to remove private string _targetLayerName = "Highways"; #endregion #region ASP.NET Page Life Cycle Event Handlers protected void Page_PreInit(object sender, System.EventArgs e) { // Instantiate a class-level ServerObjectStateModifier to use in removing and re-adding the layer _serverObjectStateModifier = new ServerObjectStateModifier(); } protected void Page_Load(object sender, System.EventArgs e) { try { // Register the remover layer button so it initiates an asynchronous // postback when clicked ScriptManager1.RegisterAsyncPostBackControl(RemoveLayerButton); // If the target layer has been removed (indicated by the session variable), then we need to re-remove it // so that the Web ADF components accessing the changed map resource is aware of the change. if ((Session["LayerRemoved"] != null) && ((bool)Session["LayerRemoved"])) { // Get the layer's target resource and call the method to remove the layer from it ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.MapFunctionality agsMapFunctionality = (ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.MapFunctionality)Map1.GetFunctionality(_targetResourceName); ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.MapResourceLocal mapResourceLocal = (ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.MapResourceLocal)agsMapFunctionality.Resource; _removedLayerDictionary = _serverObjectStateModifier.RemoveLayer(mapResourceLocal, _targetLayerName); } MapResourceManager1.ResourcesDispose += new System.EventHandler(MapResourceManager1_ResourcesDispose); } catch (System.Exception exception) { // Check whether the page is loading asynchronously if (ScriptManager1.IsInAsyncPostBack) { // Get a callback result that will show an alert with information pertaining to the exception ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult errorCallbackResult = Utility.GetErrorCallback(exception); // Get the control that initiated the postback System.Web.UI.Control control = Utility.GetPostBackControl(Page); // If the control is a Web ADF Control (i.e. WebCotnrol or CompositeControl), add the error // callback to that control's callback results. Otherwise, add the error callback to the // callback results collection member variable, which will be passed to the client in // GetCallbackResult. if (control is ESRI.ArcGIS.ADF.Web.UI.WebControls.WebControl) { ESRI.ArcGIS.ADF.Web.UI.WebControls.WebControl adfWebControl = control as ESRI.ArcGIS.ADF.Web.UI.WebControls.WebControl; adfWebControl.CallbackResults.Add(errorCallbackResult); } else if (control is ESRI.ArcGIS.ADF.Web.UI.WebControls.CompositeControl) { ESRI.ArcGIS.ADF.Web.UI.WebControls.CompositeControl adfCompositeControl = control as ESRI.ArcGIS.ADF.Web.UI.WebControls.CompositeControl; adfCompositeControl.CallbackResults.Add(errorCallbackResult); } else { ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResultCollection callbackResults = new ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResultCollection(); callbackResults.Add(errorCallbackResult); ScriptManager1.RegisterDataItem(Page, callbackResults.ToString(), false); } } else { // Since the page is in full postback, write the javascript alert code directly to the response string jsErrorAlert = string.Format("<script>{0}</script>", Utility.GetJavaScriptErrorString(exception)); Response.Write(jsErrorAlert); } } } #endregion #region Web ADF Control Event Handlers void MapResourceManager1_ResourcesDispose(object sender, System.EventArgs e) { // If the target layer has been removed (indicated by the session variable), then we need to add it back to the resource // before the server context created as a result of the current page request is released. Otherwise, the target layer // will be missing from the map service outside the scope of this request, so other clients using the service will not have // access to it. if ((Session["LayerRemoved"] != null) && ((bool)Session["LayerRemoved"])) { // Get the resource from which the layer was removed ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.MapFunctionality agsMapFunctionality = (ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.MapFunctionality)Map1.GetFunctionality(_targetResourceName); ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.MapResourceLocal mapResourceLocal = (ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.MapResourceLocal)agsMapFunctionality.Resource; // Retrieve a reference to the missing layer and its original layer index from the class-level dictionary that // stored this information when the layer was removed System.Collections.Generic.Dictionary<int, ESRI.ArcGIS.Carto.ILayer>.Enumerator removedLayerEnumerator = _removedLayerDictionary.GetEnumerator(); removedLayerEnumerator.MoveNext(); ESRI.ArcGIS.Carto.IGeoFeatureLayer removedGeoFeatureLayer = removedLayerEnumerator.Current.Value as ESRI.ArcGIS.Carto.IGeoFeatureLayer; int layerIndex = removedLayerEnumerator.Current.Key; // Add the layer back to the map service _serverObjectStateModifier.AddLayer(mapResourceLocal, removedGeoFeatureLayer, layerIndex); } } #endregion #region ASP.NET Web Control Event Handlers public void RemoveLayerButton_Click(object sender, System.EventArgs e) { try { Session["LayerRemoved"] = true; // Get the layer's target resource and call the method to remove the layer from it ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.MapFunctionality agsMapFunctionality = (ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.MapFunctionality)Map1.GetFunctionality(_targetResourceName); ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.MapResourceLocal mapResourceLocal = (ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.MapResourceLocal)agsMapFunctionality.Resource; // Store a reference to the dictionary containing the layer and its index in the map service _removedLayerDictionary = _serverObjectStateModifier.RemoveLayer(mapResourceLocal, _targetLayerName); // Refresh the Map and Toc so they reflect the layer's removal RefreshMapAndToc(); } catch (System.Exception exception) { // Get a callback result that will alert the user of the error and add it to the callback results collection ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult errorCallbackResult = Utility.GetErrorCallback(exception); ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResultCollection callbackResults = new ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResultCollection(); callbackResults.Add(errorCallbackResult); ScriptManager1.RegisterDataItem(Page, callbackResults.ToString(), false); } } #endregion #region Instance Methods // Refreshes the Map and Toc controls and populates the callback results member variable with the // resulting callback results private void RefreshMapAndToc() { Toc1.Refresh(); Map1.CallbackResults.CopyFrom(Toc1.CallbackResults); Map1.RefreshResource(_targetResourceName); ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResultCollection callbackResults = new ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResultCollection(); callbackResults.CopyFrom(Map1.CallbackResults); ScriptManager1.RegisterDataItem(Page, callbackResults.ToString(), false); } #endregion }