Common Extend tasks
Common_ExtendTasks_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
Public Class Utility
  Private Sub New()
  End Sub
  Friend Shared Function FindControl(ByVal controlID As String, ByVal page As System.Web.UI.Page) As System.Web.UI.Control
    ' Make sure the passed-in page and control are not null
    If page Is Nothing OrElse controlID Is Nothing Then
    Return Nothing
    End If

    ' Try getting a reference to the control being sought via the page's FindControl function
    Dim webControl As System.Web.UI.Control = page.FindControl(controlID)

    ' Check whether the control was found
    If webControl Is Nothing Then
      ' Call method to traverse the Page's controls and find the unique id of the control
      ' having the passed in control ID
      Dim uniqueControlID As String = GetControlUniqueID(controlID, page.Controls)
      If Not uniqueControlID Is Nothing Then
        webControl = page.FindControl(uniqueControlID)
      Else
        webControl = page.FindControl(controlID)
      End If
    End If
    Return webControl
  End Function

  Friend Shared Function GetControlUniqueID(ByVal controlID As String, ByVal controls As System.Web.UI.ControlCollection) As String
    ' Declare a Control object to store references to controls in the passed-in ControlCollection
    Dim control As System.Web.UI.Control
    ' Declare a string variable to store references to the UniqueIDs of controls in the passed-in
    ' collection
    Dim uniqueID As String = Nothing

    ' Iterate through the controls in the passed-in collection
    Dim i As Integer = 0
    Do While i < controls.Count
      ' Get a reference to the current control
      control = controls(i)

      ' Check whether the current control's ID matches the passed in control ID
      If control.ID = controlID Then
        ' The control's ID matches, so get a reference to its UniqueID and exit the loop
        uniqueID = control.UniqueID
        Exit Do
      End If

      ' Check whether the current control contains any child controls
      If control.Controls.Count > 0 Then
        ' Recursively call GetControlUniqueID with the passed-in control ID and the current
        ' control's collection of child controls
        uniqueID = GetControlUniqueID(controlID, control.Controls)
        ' Check whether the ID was found.  If so, exit the loop
        If Not uniqueID Is Nothing Then
          Exit Do
        End If
      End If
      i += 1
    Loop
    Return uniqueID
  End Function
End Class