ADF Tutorials
ADFTutorials_CSharp\UsingCommonAPI\App_Code\SelectTool.cs
// 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.
// 

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/// <summary>
/// Summary description for SelectTool
/// </summary>
public class SelectTool : ESRI.ArcGIS.ADF.Web.UI.WebControls.Tools.IMapServerToolAction
{
    void ESRI.ArcGIS.ADF.Web.UI.WebControls.Tools.IMapServerToolAction.ServerAction(ESRI.ArcGIS.ADF.Web.UI.WebControls.ToolEventArgs toolEventArgs)
    {
        ESRI.ArcGIS.ADF.Web.UI.WebControls.Map adfMap =
            (ESRI.ArcGIS.ADF.Web.UI.WebControls.Map)toolEventArgs.Control;

        ESRI.ArcGIS.ADF.Web.UI.WebControls.MapRectangleEventArgs mapRectangleEventArgs =
    (ESRI.ArcGIS.ADF.Web.UI.WebControls.MapRectangleEventArgs)toolEventArgs;
        ESRI.ArcGIS.ADF.Web.Geometry.Envelope adfEnvelope =
            mapRectangleEventArgs.MapExtent;

        // Get the name of the resource on which to perform the selection
        string resourceName = (string)adfMap.Page.Request.Params["DropDownList1"];

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

        // Create a query functionality to use in querying the resource
        ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality commonQueryFunctionality =
            (ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality)
            gisResource.CreateFunctionality(
            typeof(ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality), null);

        // Get the resource's queryable layers
        string[] layerIDs = null;
        string[] layerNames = null;
        commonQueryFunctionality.GetQueryableLayers(null, out layerIDs, out layerNames);

        // Get the name of the selection layer from the page's request parameters
        string selectionLayerName = (string)adfMap.Page.Request.Params["DropDownList2"];
        string selectionLayerID = null;

        // Get the ID of the selection layer from the layer IDs array
        for (int i = 0; i < layerNames.Length; i++)
        {
            if (layerNames[i] == selectionLayerName)
            {
                selectionLayerID = layerIDs[i];
                break;
            }
        }

        // Set-up a spatial filter to use in querying the resource
        ESRI.ArcGIS.ADF.Web.SpatialFilter adfSpatialFilter =
        new ESRI.ArcGIS.ADF.Web.SpatialFilter();
        adfSpatialFilter.ReturnADFGeometries = true;
        adfSpatialFilter.MaxRecords = 100;
        adfSpatialFilter.Geometry = adfEnvelope;

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

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

        // Select each result so that its node is checked in the TaskResults control
        foreach (System.Data.DataRow dataRow in resultsGraphicsLayer.Rows)
            dataRow[resultsGraphicsLayer.IsSelectedColumn] = true;

        // Retrieve the selection layer's layerFormat and apply it to the results
        ESRI.ArcGIS.ADF.Web.UI.WebControls.LayerFormat 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
        string resultsDataSetName = string.Format("Selected Features - {0}",
        resultsGraphicsLayer.TableName);
        System.Data.DataSet resultsDataSet = new System.Data.DataSet(resultsDataSetName);
        resultsDataSet.Tables.Add(resultsGraphicsLayer);

        // Retrieve the TaskResults control from session and add the results to it
        ESRI.ArcGIS.ADF.Web.UI.WebControls.TaskResults taskResults =
        (ESRI.ArcGIS.ADF.Web.UI.WebControls.TaskResults)
        adfMap.Page.Session["TaskResultsControl"];
        taskResults.DisplayResults(null, null, null, resultsDataSet);

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


    }
}