Common PostbackManager
Common_PostbackManager_VBNet\PostbackManagerWebSite\CreateGraphicElement.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 CreateGraphicElement
  Inherits System.Web.UI.Page
  Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
    ' Wire the handler for the PostbackManager's RequestReceived event
    AddHandler PostbackManager1.RequestReceived, AddressOf PostbackManager1_RequestReceived
  End Sub

  Private Sub PostbackManager1_RequestReceived(ByVal sender As Object, ByVal args As PostbackManager_VBNet.AdfRequestEventArgs)
    ' Get the request's arguments and check whether a new feature is to be created
    Dim requestArgs As System.Collections.Specialized.NameValueCollection = ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackUtility.ParseStringIntoNameValueCollection(args.RequestArguments)
    If requestArgs("EventArg") = "NewFeature" Then
      Dim adfGeometry As ESRI.ArcGIS.ADF.Web.Geometry.Geometry = Nothing
      Dim adfSymbol As ESRI.ArcGIS.ADF.Web.Display.Symbol.Symbol = Nothing

      ' Get the coordinates from the arguments
      Dim coordinates As String = requestArgs("Coordinates")

      ' Generate a random color for the graphic feature
      Dim randomizer As System.Random = New System.Random()
      Dim randomColor As System.Drawing.Color = System.Drawing.Color.FromArgb(randomizer.Next(0, 256), randomizer.Next(0, 256), randomizer.Next(0, 256))

      ' Create the feature's geometry and symbol
      Select Case requestArgs("Geometry")
        Case "Point"
          ' Get the coordinates and create a point
          Dim pointCoords As String() = coordinates.Split(","c)
          adfGeometry = New ESRI.ArcGIS.ADF.Web.Geometry.Point(Double.Parse(pointCoords(0)), Double.Parse(pointCoords(1)))

          ' Create a marker symbol with a random color, size, and symbol type
          adfSymbol = New ESRI.ArcGIS.ADF.Web.Display.Symbol.SimpleMarkerSymbol(randomColor, randomizer.Next(10, 25), CType(randomizer.Next(0, 5), ESRI.ArcGIS.ADF.Web.Display.Symbol.MarkerSymbolType))
        Case "Polyline"
          ' Create a Path from the request's coordinates
          Dim adfPath As ESRI.ArcGIS.ADF.Web.Geometry.Path = New ESRI.ArcGIS.ADF.Web.Geometry.Path(coordinates, ","c, ";"c)

          ' Encapsulate the path in a Polyline
          Dim adfPolyline As ESRI.ArcGIS.ADF.Web.Geometry.Polyline = New ESRI.ArcGIS.ADF.Web.Geometry.Polyline()
          adfPolyline.Paths.Add(adfPath)
          adfGeometry = adfPolyline

          ' Create a line symbol with a random color, width, and line type
          adfSymbol = New ESRI.ArcGIS.ADF.Web.Display.Symbol.SimpleLineSymbol(randomColor, randomizer.Next(1, 5), CType(randomizer.Next(0, 5), ESRI.ArcGIS.ADF.Web.Display.Symbol.LineType))
        Case "Polygon"
          ' Create a Ring from the path's coordinates
          Dim adfRing As ESRI.ArcGIS.ADF.Web.Geometry.Ring = New ESRI.ArcGIS.ADF.Web.Geometry.Ring(coordinates, ","c, ";"c)

          ' Encapsulate the Ring in a Polygon
          Dim adfPolygon As ESRI.ArcGIS.ADF.Web.Geometry.Polygon = New ESRI.ArcGIS.ADF.Web.Geometry.Polygon()
          adfPolygon.Rings.Add(adfRing)
          adfGeometry = adfPolygon

          ' Create a random color for the boundary
          Dim randomBoundaryColor As System.Drawing.Color = System.Drawing.Color.FromArgb(randomizer.Next(0, 256), randomizer.Next(0, 256), randomizer.Next(0, 256))

          ' Create a fill symbol with a random fill color, boundary color, transparency, and fill type
          adfSymbol = New ESRI.ArcGIS.ADF.Web.Display.Symbol.SimpleFillSymbol(randomColor, randomBoundaryColor, randomizer.Next(0, 75), randomizer.Next(0, 75), CType(randomizer.Next(0, 10), ESRI.ArcGIS.ADF.Web.Display.Symbol.PolygonFillType))
      End Select

      ' Get the functionality of the graphics resource that will hold the graphic elements
      Dim graphicsMapFunctionality As ESRI.ArcGIS.ADF.Web.DataSources.Graphics.MapFunctionality = TryCast(Map1.GetFunctionality("GraphicsResource"), ESRI.ArcGIS.ADF.Web.DataSources.Graphics.MapFunctionality)

      ' Attempt to retrieve the element graphics layer that will hold the graphic elements
      Dim elementGraphicsLayer As ESRI.ArcGIS.ADF.Web.Display.Graphics.ElementGraphicsLayer = Nothing
      elementGraphicsLayer = TryCast(graphicsMapFunctionality.GraphicsDataSet.Tables("Element Graphics"), ESRI.ArcGIS.ADF.Web.Display.Graphics.ElementGraphicsLayer)

      ' Check whether the element graphics layer was found.  If not, create it.
      If elementGraphicsLayer Is Nothing Then
        ' Create a new ElementGraphicsLayer and add it to the graphics resource
        elementGraphicsLayer = New ESRI.ArcGIS.ADF.Web.Display.Graphics.ElementGraphicsLayer()
        elementGraphicsLayer.TableName = "Element Graphics"
        graphicsMapFunctionality.GraphicsDataSet.Tables.Add(elementGraphicsLayer)
      End If

      ' Create a graphic element with the user-drawn geometry and the random symbol and add it to the 
      ' graphics layer
      Dim graphicElement As ESRI.ArcGIS.ADF.Web.Display.Graphics.GraphicElement = New ESRI.ArcGIS.ADF.Web.Display.Graphics.GraphicElement(adfGeometry, adfSymbol)
      elementGraphicsLayer.Add(graphicElement)

      ' Refresh the resource and copy the map's callback results to the PostbackManager so the resource
      ' updates are processed on the client
      Map1.RefreshResource("GraphicsResource")
      PostbackManager1.CallbackResults.CopyFrom(Map1.CallbackResults)
    End If
  End Sub
End Class