Common Add graphics
Common_AddGraphics_VBNet\Default_JavaScriptMapTips.aspx.vb
' 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.
' 

Imports Microsoft.VisualBasic
Imports System
Public Partial Class Default_JavaScriptMapTips
  Inherits System.Web.UI.Page
  #Region "ASP.NET Page Event Handlers"

  Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs)
    ' Make sure the page is not in a postback, so the code enclosed in the if block
    ' only executes on initial page load
    If (Not Page.IsPostBack) Then
      ' Call the method that will populate the graphics resource
      ' included in the MapResourceManager
      AddGraphics()
    End If
  End Sub

  #End Region

  #Region "Instance Methods"

  Private Sub 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"
      Dim graphicsMapFunctionality As ESRI.ArcGIS.ADF.Web.DataSources.Graphics.MapFunctionality = CType(Map1.GetFunctionality("ADFGraphicsResource"), ESRI.ArcGIS.ADF.Web.DataSources.Graphics.MapFunctionality)

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

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

      ' Create a non-data source specific query functionality object from the GIS resource
      Dim queryFunctionality As ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality = CType(gisResource.CreateFunctionality (GetType(ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality), Nothing), ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality)

      ' Declare string arrays to store layer IDs and names
            Dim layerIDs As String() = Nothing
            Dim layerNames As String() = Nothing
      ' 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(Nothing, layerIDs, layerNames)

      ' Set the name of the layer to query and that will be used to generate map tips
            Dim queryLayerName As String = "counties"

      ' Declare a variable to hold the ID of the query layer
      Dim queryLayerID As String = Nothing
      ' Iterate through the layer names array to find the ID of the query layer
      Dim index As Integer = 0
      Do While index < layerNames.Length
        ' 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() Then
                    queryLayerID = layerIDs(index)
                    Exit Do
                End If
        index += 1
      Loop

      ' Instantiate a Web ADF SpatialFilter to use in executing the query
      Dim adfSpatialFilter As ESRI.ArcGIS.ADF.Web.SpatialFilter = 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.
      Dim resultDataTable As System.Data.DataTable = 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.
      Dim featureGraphicsLayer As ESRI.ArcGIS.ADF.Web.Display.Graphics.FeatureGraphicsLayer = CType(ESRI.ArcGIS.ADF.Web.Converter.ToGraphicsLayer(resultDataTable), ESRI.ArcGIS.ADF.Web.Display.Graphics.FeatureGraphicsLayer)

      ' 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
      Dim layerFormat As ESRI.ArcGIS.ADF.Web.UI.WebControls.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.
      Dim selectedColumn As System.Data.DataColumn = 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.  
      For Each row As System.Data.DataRow In featureGraphicsLayer.Rows
        row(selectedColumn) = True
      Next row
    Catch exception As System.Exception
      Dim jsErrorAlert As String = String.Format("<script>{0}</script>", Utility.GetJavaScriptErrorString(exception))
      Response.Write(jsErrorAlert)
    End Try
  End Sub

  #End Region
End Class