ArcGIS Add dynamic data
ArcGIS_AddDynamicData_VBNet\MoveLayer_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 the move an existing layer in a pooled ArcGIS Server map service
Partial Public Class MoveLayer_ADF
    Inherits System.Web.UI.Page
#Region "Instance Variable Declarations"

    Private _serverObjectStateModifier As ServerObjectStateModifier = Nothing

    ' Specify the name of the resource containing the layer to move
    Private _targetResourceName As String = "MapResourceItem0"
    ' Specify the name of the layer to move
    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 moving the layer
        _serverObjectStateModifier = New ServerObjectStateModifier()
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
        Try
            ' Register the layer up and down buttons so they initiate asynchronous
            ' postbacks when clicked
            ScriptManager1.RegisterAsyncPostBackControl(LayerUpButton)
            ScriptManager1.RegisterAsyncPostBackControl(LayerDownButton)

            ' If the target layer has been moved (indicated by the session variable storing the moved layer's current index
            ' being populated), then we need to move it again to the last index at which it was located so that the Web ADF 
            ' components accessing the map resource are aware of the correct layer position.  
            If Session("currentLayerIndex") IsNot Nothing Then
                ' Get the layer's target resource and call the method to move the target layer to its current index, as stored
                ' in session
                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)

                _serverObjectStateModifier.MoveLayer(mapResourceLocal, _targetLayerName, CInt(Fix(Session("currentLayerIndex"))))
            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 moved (indicated by the session variable storing the layer's initial position), then we 
        ' need to move it back to its original position in the map resource before the server context created as a result of 
        ' the current page request is released.  Otherwise, the target layer will be in the altered position outside the scope
        ' of this page request, so clients accessing the map service will see the layer in the new position
        If Session("originalLayerIndex") IsNot Nothing Then
            ' Get the layer's target resource and call the method to move the layer back to its original position
            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)

            _serverObjectStateModifier.MoveLayer(mapResourceLocal, _targetLayerName, CInt(Fix(Session("originalLayerIndex"))))
        End If
    End Sub

#End Region

#Region "ASP.NET Web Control Event Handlers"

    Public Sub LayerUpButton_Click1(ByVal sender As Object, ByVal e As System.EventArgs) Handles LayerUpButton.Click
        Try
            ' Get the resource containing the layer to be moved
            Dim mapResourceLocal As ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.MapResourceLocal = GetResource()

            Dim currentLayerIndex As Integer = GetCurrentlayerIndex(mapResourceLocal)

            ' Call the method to move the layer, passing in a layer index one less than the current one, which will move the
            ' layer up one position.
            currentLayerIndex = _serverObjectStateModifier.MoveLayer(mapResourceLocal, _targetLayerName, currentLayerIndex - 1)
            If currentLayerIndex <> -1 Then
                ' If a non-negative layer index was returned then the layer was moved successfully.  Update the current layer
                ' index session variable and refresh the Map and Toc so they reflect the layer's new position.
                Session("currentLayerIndex") = currentLayerIndex
                RefreshMapAndToc()
            End If
        Catch exception As System.Exception
            ProcessError(exception)
        End Try
    End Sub

    Public Sub LayerDownButton_Click1(ByVal sender As Object, ByVal e As System.EventArgs)
        Try
            ' Get the resource containing the layer to be moved
            Dim mapResourceLocal As ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.MapResourceLocal = GetResource()

            Dim currentLayerIndex As Integer = GetCurrentlayerIndex(mapResourceLocal)

            ' Call the method to move the layer, passing in a layer index one more than the current one, which will move the
            ' layer up one position.
            currentLayerIndex = _serverObjectStateModifier.MoveLayer(mapResourceLocal, _targetLayerName, currentLayerIndex + 1)
            If currentLayerIndex <> -1 Then
                ' If a non-negative layer index was returned then the layer was moved successfully.  Update the current layer
                ' index session variable and refresh the Map and Toc so they reflect the layer's new position.
                Session("currentLayerIndex") = currentLayerIndex
                RefreshMapAndToc()
            End If
        Catch exception As System.Exception
            ProcessError(exception)
        End Try
    End Sub

#End Region

#Region "Instance Methods"

    ' Retrieves the index of the layer indicated by the passed-in name from the passed-in resource
    Private Function GetLayerIndex(ByVal mapResourceLocal As ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.MapResourceLocal, ByVal layerName As String) As Integer
        ' Get the map underlying the passed-in map resource
        Dim mapServerObjects As ESRI.ArcGIS.Carto.IMapServerObjects = CType(mapResourceLocal.MapServer, ESRI.ArcGIS.Carto.IMapServerObjects)
        Dim aoMap As ESRI.ArcGIS.Carto.IMap = mapServerObjects.Map(mapResourceLocal.DataFrame)

        ' Get a reference to the layer with the passed-in name from the map
        Dim enumLayer As ESRI.ArcGIS.Carto.IEnumLayer = aoMap.Layers(Nothing, True)
        Dim currentLayer As ESRI.ArcGIS.Carto.ILayer = Nothing
        Dim layerIndex As Integer = 0
        currentLayer = enumLayer.Next()
        Do While currentLayer IsNot Nothing
            If currentLayer.Name = layerName Then
                Exit Do
            End If

            layerIndex += 1
            currentLayer = enumLayer.Next()
        Loop

        Return layerIndex
    End Function

    Private Function GetCurrentlayerIndex(ByVal mapResourceLocal As ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.MapResourceLocal) As Integer
        ' Get the current index of the layer.  If the session variable storing the layer's original index has not
        ' been populated, populate it with the current index.  This will be used to restore the layer to its
        ' original position in the map service.
        Dim currentLayerIndex As Integer
        If Session("originalLayerIndex") Is Nothing Then
            currentLayerIndex = GetLayerIndex(mapResourceLocal, _targetLayerName)
            Session("originalLayerIndex") = currentLayerIndex
            Session("currentLayerIndex") = currentLayerIndex
        Else
            currentLayerIndex = CInt(Fix(Session("currentLayerIndex")))
        End If

        Return currentLayerIndex
    End Function

    ' 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

    Private Function GetResource() As ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.MapResourceLocal
        Dim agsMapFunctionality As ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.MapFunctionality = CType(Map1.GetFunctionality(_targetResourceName), ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.MapFunctionality)
        Return TryCast(agsMapFunctionality.Resource, ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.MapResourceLocal)
    End Function

    Private Sub ProcessError(ByVal 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 Sub

#End Region

  
  
End Class