Common PostbackManager
Common_PostbackManager_CSharp\PostbackManagerWebSite\CreateGraphicElement.aspx.cs
// 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.
// 

public partial class CreateGraphicElement : System.Web.UI.Page 
{
    protected void Page_Load(object sender, System.EventArgs e)
    {
        // Wire the handler for the PostbackManager's RequestReceived event
        PostbackManager1.RequestReceived += new PostbackManager_CSharp.RequestReceivedEventHandler(PostbackManager1_RequestReceived);
    }

    void PostbackManager1_RequestReceived(object sender, PostbackManager_CSharp.AdfRequestEventArgs args)
    {
        // Get the request's arguments and check whether a new feature is to be created
        System.Collections.Specialized.NameValueCollection requestArgs =
            ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackUtility.ParseStringIntoNameValueCollection(args.RequestArguments);
        if (requestArgs["EventArg"] == "NewFeature")
        {
            ESRI.ArcGIS.ADF.Web.Geometry.Geometry adfGeometry = null;
            ESRI.ArcGIS.ADF.Web.Display.Symbol.Symbol adfSymbol = null;

            // Get the coordinates from the arguments
            string coordinates = requestArgs["Coordinates"];

            // Generate a random color for the graphic feature
            System.Random randomizer = new System.Random();
            System.Drawing.Color randomColor = System.Drawing.Color.FromArgb(
                randomizer.Next(0, 256), randomizer.Next(0, 256), randomizer.Next(0, 256));

            // Create the feature's geometry and symbol
            switch (requestArgs["Geometry"])
            {
                case "Point":
                    // Get the coordinates and create a point
                    string[] pointCoords = coordinates.Split(',');
                    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),
                            (ESRI.ArcGIS.ADF.Web.Display.Symbol.MarkerSymbolType)randomizer.Next(0, 5));
                    break;
                case "Polyline":
                    // Create a Path from the request's coordinates
                    ESRI.ArcGIS.ADF.Web.Geometry.Path adfPath =
                        new ESRI.ArcGIS.ADF.Web.Geometry.Path(coordinates, ',', ';');

                    // Encapsulate the path in a Polyline
                    ESRI.ArcGIS.ADF.Web.Geometry.Polyline adfPolyline = 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), (ESRI.ArcGIS.ADF.Web.Display.Symbol.LineType)randomizer.Next(0, 5));
                    break;
                case "Polygon":
                    // Create a Ring from the path's coordinates
                    ESRI.ArcGIS.ADF.Web.Geometry.Ring adfRing =
                        new ESRI.ArcGIS.ADF.Web.Geometry.Ring(coordinates, ',', ';');

                    // Encapsulate the Ring in a Polygon
                    ESRI.ArcGIS.ADF.Web.Geometry.Polygon adfPolygon = new ESRI.ArcGIS.ADF.Web.Geometry.Polygon();
                    adfPolygon.Rings.Add(adfRing);
                    adfGeometry = adfPolygon;

                    // Create a random color for the boundary
                    System.Drawing.Color randomBoundaryColor = 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), 
                        (ESRI.ArcGIS.ADF.Web.Display.Symbol.PolygonFillType)randomizer.Next(0, 10));
                    break;
            }

            // Get the functionality of the graphics resource that will hold the graphic elements
            ESRI.ArcGIS.ADF.Web.DataSources.Graphics.MapFunctionality graphicsMapFunctionality =
                Map1.GetFunctionality("GraphicsResource") as ESRI.ArcGIS.ADF.Web.DataSources.Graphics.MapFunctionality;

            // Attempt to retrieve the element graphics layer that will hold the graphic elements
            ESRI.ArcGIS.ADF.Web.Display.Graphics.ElementGraphicsLayer elementGraphicsLayer = null;
            elementGraphicsLayer = graphicsMapFunctionality.GraphicsDataSet.Tables["Element Graphics"]
                as ESRI.ArcGIS.ADF.Web.Display.Graphics.ElementGraphicsLayer;

            // Check whether the element graphics layer was found.  If not, create it.
            if (elementGraphicsLayer == null)
            {
                // 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);
            }

            // Create a graphic element with the user-drawn geometry and the random symbol and add it to the 
            // graphics layer
            ESRI.ArcGIS.ADF.Web.Display.Graphics.GraphicElement 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);
        }
    }
}