Common Add custom tool
Common_AddCustomTool_VBNet\Default.aspx.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 Partial Class [Default]
  Inherits System.Web.UI.Page
  Implements IBaseToolbarRefresh
  #Region "ASP.NET Page Event Handlers"

  Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs)

    ' Used to determine if a previous/next action occurred
    If (Not IsPostBack) Then
      Session("previousNext") = False
      Session("previousNextMapHandler") = False
    End If
  End Sub

  #End Region

  #Region "Web ADF Control Event Handlers"

  Protected Sub Map1_ExtentChanged(ByVal sender As Object, ByVal extentEventArgs As ESRI.ArcGIS.ADF.Web.UI.WebControls.ExtentEventArgs)
    ' PreviousNext session variable tracks if a Previous or Next command was initiated to change extent
    If Not Session("previousNext") Is Nothing Then
      Try
        Dim isPreviousNext As Boolean = CBool(Session("previousNext"))
        Dim isPreviousNextMapHandler As Boolean = CBool(Session("previousNextMapHandler"))

        ' If Previous or Next command, isPreviousNext is true.  Reset session variable to false
        If isPreviousNext Then
          ' Reset to false
          Session("previousNext") = False
        ElseIf isPreviousNextMapHandler Then
          Session("previousNextMapHandler") = False
        ' If another event changed the map extent, do the following
        Else
          ' On initial load, create the hashtable to store extent Envelopes or get session variable
          Dim extentsHashTable As System.Collections.Hashtable = Nothing
          If Not Session("extentHistory") Is Nothing Then
            extentsHashTable = CType(Session("extentHistory"), System.Collections.Hashtable)
          Else
            extentsHashTable = New System.Collections.Hashtable()
          End If

          ' Track the index of the current extent, if first extent the index is 0
          Dim currentExtentIndex As Integer = 0
          If Not Session("currentExtentIndex") Is Nothing Then
            currentExtentIndex = CInt(Fix(Session("currentExtentIndex")))
          Else
            Session("currentExtentIndex") = 0
          End If

          ' If current index is less than highest index in the extent history, continue
          Dim removeFromExtentIndex As Integer = 1
          If currentExtentIndex < (extentsHashTable.Count - 1) Then
            ' Since another event triggered the extent change (not Previous or Next)
            ' we need to delete the extents in front of the current index
            Dim i As Integer = extentsHashTable.Count - 1
            Do While i > currentExtentIndex
              extentsHashTable.Remove(i)
              removeFromExtentIndex += 1
              i -= 1
            Loop

            ' Disable NextExtent command
            Map1.CallbackResults.CopyFrom(RefreshToolbar("nextExtent", True))
          End If

          ' Add current extent to the hashtable, htExtents.Count is 0 for the first extent
          ' added to the array
          extentsHashTable.Add(extentsHashTable.Count, extentEventArgs.NewExtent)

          ' Extent index is a 0-based array
          Session("currentExtentIndex") = extentsHashTable.Count - 1

          ' Store extent Envelopes in an hashtable
          Session("extentHistory") = extentsHashTable

          ' Enable PreviousExtent command if number of extents is greater than 1
          If extentsHashTable.Count > 1 Then
            Map1.CallbackResults.CopyFrom(RefreshToolbar("previousExtent", False))
          End If
        End If
      Catch exception As System.Exception
        Dim errorCallbackResult As ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult = CustomToolLibrary.Utility.GetErrorCallback(exception)
        Map1.CallbackResults.Add(errorCallbackResult)
      End Try
    End If
  End Sub

  #End Region

  #Region "Instance Methods"

  Public Function RefreshToolbar(ByVal toolItemName As String, ByVal disabled As Boolean) As ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResultCollection Implements IBaseToolbarRefresh.RefreshToolbar
    Try
      ' Enable\disable toolbar item on the server
      Dim toolbarItemCollection As ESRI.ArcGIS.ADF.Web.UI.WebControls.ToolbarItemCollection = Toolbar2.ToolbarItems
      Dim command As ESRI.ArcGIS.ADF.Web.UI.WebControls.Command = Nothing
      For Each toolbarItem As ESRI.ArcGIS.ADF.Web.UI.WebControls.ToolbarItem In toolbarItemCollection
        If toolbarItem.Name = toolItemName Then
          command = CType(toolbarItem, ESRI.ArcGIS.ADF.Web.UI.WebControls.Command)
          If command.Disabled <> disabled Then
            If disabled Then
              command.Disabled = True
            Else
              command.Disabled = False
            End If

            Toolbar2.Refresh()

            ' Enable/disable toolbar item on the client (browser)
            Dim jsToolbarItemEnable As String = "var toolbar = Toolbars['" & Toolbar2.ClientID & "']; var toolbaritem = toolbar.items['" & command.Name & "'];toolbaritem.disabled = " & command.Disabled.ToString().ToLower() & ";"
            Dim enableToolbarItemCallbackResult As ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult = ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult.CreateJavaScript(jsToolbarItemEnable)
            Toolbar2.CallbackResults.Add(enableToolbarItemCallbackResult)
          End If
        End If
      Next toolbarItem
    Catch exception As System.Exception
      Dim errorCallbackResult As ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult = CustomToolLibrary.Utility.GetErrorCallback(exception)
      Toolbar2.CallbackResults.Add(errorCallbackResult)
    End Try

    Return Toolbar2.CallbackResults
  End Function

  #End Region
End Class