ADF Tutorials
ADFTutorials_VBNet\UsingCommonAPI\App_Code\SelectTool.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.
' 

Imports Microsoft.VisualBasic
Imports System
Imports System.Data
Imports System.Configuration
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls

''' <summary>
''' Summary description for SelectTool
''' </summary>
Public Class SelectTool
  Implements ESRI.ArcGIS.ADF.Web.UI.WebControls.Tools.IMapServerToolAction
    Private Sub ServerAction(ByVal toolEventArgs As ESRI.ArcGIS.ADF.Web.UI.WebControls.ToolEventArgs) Implements ESRI.ArcGIS.ADF.Web.UI.WebControls.Tools.IMapServerToolAction.ServerAction

        Dim adfMap As ESRI.ArcGIS.ADF.Web.UI.WebControls.Map = CType(toolEventArgs.Control, ESRI.ArcGIS.ADF.Web.UI.WebControls.Map)

        Dim mapRectangleEventArgs As ESRI.ArcGIS.ADF.Web.UI.WebControls.MapRectangleEventArgs = CType(toolEventArgs, ESRI.ArcGIS.ADF.Web.UI.WebControls.MapRectangleEventArgs)
        Dim adfEnvelope As ESRI.ArcGIS.ADF.Web.Geometry.Envelope = mapRectangleEventArgs.MapExtent

        ' Get the name of the resource on which to perform the selection
        Dim resourceName As String = CStr(adfMap.Page.Request.Params("DropDownList1"))

        ' Get a reference to the resource
        Dim commonMapFunctionality As ESRI.ArcGIS.ADF.Web.DataSources.IMapFunctionality = adfMap.GetFunctionality(resourceName)
        Dim gisResource As ESRI.ArcGIS.ADF.Web.DataSources.IGISResource = commonMapFunctionality.Resource

        ' Create a query functionality to use in querying the resource
        Dim commonQueryFunctionality As ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality = CType(gisResource.CreateFunctionality(GetType(ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality), Nothing), ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality)

        ' Get the resource's queryable layers
        Dim layerIDs As String() = Nothing
        Dim layerNames As String() = Nothing
        commonQueryFunctionality.GetQueryableLayers(Nothing, layerIDs, layerNames)

        ' Get the name of the selection layer from the page's request parameters
        Dim selectionLayerName As String = CStr(adfMap.Page.Request.Params("DropDownList2"))
        Dim selectionLayerID As String = Nothing

        ' Get the ID of the selection layer from the layer IDs array
        Dim i As Integer = 0
        Do While i < layerNames.Length
            If layerNames(i) = selectionLayerName Then
                selectionLayerID = layerIDs(i)
                Exit Do
            End If
            i += 1
        Loop

        ' Set-up a spatial filter to use in querying the resource
        Dim adfSpatialFilter As ESRI.ArcGIS.ADF.Web.SpatialFilter = New ESRI.ArcGIS.ADF.Web.SpatialFilter()
        adfSpatialFilter.ReturnADFGeometries = True
        adfSpatialFilter.MaxRecords = 100
        adfSpatialFilter.Geometry = adfEnvelope

        ' Query the selection layer with the user-drawn rectangle
        Dim resultsDataTable As System.Data.DataTable = commonQueryFunctionality.Query(commonMapFunctionality.Name, selectionLayerID, adfSpatialFilter)

        ' Convert the results data table to a graphics layer
        Dim resultsGraphicsLayer As ESRI.ArcGIS.ADF.Web.Display.Graphics.GraphicsLayer = ESRI.ArcGIS.ADF.Web.Converter.ToGraphicsLayer(resultsDataTable)

        ' Select each result so that its node is checked in the TaskResults control
        For Each dataRow As System.Data.DataRow In resultsGraphicsLayer.Rows
            dataRow(resultsGraphicsLayer.IsSelectedColumn) = True
        Next dataRow

        ' Retrieve the selection layer's layerFormat and apply it to the results
        Dim layerFormat As ESRI.ArcGIS.ADF.Web.UI.WebControls.LayerFormat = ESRI.ArcGIS.ADF.Web.UI.WebControls.LayerFormat.FromMapResourceManager(adfMap.MapResourceManagerInstance, resourceName, selectionLayerID)
        layerFormat.Apply(resultsGraphicsLayer)

        ' Render the results on the client to enable callouts and highlighting
        resultsGraphicsLayer.RenderOnClient = True

        ' Create a DataSet and add the results to it
        Dim resultsDataSetName As String = String.Format("Selected Features - {0}", resultsGraphicsLayer.TableName)
        Dim resultsDataSet As System.Data.DataSet = New System.Data.DataSet(resultsDataSetName)
        resultsDataSet.Tables.Add(resultsGraphicsLayer)

        ' Retrieve the TaskResults control from session and add the results to it
        Dim taskResults As ESRI.ArcGIS.ADF.Web.UI.WebControls.TaskResults = CType(adfMap.Page.Session("TaskResultsControl"), ESRI.ArcGIS.ADF.Web.UI.WebControls.TaskResults)
        taskResults.DisplayResults(Nothing, Nothing, Nothing, resultsDataSet)

        ' Copy the TaskResults' callback results to the Map so the results show up
        adfMap.CallbackResults.CopyFrom(taskResults.CallbackResults)


    End Sub
End Class