Common Context menu
Common_ContextMenu_VBNet\App_Code\Utility.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
Imports System.Data
Imports System.Configuration
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls

Public Class Utility
  Private Sub New()
  End Sub
  Public Shared Function ProcessMapToolError(ByVal message As String) As ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResultCollection
    ' Create a collection of callback results to 1) display an error message and 2) reset map cursor.
    Dim jsAlertException As String = "alert('" & message & "')"
    Dim alertCallbackResult As ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult = New ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult(Nothing, Nothing, "javascript", jsAlertException)

    Dim jsChangeCursor As String = "map.divObject.style.cursor = map.cursor"
    Dim cursorCallbackResult As ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult = New ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult(Nothing, Nothing, "javascript", jsChangeCursor)

    Dim callbackResultCollection As ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResultCollection = New ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResultCollection()
    callbackResultCollection.Add(alertCallbackResult)
    callbackResultCollection.Add(cursorCallbackResult)

    Return callbackResultCollection
  End Function

  Public Shared Function FindControl(ByVal control As String, ByVal page As System.Web.UI.Page) As System.Web.UI.Control
    Dim buddyControl As System.Web.UI.Control = page.FindControl(control)
    If buddyControl Is Nothing Then
      Dim webPartControl As String = GetControlUniqueID(control, page.Controls)
      If Not webPartControl Is Nothing Then
        buddyControl = page.FindControl(webPartControl)
      Else
        buddyControl = page.FindControl(control)
      End If
    End If
    Return buddyControl
  End Function

  Public Shared Function GetControlUniqueID(ByVal controlID As String, ByVal controls As System.Web.UI.ControlCollection) As String
    Dim control As System.Web.UI.Control
    Dim uniqueID As String = Nothing
    Dim i As Integer = 0
    Do While i < controls.Count
      control = controls(i)
      If control.ID = controlID Then
        uniqueID = control.UniqueID
        Exit Do
      End If
      If control.Controls.Count > 0 Then
        uniqueID = GetControlUniqueID(controlID, control.Controls)
        If Not uniqueID Is Nothing Then
          Exit Do
        End If
      End If
      i += 1
    Loop
    Return uniqueID
  End Function
End Class