ArcGIS Add dynamic data
ArcGIS_AddDynamicData_VBNet\RemoveLayer_ADF.aspx.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.
' 

' 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.
Partial Public Class RemoveLayer_ADF
    Inherits System.Web.UI.Page
#Region "Instance Variable Declarations"

    Private _serverObjectStateModifier As ServerObjectStateModifier = Nothing
    Private _removedLayerDictionary As System.Collections.Generic.Dictionary(Of Integer, ESRI.ArcGIS.Carto.ILayer) = Nothing

    ' Specify the name of the resource containing the layer to remove
    Private _targetResourceName As String = "MapResourceItem0"
    ' Specify the name of the layer to remove
    Private _targetLayerName As String = "Highways"

#End Region

#Region "ASP.NET Page Life Cycle Event Handlers"

    Protected Sub Page_PreInit(ByVal sender As Object, ByVal e As System.EventArgs)
        ' Instantiate a class-level ServerObjectStateModifier to use in removing and re-adding the layer
        _serverObjectStateModifier = New ServerObjectStateModifier()
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
        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") IsNot Nothing) AndAlso (CBool(Session("LayerRemoved"))) Then
                ' Get the layer's target resource and call the method to remove the layer from it
                Dim agsMapFunctionality As ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.MapFunctionality = CType(Map1.GetFunctionality(_targetResourceName), ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.MapFunctionality)
                Dim mapResourceLocal As ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.MapResourceLocal = CType(agsMapFunctionality.Resource, ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.MapResourceLocal)
                _removedLayerDictionary = _serverObjectStateModifier.RemoveLayer(mapResourceLocal, _targetLayerName)
            End If

            AddHandler MapResourceManager1.ResourcesDispose, AddressOf MapResourceManager1_ResourcesDispose
        Catch exception As System.Exception
            ' Check whether the page is loading asynchronously
            If ScriptManager1.IsInAsyncPostBack Then
                ' Get a callback result that will show an alert with information pertaining to the exception
                Dim errorCallbackResult As ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult = Utility.GetErrorCallback(exception)
                ' Get the control that initiated the postback
                Dim control As System.Web.UI.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 TypeOf control Is ESRI.ArcGIS.ADF.Web.UI.WebControls.WebControl Then
                    Dim adfWebControl As ESRI.ArcGIS.ADF.Web.UI.WebControls.WebControl = TryCast(control, ESRI.ArcGIS.ADF.Web.UI.WebControls.WebControl)
                    adfWebControl.CallbackResults.Add(errorCallbackResult)
                ElseIf TypeOf control Is ESRI.ArcGIS.ADF.Web.UI.WebControls.CompositeControl Then
                    Dim adfCompositeControl As ESRI.ArcGIS.ADF.Web.UI.WebControls.CompositeControl = TryCast(control, ESRI.ArcGIS.ADF.Web.UI.WebControls.CompositeControl)
                    adfCompositeControl.CallbackResults.Add(errorCallbackResult)
                Else
                    Dim callbackResults As New ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResultCollection()
                    callbackResults.Add(errorCallbackResult)
                    ScriptManager1.RegisterDataItem(Page, callbackResults.ToString(), False)
                End If
            Else
                ' Since the page is in full postback, write the javascript alert code directly to the response
                Dim jsErrorAlert As String = String.Format("<script>{0}</script>", Utility.GetJavaScriptErrorString(exception))
                Response.Write(jsErrorAlert)
            End If
        End Try
    End Sub

#End Region

#Region "Web ADF Control Event Handlers"

    Private Sub MapResourceManager1_ResourcesDispose(ByVal sender As Object, ByVal e As System.EventArgs)
        ' 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") IsNot Nothing) AndAlso (CBool(Session("LayerRemoved"))) Then
            ' Get the resource from which the layer was removed
            Dim agsMapFunctionality As ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.MapFunctionality = CType(Map1.GetFunctionality(_targetResourceName), ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.MapFunctionality)
            Dim mapResourceLocal As ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.MapResourceLocal = CType(agsMapFunctionality.Resource, ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.MapResourceLocal)

            ' 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
            Dim removedLayerEnumerator As System.Collections.Generic.Dictionary(Of Integer, ESRI.ArcGIS.Carto.ILayer).Enumerator = _removedLayerDictionary.GetEnumerator()
            removedLayerEnumerator.MoveNext()
            Dim removedGeoFeatureLayer As ESRI.ArcGIS.Carto.IGeoFeatureLayer = TryCast(removedLayerEnumerator.Current.Value, ESRI.ArcGIS.Carto.IGeoFeatureLayer)
            Dim layerIndex As Integer = removedLayerEnumerator.Current.Key

            ' Add the layer back to the map service
            _serverObjectStateModifier.AddLayer(mapResourceLocal, removedGeoFeatureLayer, layerIndex)
        End If
    End Sub

#End Region

#Region "ASP.NET Web Control Event Handlers"

    Public Sub RemoveLayerButton_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Try
            Session("LayerRemoved") = True

            ' Get the layer's target resource and call the method to remove the layer from it
            Dim agsMapFunctionality As ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.MapFunctionality = CType(Map1.GetFunctionality(_targetResourceName), ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.MapFunctionality)
            Dim mapResourceLocal As ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.MapResourceLocal = CType(agsMapFunctionality.Resource, ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.MapResourceLocal)

            ' 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 exception As System.Exception
            ' Get a callback result that will alert the user of the error and add it to the callback results collection
            Dim errorCallbackResult As ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult = Utility.GetErrorCallback(exception)
            Dim callbackResults As New ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResultCollection()
            callbackResults.Add(errorCallbackResult)
            ScriptManager1.RegisterDataItem(Page, callbackResults.ToString(), False)
        End Try
    End Sub

#End Region

#Region "Instance Methods"

    ' Refreshes the Map and Toc controls and populates the callback results member variable with the
    ' resulting callback results 
    Private Sub RefreshMapAndToc()
        Toc1.Refresh()
        Map1.CallbackResults.CopyFrom(Toc1.CallbackResults)

        Map1.RefreshResource(_targetResourceName)
        Dim callbackResults As New ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResultCollection()
        callbackResults.CopyFrom(Map1.CallbackResults)
        ScriptManager1.RegisterDataItem(Page, callbackResults.ToString(), False)
    End Sub

#End Region
End Class