Common Add graphics
Common_AddGraphics_CSharp\Default_JavaScriptMapTips.aspx.cs
// Copyright 2011 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 Default_JavaScriptMapTips : System.Web.UI.Page
{
    #region ASP.NET Page Event Handlers

    protected void Page_PreRender(object sender, System.EventArgs e)
    {
        // Make sure the page is not in a postback, so the code enclosed in the if block
        // only executes on initial page load
        if (!Page.IsPostBack)
        {
            // Call the method that will populate the graphics resource
            // included in the MapResourceManager
            AddGraphics();
        }
    }

    #endregion

    #region Instance Methods

    private void AddGraphics()
    {
        try
        {
            // Get the graphics map functionality for the graphics resource specified in the 
            // MapResourceManager.  Note that this MapFunctionality type is data-source specific
            // (graphics).  Also, the code assumes the resource is named "ADFGraphicsResource"
            ESRI.ArcGIS.ADF.Web.DataSources.Graphics.MapFunctionality graphicsMapFunctionality =
                (ESRI.ArcGIS.ADF.Web.DataSources.Graphics.MapFunctionality)Map1.GetFunctionality("ADFGraphicsResource");

            // Get the common map functionality for the map resource named "California."  This 
            // MapFunctionality type is NOT data-source specific.
            ESRI.ArcGIS.ADF.Web.DataSources.IMapFunctionality commonMapFunctionality =
              (ESRI.ArcGIS.ADF.Web.DataSources.IMapFunctionality)Map1.GetFunctionality("California");

            // Get a reference to the underlying GIS resource from the common map functionality
            ESRI.ArcGIS.ADF.Web.DataSources.IGISResource gisResource = commonMapFunctionality.Resource;

            // Create a non-data source specific query functionality object from the GIS resource
            ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality queryFunctionality
                = (ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality)gisResource.CreateFunctionality
                (typeof(ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality), null);

            // Declare string arrays to store layer IDs and names
            string[] layerIDs;
            string[] layerNames;
            // Get the queryable layers contained in the "California" map resource via the 
            // query functionality object created by this resource.  Store the output in 
            // the layer ID and name arrays.
            queryFunctionality.GetQueryableLayers(null, out layerIDs, out layerNames);

            // Set the name of the layer to query and that will be used to generate map tips
            string queryLayerName = "COUNTIES";

            // Declare a variable to hold the ID of the query layer
            string queryLayerID = null;
            // Iterate through the layer names array to find the ID of the query layer
            for (int index = 0; index < layerNames.Length; index++)
            {
                // Check to see whether the layer name at the current index of the array matches
                // the name of the query layer.  If so, retrieve the ID at the same index from 
                // the layer ID array and assign it to the query layer ID variable
                if (layerNames[index].ToLower() == queryLayerName.ToLower())
                {
                    queryLayerID = layerIDs[index];
                    break;
                }
            }

            // Instantiate a Web ADF SpatialFilter to use in executing the query
            ESRI.ArcGIS.ADF.Web.SpatialFilter adfSpatialFilter =
                new ESRI.ArcGIS.ADF.Web.SpatialFilter();
            // Set the property that will cause feature geometries to be return with the query results
            adfSpatialFilter.ReturnADFGeometries = true;

            // Execute a query on the COUNTIES layer of the California map resource.  Since the spatial
            // filter does not have a geometry defined, the query will return all the features in the 
            // COUNTIES layer.  Note that the name from either commonMapFunctionality or 
            // graphicsMapFunctionality could have been used for the mapFunctionalityName parameter,
            // as these both refer ultimately to Map1's map functionality.
            System.Data.DataTable resultDataTable =
                queryFunctionality.Query(commonMapFunctionality.Name, queryLayerID, adfSpatialFilter);

            // Convert the data table containing the query results to an ADF feature graphics layer.
            // This is done by using the ADF's Converter utility class, and then casting the resulting
            // GraphicsLayer to a FeatureGraphicsLayer.  Note that this can be done because the 
            // data table contains Web ADF feature data, including the ADF geometries.
            ESRI.ArcGIS.ADF.Web.Display.Graphics.FeatureGraphicsLayer featureGraphicsLayer =
                (ESRI.ArcGIS.ADF.Web.Display.Graphics.FeatureGraphicsLayer)
                ESRI.ArcGIS.ADF.Web.Converter.ToGraphicsLayer(resultDataTable);

            // Set the property to draw the feature graphics layer on the client.  The layer
            // will not be visible if this property is set to false.
            featureGraphicsLayer.RenderOnClient = true;

            // Get the layer format from the COUNTIES layer of the California map resource. The
            // layer format specifies how selected features appear, field aliases, which fields
            // are visible, the primary display field, and how attribute displays are formatted
            ESRI.ArcGIS.ADF.Web.UI.WebControls.LayerFormat layerFormat =
                ESRI.ArcGIS.ADF.Web.UI.WebControls.LayerFormat.FromMapResourceManager(
                MapResourceManager1, "California", queryLayerID);

            // Remove any other graphics from ADFGraphicsResource
            graphicsMapFunctionality.GraphicsDataSet.Tables.Clear();
            // Add the feature graphics layer to ADFGraphicsResource
            graphicsMapFunctionality.GraphicsDataSet.Tables.Add(featureGraphicsLayer);

            // Apply the layer format from the COUNTIES layer to the feature graphics layer.
            layerFormat.Apply(featureGraphicsLayer);

            // Determine which column in the data table underlying the feature graphics layer
            // is the one to store whether the feature is selected or not.
            System.Data.DataColumn selectedColumn = featureGraphicsLayer.IsSelectedColumn;

            // Set each row in the data table underlying the feature graphics layer to have
            // a selected value of true, thus displaying the feature composed of that row
            // as selected.  The combination of applying the layer format to and selecting 
            // each feature within the feature graphics layer will have the effect of enabling
            // the out-of-the-box on hover and on click functionalities for selected features
            // for each feature within the graphics layer.  Since every county contained in the
            // COUNTIES layer is also in the feature graphics layer, the end result is an
            // in-memory copy of the COUNTIES layer that highlights and shows a map-tip for
            // each county on hover, and shows detailed attribute information on click.  Note
            // that, to modify the appearance of the highlight, map-tip, or attribute information,
            // users can simply edit the LayerDefinition of the COUNTIES layer via the
            // MapResourceManager in Visual Studio's design view.  
            foreach (System.Data.DataRow row in featureGraphicsLayer.Rows)
                row[selectedColumn] = true;
        }
        catch (System.Exception exception)
        {
            string jsErrorAlert = string.Format("<script>{0}</script>", 
                Utility.GetJavaScriptErrorString(exception));
            Response.Write(jsErrorAlert);
        }
    }

    #endregion
}