Common Timer redraw
Common_TimerRedraw_VBNet\SimpleUpdate.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
Partial Public Class SimpleUpdate
  Inherits System.Web.UI.Page
  #Region "Member Variables"

  ' Name of the resource to add graphics to
  Private _graphicsResourceName As String = "GraphicsResource"

  ' Flag indicating whether one type of graphics is still visible when another type is cleared
  Private _graphicsShown As Boolean = False

  ' Array containing the names of the 48 contiguous states.  Used in creating feature graphics for a 
  ' random subset of these states.
  Private _states() As String = { "Alabama","Arizona","Arkansas","California","Colorado","Connecticut", "Delaware","Florida","Georgia","Idaho","Illinois","Indiana","Iowa","Kansas","Kentucky","Louisiana", "Maine","Maryland","Massachusetts","Michigan","Minnesota","Mississippi","Missouri","Montana", "Nebraska","Nevada","New Hampshire","New Jersey","New Mexico","New York","North Carolina", "North Dakota","Ohio","Oklahoma","Oregon","Pennsylvania","Rhode Island","South Carolina","South Dakota", "Tennessee","Texas","Utah","Vermont","Virginia","Washington","West Virginia","Wisconsin","Wyoming" }

  #End Region

  #Region "Event Handlers - Page_Load, RequestReceived"

  Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
    ' Add a handler for the PostbackManager's RequestReceived event.  This event fires whenever doAsyncRequest
    ' is called on on the client tier PostbackManager.  For more information on the PostbackManager, see the
    ' Common_PostbackManager sample.
    AddHandler PostbackManager1.RequestReceived, AddressOf PostbackManager1_RequestReceived
  End Sub

  ' Fires when a request is received that was initiated by a call to PostbackManager.doAsyncRequest on the client
    Private Sub PostbackManager1_RequestReceived(ByVal sender As Object, ByVal args As PostbackManager_VBNet.AdfRequestEventArgs)
        ' Parse the request arguments
        Dim requestArgs As System.Collections.Specialized.NameValueCollection = ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackUtility.ParseStringIntoNameValueCollection(args.RequestArguments)

        ' Check the event argument and draw or clear graphics accordingly
        Select Case requestArgs("EventArg")
            Case "DrawFeatureGraphics"
                DrawFeatureGraphics()
            Case "DrawElementGraphics"
                DrawElementGraphics()
            Case "DrawAllGraphics"
                DrawFeatureGraphics()
                DrawElementGraphics()
            Case "ClearFeatureGraphics"
                ClearGraphics("Feature")
            Case "ClearElementGraphics"
                ClearGraphics("Element")
        End Select

        ' Apply the graphics updates to the map
        Map1.RefreshResource(_graphicsResourceName)
        PostbackManager1.CallbackResults.CopyFrom(Map1.CallbackResults)

        ' Create a callback result to hide the activity indicator
        Dim hideIndicatorCallbackResult As ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult = ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult.CreateJavaScript("hideActivityIndicator();")
        PostbackManager1.CallbackResults.Add(hideIndicatorCallbackResult)

        ' Check whether graphics were created
        If requestArgs("EventArg").Contains("Draw") OrElse _graphicsShown Then
            ' Create callback result that will issue an update graphics request to the server in the update
            ' interval specified on the UI
            Dim updateGraphicsCallbackResult As ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult = ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult.CreateJavaScript("setGraphicsTimeout();")
            PostbackManager1.CallbackResults.Add(updateGraphicsCallbackResult)
        End If
    End Sub

  #End Region

  #Region "Private Methods"

  #Region "Graphics Manipulation - DrawFeatureGraphics, DrawElementGraphics, ClearGraphics"

  ' Draws a random subset of states as feature graphics on the map
  Private Sub DrawFeatureGraphics()
    ' Get the graphics resource
    Dim graphicsMapFunctionality As ESRI.ArcGIS.ADF.Web.DataSources.Graphics.MapFunctionality = TryCast(Map1.GetFunctionality(_graphicsResourceName), ESRI.ArcGIS.ADF.Web.DataSources.Graphics.MapFunctionality)

    ' Get a query functionality for the USA_Data resource
        Dim targetResourceName As String = "USA"
    Dim commonMapFunctionality As ESRI.ArcGIS.ADF.Web.DataSources.IMapFunctionality = Map1.GetFunctionality(targetResourceName)
    Dim queryFunctionality As ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality = CType(commonMapFunctionality.Resource.CreateFunctionality(GetType(ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality), Nothing), ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality)

    ' Get the names and IDs of the layers in the resource that can be queried
    Dim layerIDs() As String
    Dim layerNames() As String
    queryFunctionality.GetQueryableLayers(Nothing, layerIDs, layerNames)

    ' Get the index of the states layer
    Dim targetLayerName As String = "States"
    Dim targetLayerIndex As Integer = 0
    For i As Integer = 0 To layerNames.Length - 1
      If layerNames(i).ToLower() = targetLayerName.ToLower() Then
        targetLayerIndex = i
        Exit For
      End If
    Next i

    ' Initialize a filter for querying states
    Dim adfQueryFilter As New ESRI.ArcGIS.ADF.Web.QueryFilter()
    adfQueryFilter.ReturnADFGeometries = True
    adfQueryFilter.MaxRecords = 50

    ' Specify that only the STATE_NAME field be queried
    Dim targetFieldName As String = "STATE_NAME"
    Dim stringCollection As New ESRI.ArcGIS.ADF.StringCollection(targetFieldName, ","c)
    adfQueryFilter.SubFields = stringCollection

    Dim stringBuilder As New System.Text.StringBuilder()

    ' Generate the number of states to display graphics for
    Dim randomizer As New System.Random()
    Dim numberStates As Integer = randomizer.Next(4, 26)

    ' Get a state name from the list
    Dim stateName As String = _states(randomizer.Next(_states.Length))
    ' Add the number of unique state names specified by numberStates
    For i As Integer = 0 To numberStates - 1
      ' Get the list
      Dim stateList As String = stringBuilder.ToString()

      ' Keep picking random state names until one is picked that isn't on the list
      Do While stateList.Contains(stateName)
        stateName = _states(randomizer.Next(_states.Length))
      Loop

      ' Add the state to the list
      stringBuilder.AppendFormat("'{0}',", stateName)
    Next i

    ' Remove the trailing comma from the list
    Dim whereClause As String = stringBuilder.ToString()
    whereClause = whereClause.Substring(0, whereClause.Length - 1)

    ' Specify that the query filter get features that match the states in the list
    adfQueryFilter.WhereClause = String.Format("STATE_NAME IN ({0})", whereClause)

    ' Execute query
    Dim resultsTable As System.Data.DataTable = queryFunctionality.Query(commonMapFunctionality.Name, layerIDs(targetLayerIndex), adfQueryFilter)

    ' Convert results to a graphics layer and add to the map
    Dim resultsGraphicsLayer As ESRI.ArcGIS.ADF.Web.Display.Graphics.GraphicsLayer = ESRI.ArcGIS.ADF.Web.UI.WebControls.Converter.ToGraphicsLayer(resultsTable)
    Dim layerName As String = "Feature Graphics"
    resultsGraphicsLayer.TableName = layerName
    If graphicsMapFunctionality.GraphicsDataSet.Tables.Contains(layerName) Then
      graphicsMapFunctionality.GraphicsDataSet.Tables.Remove(layerName)
    End If
    graphicsMapFunctionality.GraphicsDataSet.Tables.Add(resultsTable)
  End Sub

  ' Draws a set of random element graphic points on the map
  Private Sub DrawElementGraphics()
    ' Get the map functionality for the graphics resource that will hold the element graphics
    Dim graphicsMapFunctionality As ESRI.ArcGIS.ADF.Web.DataSources.Graphics.MapFunctionality = TryCast(Map1.GetFunctionality(_graphicsResourceName), ESRI.ArcGIS.ADF.Web.DataSources.Graphics.MapFunctionality)

    ' Retrieve or create the element graphics layer
    Dim layerName As String = "Element Graphics"
    Dim elementGraphicsLayer As ESRI.ArcGIS.ADF.Web.Display.Graphics.ElementGraphicsLayer = Nothing
    If graphicsMapFunctionality.GraphicsDataSet.Tables.Contains(layerName) Then
      elementGraphicsLayer = TryCast(graphicsMapFunctionality.GraphicsDataSet.Tables(layerName), ESRI.ArcGIS.ADF.Web.Display.Graphics.ElementGraphicsLayer)

      ' Remove graphics from the layer
      elementGraphicsLayer.Clear()
    Else
      ' Create a new element graphics layer and add it to the map
      elementGraphicsLayer = New ESRI.ArcGIS.ADF.Web.Display.Graphics.ElementGraphicsLayer()
      elementGraphicsLayer.TableName = layerName
      graphicsMapFunctionality.GraphicsDataSet.Tables.Add(elementGraphicsLayer)
    End If

    ' Add a random number of points to add to the layer
    Dim randomizer As New System.Random()
    Dim numberPoints As Integer = randomizer.Next(4, 20)
    For i As Integer = 0 To numberPoints - 1
      ' Get a point that is randomly placed within the map's extent
      Dim adfPoint As ESRI.ArcGIS.ADF.Web.Geometry.Point = GetRandomPoint(Map1.Extent, randomizer)

      ' Create a marker symbol with a random color, size, and symbol type
      Dim markerSymbol As New ESRI.ArcGIS.ADF.Web.Display.Symbol.SimpleMarkerSymbol()
      markerSymbol.Color = GetRandomColor(randomizer)
      markerSymbol.Width = randomizer.Next(10, 30)
      markerSymbol.Type = CType(randomizer.Next(0, 5), ESRI.ArcGIS.ADF.Web.Display.Symbol.MarkerSymbolType)

      ' Use the point and symbol to create a graphic element
      Dim graphicElement As New ESRI.ArcGIS.ADF.Web.Display.Graphics.GraphicElement(adfPoint, markerSymbol)

      ' Add the graphic to the layer
      elementGraphicsLayer.Add(graphicElement)
    Next i
  End Sub

  ' Removes element or feature graphics from the map
  Private Sub ClearGraphics(ByVal featureType As String)
    ' Get the graphics resource
    Dim graphicsMapFunctionality As ESRI.ArcGIS.ADF.Web.DataSources.Graphics.MapFunctionality = TryCast(Map1.GetFunctionality(_graphicsResourceName), ESRI.ArcGIS.ADF.Web.DataSources.Graphics.MapFunctionality)

    ' Remove the element or feature graphics layer from the resource
    Dim graphicsLayerName As String = String.Format("{0} Graphics", featureType)
    If graphicsMapFunctionality.GraphicsDataSet.Tables.Contains(graphicsLayerName) Then
      graphicsMapFunctionality.GraphicsDataSet.Tables.Remove(graphicsLayerName)
    End If

    ' Set flag indicating whether there are still graphics visible.  This will be used to determine whether
    ' to create a callback to set a new graphics update timeout.
    _graphicsShown = (graphicsMapFunctionality.GraphicsDataSet.Tables.Count > 0)
  End Sub

  #End Region

  #Region "Random Helper Methods - GetRandomColor, GetRandomPoint"

  ' Generates a random color
  Private Function GetRandomColor(ByVal randomizer As System.Random) As System.Drawing.Color
    Dim rgb(2) As Byte
    randomizer.NextBytes(rgb)
    Return System.Drawing.Color.FromArgb(rgb(0), rgb(1), rgb(2))
  End Function

  ' Generates a random point within the given extent
  Private Function GetRandomPoint(ByVal adfEnvelope As ESRI.ArcGIS.ADF.Web.Geometry.Envelope, ByVal randomizer As System.Random) As ESRI.ArcGIS.ADF.Web.Geometry.Point
    Dim x As Double = adfEnvelope.XMin + randomizer.NextDouble() * adfEnvelope.Width
    Dim y As Double = adfEnvelope.YMin + randomizer.NextDouble() * adfEnvelope.Height
    Return New ESRI.ArcGIS.ADF.Web.Geometry.Point(x, y)
  End Function

  #End Region

  #End Region
End Class