Common MapTips
Common_MapTips_VBNet\App_Code\MapTipsLayerMenu.vb
' 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.
' 

Imports Microsoft.VisualBasic
Imports System
Public Class MapTipsLayerMenu
  Implements ESRI.ArcGIS.ADF.Web.UI.WebControls.Tools.IMapServerDropDownBoxAction
  #Region "IServerAction Members"

  Private Sub IServerAction_ServerAction(ByVal toolbarItemInfo As ESRI.ArcGIS.ADF.Web.UI.WebControls.Tools.ToolbarItemInfo) Implements ESRI.ArcGIS.ADF.Web.UI.WebControls.Tools.IServerAction.ServerAction
    ' Get the Web ADF Map
    Dim adfMap As ESRI.ArcGIS.ADF.Web.UI.WebControls.Map = TryCast(toolbarItemInfo.BuddyControls(0), ESRI.ArcGIS.ADF.Web.UI.WebControls.Map)

    ' Get the DropDownBox containing the layer names
    Dim dropDownBox As ESRI.ArcGIS.ADF.Web.UI.WebControls.DropDownBox = TryCast(toolbarItemInfo.Toolbar.ToolbarItems.Find("DropDownBox0"), ESRI.ArcGIS.ADF.Web.UI.WebControls.DropDownBox)

    ' Get the MapTips control that will be used to show MapTips on the selected layer        
    Dim mapTips As ESRI.ArcGIS.ADF.Web.UI.WebControls.MapTips = TryCast(ESRI.ArcGIS.ADF.Web.UI.WebControls.Utility.FindControl("MapTips1", adfMap.Page), ESRI.ArcGIS.ADF.Web.UI.WebControls.MapTips)

    ' Get the selected layer name
    Dim layerName As String = dropDownBox.SelectedItem.Text
    If layerName = "- Select MapTips Layer -" Then
      Return
    End If

    ' Get the ID of the selected layer and the name of the resource item containing that layer
    Dim resourceNameAndLayerID() As String = dropDownBox.SelectedItem.Value.Split(":"c)

    ' Update the MapTips Layer property
    mapTips.Layer = String.Format("{0}::{1}::{2}", adfMap.MapResourceManager, resourceNameAndLayerID(0), layerName)

    ' Retrieve the layer format of the selected layer and apply it to the MapTips control
    Dim layerFormat As ESRI.ArcGIS.ADF.Web.UI.WebControls.LayerFormat = ESRI.ArcGIS.ADF.Web.UI.WebControls.LayerFormat.FromMapResourceManager(adfMap.MapResourceManagerInstance, resourceNameAndLayerID(0), resourceNameAndLayerID(1))

'  #Region "Only necessary for 9.3 final "
    Dim mapResourceItem As ESRI.ArcGIS.ADF.Web.UI.WebControls.MapResourceItem = adfMap.MapResourceManagerInstance.ResourceItems.Find(mapTips.ResourceName)

    Dim layerFormatDictionary As New System.Collections.Generic.Dictionary(Of String, ESRI.ArcGIS.ADF.Web.UI.WebControls.LayerFormat)()

    ' Iterate through the resource's layer definitions and add each one's ID and LayerFormat to the
    ' dictionary
    For Each layerDefinition As ESRI.ArcGIS.ADF.Web.UI.WebControls.LayerDefinition In mapResourceItem.LayerDefinitions
      layerFormatDictionary.Add(layerDefinition.ID, layerDefinition.LayerFormat)
    Next layerDefinition

    ' Set the resource's layer definitions to null to force full re-initialization
    mapResourceItem.LayerDefinitions = Nothing

    ' Since the re-initialization will revert the layers to their default LayerFormats, explicitly
    ' re-apply the stored LayerFormats
    For Each layerID As String In layerFormatDictionary.Keys
      mapResourceItem.LayerDefinitions(layerID).LayerFormat = layerFormatDictionary(layerID)
    Next layerID
'  #End Region

    mapTips.LayerFormat = layerFormat

    ' Refresh the MapTips and the resource on which MapTips are to be shown to apply the changes
    mapTips.RefreshMapTips()
    adfMap.RefreshResource(mapTips.ResourceName)
  End Sub

  #End Region
End Class