Common Extend tasks
Common_ExtendTasks_CSharp\App_Code\ExtendFindAddressTask.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.Web;
using ESRI.ArcGIS.ADF.Tasks;
using ESRI.ArcGIS.ADF.Web.UI.WebControls;
using ESRI.ArcGIS.ADF.Web.DataSources;
using ESRI.ArcGIS.ADF.Web.Display.Graphics;

namespace ExtendedTasks
{
    public class ExtendFindAddressTask : FindAddressTask
    {
        TaskResults m_taskResults = null;

        #region Instance Properties

        // Convenient access to the first TaskResults control in the Task's TaskResultsContainers collection
        private ESRI.ArcGIS.ADF.Web.UI.WebControls.TaskResults TaskResultsInstance
        {
            get
            {
                // Retrieve the TaskResults control if it has not already been
                if ((m_taskResults == null) && (TaskResultsContainers[0] != null))
                    m_taskResults = Utility.FindControl(TaskResultsContainers[0].Name, Page) as
                        ESRI.ArcGIS.ADF.Web.UI.WebControls.TaskResults;
                return m_taskResults;
            }
        }

        #endregion

        #region FindAddressTask Overrides

        // Customizes the task results
        public override void ExecuteTask()
        {
            // Call the base task's ExcecuteTask function so that a default set of results is created
            base.ExecuteTask();

            // Results generated by the FindAddressTask are packaged in a Web ADF GraphicsDataSet
            if (Results is GraphicsDataSet)
            {
                GraphicsDataSet resultsGraphicsDataSet = (GraphicsDataSet)Results;

                // Only one table of match candidates is packaged as a FeatureGraphicsLayer and returned.
                FeatureGraphicsLayer matchedResultsLayer = (FeatureGraphicsLayer)resultsGraphicsDataSet.Tables[0];

                // Define custom default renderer
                ESRI.ArcGIS.ADF.Web.Display.Renderer.SimpleRenderer newSimpleRenderer =
                    new ESRI.ArcGIS.ADF.Web.Display.Renderer.SimpleRenderer();

                ESRI.ArcGIS.ADF.Web.Display.Symbol.SimpleMarkerSymbol simpleMarkerSymbol =
                    new ESRI.ArcGIS.ADF.Web.Display.Symbol.SimpleMarkerSymbol();
                simpleMarkerSymbol.Color = System.Drawing.Color.Red;
                simpleMarkerSymbol.Type = ESRI.ArcGIS.ADF.Web.Display.Symbol.MarkerSymbolType.Circle;
                simpleMarkerSymbol.Width = 12;
                simpleMarkerSymbol.Transparency = 50;

                newSimpleRenderer.Symbol = simpleMarkerSymbol;

                // The FindAddressTask base class generates a FeatureGraphicsLayer with a layer format.
                // The layer format will be used to change the symbology and attributes displayed as results.
                LayerFormat resultLayerFormat = LayerFormat.GetDefault(matchedResultsLayer);
                
                // If results are shown as map tips, client graphics are used to render results.  If not, Web-tier graphics
                // are used.  Web-tier graphics are not interactive, thus the highlight renderer is not used.
                resultLayerFormat.Renderer = newSimpleRenderer;
                // Use the HighlightRenderer defined in the page markup.
                resultLayerFormat.HighlightRenderer = this.HighlightRenderer;

                // Create a collection of fields to define those you want to alias and render visible in 
                // task results and the map tips callout window.
                System.Collections.Specialized.NameValueCollection fieldsCollection =
                    new System.Collections.Specialized.NameValueCollection();

                // If specified, the field will be visible and its name will be aliased.  
                // The first parameter is the actual field name.  The second parameter is the alias name.
                fieldsCollection.Add("Score", "SCORE");

                // Iterate through the fields in the layer format and set properties. Assign alias names and visibility.
                foreach (FieldInfo fieldInfo in resultLayerFormat.Fields)
                {
                    bool fieldDisplayed = false;                    
                    for (int i = 0; i < fieldsCollection.Keys.Count; i++)
                    {
                        if (fieldsCollection.GetKey(i) == fieldInfo.Name)
                        {
                            fieldInfo.Alias = fieldsCollection.GetValues(i)[0];
                            fieldInfo.Visible = true;
                            fieldDisplayed = true;
                            break;
                        }

                        if (!fieldDisplayed)
                            fieldInfo.Visible = false;
                    }
                }

                // Define the primary display fields in the title of the map tip callout and the parent node for each result row in task results. 
                resultLayerFormat.Title = "{Match_addr}";

                // Once finished modifying the layer format, apply it to the graphics layer. 
                resultLayerFormat.Apply(matchedResultsLayer);
            }
        }
        #endregion
    }
}