ArcGIS ClipShip geoprocessing
ArcGIS_ClipShip_Geoprocessing_VBNet\Default.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.
' 

Partial Public Class _Default
    Inherits System.Web.UI.Page
#Region "Instance Variable Declarations"
    Private _callbackResultCollection As New ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResultCollection()

    ' Names of the layers that will hold the clipped features and user-drawn polylines
    Private _clippedFeaturesLayerName As String = "Clip"
    Private _polylineGraphicsLayerName As String = "Polylines"

    ' Name of the geoproccessing resource containing the ClipCounties task
    Private _geoprocessingResourceName As String = "Buffer and Clip Resource"

    ' Name of the resource that will store clip results if _displayResultsInMapResource is
    ' set to true
    Private _resultsMapResourceName As String = "Clip Results"

    ' Name of the resource in which to store geoprocessing results (if 
    ' _displayResultsInMapResource is false) and user-drawn polylines
    Private _graphicsResourceName As String = "Web ADF Graphics"

    ' Determines whether to display the geoprocessing results in a map resource or as graphics
    Private _displayResultsInMapResource As Boolean = True

    ' Name of the GP tool layer in the map service.  This is the layer that was created in the
    ' underlying mxd by dragging and dropping the GP tool into the mxd.  This name will be used
    ' to remove the layer from the Toc.
    Private _toolLayerName As String = "ClipCounties"

#End Region

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

    Protected Sub Page_PreRender(ByVal sender As Object, ByVal eventArgs As System.EventArgs)
        Try
            ' Initialize session variable only if the page request is occurring at the beginning of a session
            If Page.Session.IsNewSession Then
                Session("graphicsResourceName") = _graphicsResourceName
                Session("polylineGraphicsLayerName") = _polylineGraphicsLayerName
            End If

            ' Make sure control initialization only occurs during initial page load or on page refresh
            If (Not ScriptManager1.IsInAsyncPostBack) Then
                ' Add all the ArcGIS Server unit types to the units drop-down list except for unknown, points, 
                ' and decimal degress
                Dim agsUnitTypes As System.Array = System.Enum.GetValues(GetType(ESRI.ArcGIS.ADF.Web.DataSources.Units))
                For Each agsUnitType As Integer In agsUnitTypes
                    Dim unit As String = agsUnitTypes.GetValue(agsUnitType).ToString()
                    If (unit <> "Unknown") AndAlso (unit <> "DecimalDegrees") AndAlso (unit <> "Points") Then
                        unitsDropDownList.Items.Add(unit)
                    End If
                Next agsUnitType
            End If
        Catch exception As System.Exception
            Dim jsErrorAlertString As String = String.Format("<script>{0}</script>", Utility.GetJavaScriptErrorString(exception))
            Response.Write(jsErrorAlertString)
        End Try
    End Sub

    ' Generates custom callback function string for non-ADF controls\elements in page that generate callbacks
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
        ' Register the buttons so they initiate asynchronous postbacks 
        ScriptManager1.RegisterAsyncPostBackControl(bufferButton)
        ScriptManager1.RegisterAsyncPostBackControl(ClearGraphicsButton)

        RemoveLayerFromToc(_toolLayerName)

        AddHandler bufferButton.Click, AddressOf bufferButton_Click
        AddHandler ClearGraphicsButton.Click, AddressOf ClearGraphicsButton_Click

        AddHandler PostbackManager1.RequestReceived, AddressOf PostbackManager1_RequestReceived

    End Sub

#End Region

#Region "Request Handling"

    Private Sub PostbackManager1_RequestReceived(ByVal sender As Object, ByVal args As PostbackManager_VBNet.AdfRequestEventArgs)
        ' Use the Web ADF's callback utility to parse the arguments into a NameValueCollection
        Dim requestArgs As System.Collections.Specialized.NameValueCollection = ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackUtility.ParseStringIntoNameValueCollection(args.RequestArguments)

        If requestArgs("EventArg") = "CheckGeoprocessingJobStatus" Then
            Try
                ' Call the method to heck whether the geoprocssing job is finished and process results if so
                Dim downloadoutput As Boolean
                CheckGeoprocessingJobStatus(requestArgs("JobID"), requestArgs("TaskName"), requestArgs("OutputParameters"), Boolean.TryParse(requestArgs("DownloadOutput"), downloadoutput))
            Catch exception As System.Exception
                Dim errorCallbackResult As ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult = Utility.CreateErrorCallbackResult(exception)
                _callbackResultCollection.Add(errorCallbackResult)
            Finally
                PostbackManager1.CallbackResults.CopyFrom(_callbackResultCollection)
            End Try
        End If
    End Sub

#End Region

#Region "ASP.NET Web Control Event Handlers"

    Protected Sub bufferButton_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Try
            ' Get the buffer operation parameters specified by the user
            Dim bufferDistanceString As String = bufferDistanceTextBox.Text
            Dim bufferDistance As Double = 0
            Double.TryParse(bufferDistanceString, bufferDistance)

            Dim downloadOutput As Boolean = downloadCheckBox.Checked
            ' Invoke the method to initiate the geoprocessing task
            StartBufferAndClipJob(bufferDistance, unitsDropDownList.SelectedValue, downloadOutput)

        Catch exception As System.Exception
            Dim errorCallbackResult As ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult = Utility.CreateErrorCallbackResult(exception)
            _callbackResultCollection.Add(errorCallbackResult)
        Finally
            ScriptManager1.RegisterDataItem(Page, _callbackResultCollection.ToString(), False)
        End Try
    End Sub

    Protected Sub ClearGraphicsButton_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Try
            ' Invoke the method to clear the graphics from the map
            ClearGraphics()
        Catch exception As System.Exception
            Dim errorCallbackResult As ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult = Utility.CreateErrorCallbackResult(exception)
            _callbackResultCollection.Add(errorCallbackResult)
        Finally
            ScriptManager1.RegisterDataItem(Page, _callbackResultCollection.ToString(), False)
        End Try
    End Sub
#End Region

#Region "Instance Methods"

    ' Initiates the buffer and clip geoprocessing job with the passed-in parameters
    Private Sub StartBufferAndClipJob(ByVal bufferDistance As Double, ByVal units As String, ByVal downloadOutput As Boolean)
        '    #Region "Initialize resources and functionalities"

        ' Initialize GP resource and functionality
        Dim geoprocessingResourceItem As ESRI.ArcGIS.ADF.Web.UI.WebControls.GeoprocessingResourceItem = GeoprocessingResourceManager1.ResourceItems.Find(_geoprocessingResourceName)

        ' Make sure the geoprocessing resource is initialized
        If (Not geoprocessingResourceItem.Resource.Initialized) Then
            geoprocessingResourceItem.InitializeResource()
        End If

        ' Get a reference to the GP resource and its functionality
        Dim geoprocessingResource As ESRI.ArcGIS.ADF.Web.DataSources.IGeoprocessingResource = CType(geoprocessingResourceItem.Resource, ESRI.ArcGIS.ADF.Web.DataSources.IGeoprocessingResource)

        Dim commonGeoprocessingFunctionality As ESRI.ArcGIS.ADF.Web.DataSources.IGeoprocessingFunctionality = CType(geoprocessingResource.CreateFunctionality(GetType(ESRI.ArcGIS.ADF.Web.DataSources.IGeoprocessingFunctionality), Nothing), ESRI.ArcGIS.ADF.Web.DataSources.IGeoprocessingFunctionality)

        Dim agsGeoprocessingFunctionality As ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.GeoprocessingFunctionality = TryCast(commonGeoprocessingFunctionality, ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.GeoprocessingFunctionality)

        ' Make sure the geoprocessing functionality is initialized
        If (Not agsGeoprocessingFunctionality.Initialized) Then
            agsGeoprocessingFunctionality.Initialize()
        End If

        '    #End Region

        '    #Region "Prepare inputs and start the GP task"

        ' Set the name of the geoprocessing (GP) task
        Dim GPTaskName As String = "ClipCounties"

        ' Get an array of the GP task's parameters
        Dim adfGPToolInfo As ESRI.ArcGIS.ADF.Web.DataSources.GPToolInfo = agsGeoprocessingFunctionality.GetTask(GPTaskName)
        Dim adfGPParamterInfoArray() As ESRI.ArcGIS.ADF.Web.DataSources.GPParameterInfo = adfGPToolInfo.ParameterInfo

        ' Get a reference to the first input parameter (a feature set) as a Web ADF feature graphics layer
        Dim adfGPFeatureGraphicsLayer As ESRI.ArcGIS.ADF.Web.DataSources.GPFeatureGraphicsLayer = CType(adfGPParamterInfoArray(0).Value, ESRI.ArcGIS.ADF.Web.DataSources.GPFeatureGraphicsLayer)
        Dim featureGraphicsLayer As ESRI.ArcGIS.ADF.Web.Display.Graphics.FeatureGraphicsLayer = adfGPFeatureGraphicsLayer.Layer

        ' Get the graphics resource containing the layer that holds the user-drawn lines
        Dim adfGraphicsMapResource As ESRI.ArcGIS.ADF.Web.DataSources.Graphics.MapResource = TryCast(Map1.GetFunctionality(_graphicsResourceName).Resource, ESRI.ArcGIS.ADF.Web.DataSources.Graphics.MapResource)

        ' Get the graphics layer containing the user-drawn lines
        Dim polylineElementGraphicsLayer As ESRI.ArcGIS.ADF.Web.Display.Graphics.ElementGraphicsLayer = TryCast(adfGraphicsMapResource.Graphics.Tables(_polylineGraphicsLayerName), ESRI.ArcGIS.ADF.Web.Display.Graphics.ElementGraphicsLayer)

        ' If there are no lines in the layer, alert the user and exit the function
        If (polylineElementGraphicsLayer Is Nothing) OrElse (polylineElementGraphicsLayer.Rows.Count < 1) Then
            Dim noLinesAlertCallbackResult As ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult = ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult.CreateJavaScript("alert('No lines to buffer')")
            _callbackResultCollection.Add(noLinesAlertCallbackResult)
            _callbackResultCollection.Add(HideActivityIndicators())
            Return
        End If

        ' Add each polyline to the feature graphics layer that we're using as input to the GP task
        For Each dataRow As System.Data.DataRow In polylineElementGraphicsLayer.Rows
            Dim adfPolyline As ESRI.ArcGIS.ADF.Web.Geometry.Polyline = TryCast(polylineElementGraphicsLayer.GeometryFromRow(dataRow), ESRI.ArcGIS.ADF.Web.Geometry.Polyline)
            featureGraphicsLayer.Add(adfPolyline)
        Next dataRow

        ' Get the Web ADF Unit enumeration value corresponding to the user-defined unit 
        Dim adfGPLinearUnit As New ESRI.ArcGIS.ADF.Web.DataSources.GPLinearUnit()
        Dim adfUnits As ESRI.ArcGIS.ADF.Web.DataSources.Units = CType(System.Enum.Parse(GetType(ESRI.ArcGIS.ADF.Web.DataSources.Units), units, True), ESRI.ArcGIS.ADF.Web.DataSources.Units)

        ' Set the GP task's second input parameter (linear unit) using the user-defined
        ' distance and units 
        adfGPLinearUnit.Units = adfUnits
        adfGPLinearUnit.Value = bufferDistance

        ' Put the parameters in an input array and start the geoprocessing job
        Dim adfGPValueArray(1) As ESRI.ArcGIS.ADF.Web.DataSources.GPValue
        adfGPValueArray(0) = adfGPFeatureGraphicsLayer
        adfGPValueArray(1) = adfGPLinearUnit
        Dim jobID As String = agsGeoprocessingFunctionality.SubmitJob(GPTaskName, adfGPValueArray)

        '    #End Region

        '    #Region "Construct a callback to check the GP task's status"

        ' Get the output parameter names to use when retrieving geoprocessing job results
        Dim outputTaskParameters As String = Nothing
        Dim agsGPParameterInfo As ESRI.ArcGIS.ADF.Web.DataSources.GPParameterInfo
        For i As Integer = 0 To adfGPParamterInfoArray.Length - 1
            agsGPParameterInfo = adfGPParamterInfoArray(i)

            ' Only append output parameters to the parameter string
            If agsGPParameterInfo.Direction = ESRI.ArcGIS.ADF.Web.DataSources.GPParameterDirection.Output Then
                ' Only append the zip file parameter name if the user has checked the download
                ' checkbox
                If agsGPParameterInfo.Name = "output_zip" AndAlso (Not downloadOutput) Then
                    Continue For
                End If

                outputTaskParameters &= agsGPParameterInfo.Name
                If i <> adfGPParamterInfoArray.Length - 1 Then
                    outputTaskParameters &= ";"
                End If
            End If
        Next i

        Dim checkJobStatusCallbackResult As ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult = GetStatusCheckJavaScript(GPTaskName, jobID, outputTaskParameters, downloadOutput.ToString(), 1000)

        ' Add the GP job status check callback to the callback results collection
        _callbackResultCollection.Add(checkJobStatusCallbackResult)

        '    #End Region
    End Sub

    ' Check the GP job status for the job with the passed-in parameters and take action accordingly
    Protected Sub CheckGeoprocessingJobStatus(ByVal jobID As String, ByVal taskName As String, ByVal TaskOutputParameters As String, ByVal downloadOutput As Boolean)
        '    #Region "Initialize geoprocessing resources and functionalities"

        ' Initialize GP resource and functionality
        Dim geoprocessingResourceItem As ESRI.ArcGIS.ADF.Web.UI.WebControls.GeoprocessingResourceItem = GeoprocessingResourceManager1.ResourceItems.Find(_geoprocessingResourceName)

        ' Make sure the geoprocessing resource is initialized
        If (Not geoprocessingResourceItem.Resource.Initialized) Then
            geoprocessingResourceItem.InitializeResource()
        End If

        ' Get a reference to the GP resource and its functionality
        Dim geoprocessingResource As ESRI.ArcGIS.ADF.Web.DataSources.IGeoprocessingResource = CType(geoprocessingResourceItem.Resource, ESRI.ArcGIS.ADF.Web.DataSources.IGeoprocessingResource)

        Dim commonGeoprocessingFunctionality As ESRI.ArcGIS.ADF.Web.DataSources.IGeoprocessingFunctionality = CType(geoprocessingResource.CreateFunctionality(GetType(ESRI.ArcGIS.ADF.Web.DataSources.IGeoprocessingFunctionality), Nothing), ESRI.ArcGIS.ADF.Web.DataSources.IGeoprocessingFunctionality)

        Dim agsGeoprocessingFunctionality As ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.GeoprocessingFunctionality = TryCast(commonGeoprocessingFunctionality, ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.GeoprocessingFunctionality)

        ' Make sure the geoprocessing functionality is initialized
        If (Not agsGeoprocessingFunctionality.Initialized) Then
            agsGeoprocessingFunctionality.Initialize()
        End If

        '    #End Region

        '    #Region "Construct callback to alert user if GP task failed or to check job status again if task is not complete"

        ' Get the GP job's status
        Dim adfJobStatus As ESRI.ArcGIS.ADF.Web.DataSources.JobStatus = agsGeoprocessingFunctionality.GetJobStatus(jobID)

        ' If GP job failed, get job's last message and return it in an alert box
        If adfJobStatus = ESRI.ArcGIS.ADF.Web.DataSources.JobStatus.Failed OrElse adfJobStatus = ESRI.ArcGIS.ADF.Web.DataSources.JobStatus.TimedOut Then
            ' Get the GP job's messages
            Dim adfJobMessageArray() As ESRI.ArcGIS.ADF.Web.DataSources.JobMessage = agsGeoprocessingFunctionality.GetJobMessages(jobID)
            Dim messageCount As Integer = adfJobMessageArray.Length

            ' Create the alert javascript and package it in a callback
            Dim jsAlertGPError As String = String.Format("alert('GP job failed: {0})'", adfJobMessageArray(messageCount - 1).MessageDesc)
            Dim gpErrorAlertCallbackResult As ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult = ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult.CreateJavaScript(jsAlertGPError)

            ' Add the error alert to the callback results collection
            _callbackResultCollection.Add(gpErrorAlertCallbackResult)

            ' Add a callback result to hide the operaton activity indicators to the callback results collection
            _callbackResultCollection.Add(HideActivityIndicators())
            Return
        ElseIf Not adfJobStatus = ESRI.ArcGIS.ADF.Web.DataSources.JobStatus.Succeeded Then
            _callbackResultCollection.Add(GetStatusCheckJavaScript(taskName, jobID, TaskOutputParameters, downloadOutput.ToString(), 5000))
            Return
        End If

        '    #End Region

        '    #Region "Process the GP task's results"

        ' Parse the GP task's output parameter names
        Dim outputParametersArray() As String = TaskOutputParameters.Split(";"c)
        ' Get the result of the GP task
        Dim adfGPResult As ESRI.ArcGIS.ADF.Web.DataSources.GPResult = agsGeoprocessingFunctionality.GetJobResult(taskName, jobID, outputParametersArray, _displayResultsInMapResource)

        ' Iterate through the GP task results.  If the result is a data file and the user
        ' has checked the download results checkbox, add a callback that will enable 
        ' downloading of this file.  If the result is a graphics layer or a map resource
        ' definition, add the contents of these to the map so the results are displayed.
        Dim adfGPValueArray() As ESRI.ArcGIS.ADF.Web.DataSources.GPValue = adfGPResult.Values
        For Each adfGPValue As ESRI.ArcGIS.ADF.Web.DataSources.GPValue In adfGPValueArray
            If TypeOf adfGPValue Is ESRI.ArcGIS.ADF.Web.DataSources.GPDataFile AndAlso downloadOutput Then
                '        #Region "Make the results available for download"

                ' Get the URL of the data file
                Dim adfGPDataFile As ESRI.ArcGIS.ADF.Web.DataSources.GPDataFile = CType(adfGPValue, ESRI.ArcGIS.ADF.Web.DataSources.GPDataFile)
                Dim urlZipFileString As String = adfGPDataFile.Data.URL

                ' Construct JavaScript to insert an iframe into the page that has the data file url 
                ' as its source.  The iframe will attempt to load whatever is referenced as its
                ' source, which in this case will trigger a download dialog.
                Dim jsDynamicDownload As String = "var ifrm = document.createElement('IFRAME');" & "ifrm.setAttribute('src','{0}');" & "ifrm.style.width='0px';" & "ifrm.style.height='0px';" & "ifrm.style.visibility='hidden';" & "document.body.appendChild(ifrm);"
                jsDynamicDownload = String.Format(jsDynamicDownload, urlZipFileString)

                ' Package the download-triggering JavaScript in a callback and add to the callback results
                Dim dynamicDownloadCallbackResult As ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult = ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult.CreateJavaScript(jsDynamicDownload)
                _callbackResultCollection.Add(dynamicDownloadCallbackResult)

                '        #End Region
            ElseIf TypeOf adfGPValue Is ESRI.ArcGIS.ADF.Web.DataSources.GPFeatureGraphicsLayer Then
                '        #Region "Add the clipped features to the map as graphics"

                ' The clip results will be returned as a feature graphics layer if 
                ' _displayResultsInMapResource is set to false

                ' Get output feature set containing buffers as a Web ADF feature graphics layer
                Dim adfGPFeatureGraphicsLayer As ESRI.ArcGIS.ADF.Web.DataSources.GPFeatureGraphicsLayer = TryCast(adfGPValue, ESRI.ArcGIS.ADF.Web.DataSources.GPFeatureGraphicsLayer)
                Dim featureGraphicsLayer As ESRI.ArcGIS.ADF.Web.Display.Graphics.FeatureGraphicsLayer = adfGPFeatureGraphicsLayer.Layer

                ' If the graphics layer has no features, alert the user that the operation did not create 
                ' any output
                If (featureGraphicsLayer Is Nothing) OrElse (featureGraphicsLayer.Rows.Count < 1) Then
                    Dim noResultsAlertCallbackResult As ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult = ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult.CreateJavaScript("alert('No results returned');")
                    _callbackResultCollection.Add(noResultsAlertCallbackResult)

                    ' Add a callback result to hide the operation activity indicators and enable the
                    ' Buffer and Clip button
                    _callbackResultCollection.Add(HideActivityIndicators())
                    Return
                End If

                ' Get the graphics layer's symbols and set their transparency to 50%
                Dim featureSymbolList As New System.Collections.Generic.List(Of ESRI.ArcGIS.ADF.Web.Display.Symbol.FeatureSymbol)()
                featureGraphicsLayer.Renderer.GetAllSymbols(featureSymbolList)
                For Each featureSymbol As ESRI.ArcGIS.ADF.Web.Display.Symbol.FeatureSymbol In featureSymbolList
                    featureSymbol.Transparency = 50
                Next featureSymbol

                ' Set the feature graphics layer's name
                featureGraphicsLayer.TableName = _clippedFeaturesLayerName

                ' Get the graphics resource to put the results layer in
                Dim graphicsMapResource As ESRI.ArcGIS.ADF.Web.DataSources.Graphics.MapResource = TryCast(Map1.GetFunctionality(_graphicsResourceName).Resource, ESRI.ArcGIS.ADF.Web.DataSources.Graphics.MapResource)

                ' If the resource already has a results graphics layer, remove it
                If graphicsMapResource.Graphics.Tables.Contains(_clippedFeaturesLayerName) Then
                    graphicsMapResource.Graphics.Tables.Remove(_clippedFeaturesLayerName)
                End If

                ' Add new graphics layer to display new buffers
                graphicsMapResource.Graphics.Tables.Add(featureGraphicsLayer)

                ' Refresh the graphics resource and copy the map's callback results to the callback collection
                ' so the operation results are displayed
                Map1.RefreshResource(graphicsMapResource.Name)
                _callbackResultCollection.CopyFrom(Map1.CallbackResults)

                '        #End Region
            ElseIf TypeOf adfGPValue Is ESRI.ArcGIS.ADF.Web.DataSources.GPMapResourceDefinition Then
                '        #Region "Add the clipped features to the map as features                "

                ' The clip results will be returned in a map resource definition if 
                ' _displayResultsInMapResource is set to true.
                '
                ' Note that the code here makes use of the map resource definition created from the
                ' GP results by the Web ADF.  For more information on how to associate GP results 
                ' and ArcGIS Server map resources, see the LayerDescription help.

                ' Get the GP map resource definition 
                Dim gpMapResourceDefinition As ESRI.ArcGIS.ADF.Web.DataSources.GPMapResourceDefinition = TryCast(adfGPValue, ESRI.ArcGIS.ADF.Web.DataSources.GPMapResourceDefinition)

                ' Check whether a results map resource item has already been added
                Dim mapResourceItem As ESRI.ArcGIS.ADF.Web.UI.WebControls.MapResourceItem = MapResourceManager1.ResourceItems.Find(_resultsMapResourceName)
                If mapResourceItem Is Nothing Then
                    ' Create a new map resource item to hold the results since one doesn't yet exist

                    ' Instantiate the map resource item with the name specified in the class member.
                    mapResourceItem = New ESRI.ArcGIS.ADF.Web.UI.WebControls.MapResourceItem()
                    mapResourceItem.Name = _resultsMapResourceName

                    ' Apply the GP results resource definition to the map resource item
                    mapResourceItem.Definition = New ESRI.ArcGIS.ADF.Web.UI.WebControls.GISResourceItemDefinition(gpMapResourceDefinition.Value)

                    ' Initialize the map resource item's display properties
                    Dim displaySettings As New ESRI.ArcGIS.ADF.Web.DisplaySettings()
                    displaySettings.Transparency = 50
                    displaySettings.Visible = True
                    displaySettings.ImageDescriptor.TransparentColor = System.Drawing.Color.White
                    displaySettings.ImageDescriptor.TransparentBackground = True
                    mapResourceItem.DisplaySettings = displaySettings

                    ' Add the new map resource item to the resource manager
                    MapResourceManager1.ResourceItems.Insert(0, mapResourceItem)

                    ' Refresh the Toc and copy its callback results to the callback results collection so the new
                    ' resource has a Toc node
                    Toc1.Refresh()
                    _callbackResultCollection.CopyFrom(Toc1.CallbackResults)
                Else
                    ' Since the map resource item to hold the results has already been created, simply apply
                    ' the definition returned with the GP results
                    mapResourceItem.Definition = New ESRI.ArcGIS.ADF.Web.UI.WebControls.GISResourceItemDefinition(gpMapResourceDefinition.Value)
                End If

                ' Refresh the results resource and copy the map's callback results to the callback results 
                ' collection so the result features are displayed on the map
                Map1.RefreshResource(_resultsMapResourceName)
                _callbackResultCollection.CopyFrom(Map1.CallbackResults)

                '        #End Region
            End If
        Next adfGPValue

        _callbackResultCollection.Add(HideActivityIndicators())

        '    #End Region
    End Sub

    ' Creates a callback result with the client-side code needed to hide the operation activity indicator
    ' and enable the operation execution button
    Private Function HideActivityIndicators() As ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult
        Dim jsHideIndicators As String = "toggleActivityIndicators(false);"
        Dim hideIndicatorsCallbackResult As ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult = ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult.CreateJavaScript(jsHideIndicators)
        Return hideIndicatorsCallbackResult
    End Function

    ' Clears features from all graphics layers in the resource specified by _graphicsResourceName
    Private Sub ClearGraphics()
        ' Retrieve the resource and clear its graphics dataset
        Dim graphicsMapResource As ESRI.ArcGIS.ADF.Web.DataSources.Graphics.MapResource = TryCast(Map1.GetFunctionality(_graphicsResourceName).Resource, ESRI.ArcGIS.ADF.Web.DataSources.Graphics.MapResource)
        graphicsMapResource.Graphics.Clear()

        ' Refresh the resource and copy the map's callback results to the callback results collection so
        ' the graphics are removed from the map
        Map1.RefreshResource(graphicsMapResource.Name)
        _callbackResultCollection.CopyFrom(Map1.CallbackResults)
    End Sub


    ' Removes the layer matching the input name from the TOC
    Private Sub RemoveLayerFromToc(ByVal layerName As String)
        ' Iterate through the nodes at the top level of the Toc.  Each of these should correspond to a resource.
        For Each resourceTreeViewPlusNode As ESRI.ArcGIS.ADF.Web.UI.WebControls.TreeViewPlusNode In Toc1.Nodes
            Dim treeViewPlusNodeToRemove As ESRI.ArcGIS.ADF.Web.UI.WebControls.TreeViewPlusNode = Nothing

            ' Iterate through the nodes below the current top-level node.  Each of these should 
            ' correspond to a layer.
            For Each layerTreeViewPlusNode As ESRI.ArcGIS.ADF.Web.UI.WebControls.TreeViewPlusNode In resourceTreeViewPlusNode.Nodes
                ' Attempt to retrieve a node with text matching the passed-in layer name.  This could be
                ' the current node or any node below it.  Note that we need to search both the current node
                ' and any child nodes to handle group layers.
                Dim matchingTreeViewPlusNode As ESRI.ArcGIS.ADF.Web.UI.WebControls.TreeViewPlusNode = Utility.FindNodeRecursive(layerTreeViewPlusNode, layerName)

                ' If a matching node was found, store a reference to it to be accessed outside the loop
                If matchingTreeViewPlusNode IsNot Nothing Then
                    treeViewPlusNodeToRemove = matchingTreeViewPlusNode
                    Exit For
                End If
            Next layerTreeViewPlusNode

            ' Remove the matching node, if found
            If treeViewPlusNodeToRemove IsNot Nothing Then
                treeViewPlusNodeToRemove.Remove()
            End If
        Next resourceTreeViewPlusNode
    End Sub

    Private Function GetStatusCheckJavaScript(ByVal GPTaskName As String, ByVal jobID As String, ByVal TaskOutputParameters As String, ByVal downloadoutput As String, ByVal timeout As Integer) As ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult
        Dim arguments As String = String.Format("EventArg=CheckGeoprocessingJobStatus&JobID={0}&TaskName={1}&OutputParameters={2}&DownloadOutput={3}", jobID, GPTaskName, TaskOutputParameters, downloadoutput)
        ' Construct a callback with the JavaScript needed to trigger a second callback after one second 
        ' (1000 milliseconds) that will execute logic on the server to check the GP job's status and
        ' take action accordingly
        Dim jsCheckJobStatus As String = "window.setTimeout(""ESRI.ADF.Samples.PostbackManager.doAsyncRequest('{0}');"",{1});"
        jsCheckJobStatus = String.Format(jsCheckJobStatus, arguments, timeout)
        Dim checkJobStatusCallbackResult As ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult = ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult.CreateJavaScript(jsCheckJobStatus)
        Return checkJobStatusCallbackResult
    End Function

#End Region

End Class