Common Task results
Common_TaskResults_CSharp\TaskResultsWebSite\GraphicsLayerResult.aspx.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.
// 

public partial class GraphicsLayerResult : System.Web.UI.Page
{
    protected void Toolbar1_CommandClick(object sender, 
        ESRI.ArcGIS.ADF.Web.UI.WebControls.ToolbarCommandClickEventArgs toolbarCommandClickEventArgs)
    {
        // Check to make sure our custom command was clicked.  This is not necessary in this case since there 
        // is only one item on the toolbar, but is included here for demonstration.
        switch (toolbarCommandClickEventArgs.CommandName)
        {
            case ("SelectCitiesInExtent"): 
                // Get the map resource item to be queried and make sure the underlying resource is initialized
                ESRI.ArcGIS.ADF.Web.UI.WebControls.MapResourceItem mapResourceItem = 
                    MapResourceManager1.ResourceItems.Find("MapResourceItem0");
                if (!mapResourceItem.Resource.Initialized)
                    mapResourceItem.InitializeResource();

                // Create a query functionality to use in executing the query
                ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality commonQueryFunctionality = 
                    mapResourceItem.Resource.CreateFunctionality(typeof(ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality), 
                    "addDataResultQueryFunctionality") as ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality;

                // Create a spatial filter and initialize its geometry to the current map extent
                ESRI.ArcGIS.ADF.Web.SpatialFilter adfSpatialFilter = new ESRI.ArcGIS.ADF.Web.SpatialFilter();
                adfSpatialFilter.Geometry = Map1.Extent;

                // Execute the query on the first layer in the resource
                System.Data.DataTable resultsTable = commonQueryFunctionality.Query(null, "0", adfSpatialFilter);

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

                // Apply the layer format of the queried layer to the query results
                ESRI.ArcGIS.ADF.Web.UI.WebControls.LayerFormat layerFormat =
                    ESRI.ArcGIS.ADF.Web.UI.WebControls.LayerFormat.FromMapResourceManager(MapResourceManager1,
                    "MapResourceItem0", "0");
                layerFormat.Apply(resultsGraphicsLayer);
                
                // Select each feature in the graphics layer so its selected symbol shows up
                foreach (System.Data.DataRow row in resultsGraphicsLayer.Rows)
                    row[resultsGraphicsLayer.IsSelectedColumn] = true;

                // Specify that the layer be rendered in the client tier so it has client-side functionality
                // (e.g. highlighting and mapTips on hover)
                resultsGraphicsLayer.RenderOnClient = true;

                // One method of displaying the results is to place the graphics layer in a dataset and then pass 
                // that dataset to DisplayResults.  This method will result in default formatting of the resulting
                // TaskResultNodes
                System.Data.DataSet resultsDataSet = new System.Data.DataSet(
                    string.Format("Results", resultsTable.Rows.Count));
                resultsDataSet.Tables.Add(resultsGraphicsLayer);

                TaskResults1.DisplayResults(null, null, null, resultsDataSet);


                // An alternative method of displaying the graphics layer is to explictly create TaskResultNodes.
                // This method allows custom configuration of those nodes, providing options such as displaying
                // a legend and expanding the nodes

                // Create a parent node for the results and assign it a remove-only context menu
                //ESRI.ArcGIS.ADF.Web.UI.WebControls.TaskResultNode headingTaskResultNode =
                //    new ESRI.ArcGIS.ADF.Web.UI.WebControls.TaskResultNode(string.Format("Results ({0})",
                //    resultsTable.Rows.Count));
                //TaskResults1.SetupContextMenu(TaskResults1.RemoveOnlyContextMenu, headingTaskResultNode);

                //// Create a node with the results graphics layer
                //ESRI.ArcGIS.ADF.Web.UI.WebControls.TreeViewPlusNode resultsTreeViewPlusNode =
                //    TaskResults1.CreateDataTableNode(resultsGraphicsLayer, true, false, true, null, "Query Results");

                //// Expand the layer node and assign it a remove-only context menu
                //resultsTreeViewPlusNode.Expanded = true;
                //TaskResults1.SetupContextMenu(TaskResults1.RemoveOnlyContextMenu, resultsTreeViewPlusNode);

                //// Add the data node under the parent node and call EnsureVisible to make sure the parent node
                //// is not collapsed.  Note that EnsureVisible needs to be called after the node is added.
                //headingTaskResultNode.Nodes.Add(resultsTreeViewPlusNode);
                //resultsTreeViewPlusNode.EnsureVisible();

                //// Display the custom node by passing it to DisplayResults
                //TaskResults1.DisplayResults(null, null, null, headingTaskResultNode);

                // Copy the TaskResults control's callback results to the Toolbar so they are processed
                // on the client
                Toolbar1.CallbackResults.CopyFrom(TaskResults1.CallbackResults);
                break;
        }
    }
}