ArcGIS ClipShip geoprocessing
ArcGIS_ClipShip_Geoprocessing_CSharp\Default.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 _Default : System.Web.UI.Page
{
    #region Instance Variable Declarations

    

    private ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResultCollection
        _callbackResultCollection = new ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResultCollection();
    
    // Names of the layers that will hold the clipped features and user-drawn polylines
    private string _clippedFeaturesLayerName = "Clip";
    private string _polylineGraphicsLayerName = "Polylines";
    
    // Name of the geoproccessing resource containing the ClipCounties task
    private string _geoprocessingResourceName = "Buffer and Clip Resource";

    // Name of the resource that will store clip results if _displayResultsInMapResource is
    // set to true
    private string _resultsMapResourceName = "Clip Results";
    
    // Name of the resource in which to store geoprocessing results (if 
    // _displayResultsInMapResource is false) and user-drawn polylines
    private string _graphicsResourceName = "Web ADF Graphics";

    // Determines whether to display the geoprocessing results in a map resource or as graphics
    private bool _displayResultsInMapResource = true;

    // Name of the GP tool layer in the map service.  This is the layer that was created in the
    // underlying mxd by dragging and dropping the GP tool into the mxd.  This name will be used
    // to remove the layer from the Toc.
    private string _toolLayerName = "ClipCounties";

    #endregion

    #region ASP.NET Page Life Cycle Event Handlers

    protected void Page_PreRender(object sender, System.EventArgs eventArgs)
    {
        try
        {
            // Initialize session variable only if the page request is occurring at the beginning of a session
            if (Page.Session.IsNewSession)
            {
                Session["graphicsResourceName"] = _graphicsResourceName;
                Session["polylineGraphicsLayerName"] = _polylineGraphicsLayerName;
            }

            // Make sure control initialization only occurs during initial page load or on page refresh
            if (!ScriptManager1.IsInAsyncPostBack)
            {
                // Add all the ArcGIS Server unit types to the units drop-down list except for unknown, points, 
                // and decimal degress
                System.Array agsUnitTypes = System.Enum.GetValues(typeof(ESRI.ArcGIS.ADF.Web.DataSources.Units));
                foreach (int agsUnitType in agsUnitTypes)
                {
                    string unit = agsUnitTypes.GetValue(agsUnitType).ToString();
                    if ((unit != "Unknown") && (unit != "DecimalDegrees") && (unit != "Points"))
                        unitsDropDownList.Items.Add(unit);
                }
            }
        }
        catch (System.Exception exception)
        {
            string jsErrorAlertString = string.Format("<script>{0}</script>",
                Utility.GetJavaScriptErrorString(exception));
            Response.Write(jsErrorAlertString);
        }
    }

    // Generates custom callback function string for non-ADF controls\elements in page that generate callbacks
    protected void Page_Load(object sender, System.EventArgs e)
    {
        // Register the buttons so they initiate asynchronous postbacks 
        ScriptManager1.RegisterAsyncPostBackControl(bufferButton);
        ScriptManager1.RegisterAsyncPostBackControl(ClearGraphicsButton);

        RemoveLayerFromToc(_toolLayerName);

        bufferButton.Click += new System.EventHandler(bufferButton_Click);
        ClearGraphicsButton.Click += new System.EventHandler(ClearGraphicsButton_Click);

        PostbackManager1.RequestReceived +=
            new PostbackManager_CSharp.RequestReceivedEventHandler(PostbackManager1_RequestReceived);

    }

    #endregion

    #region Request Handling

  void PostbackManager1_RequestReceived(object sender, PostbackManager_CSharp.AdfRequestEventArgs args)
  {
    // Use the Web ADF's callback utility to parse the arguments into a NameValueCollection
    System.Collections.Specialized.NameValueCollection requestArgs =
      ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackUtility.ParseStringIntoNameValueCollection(args.RequestArguments);

    if (requestArgs["EventArg"] == "CheckGeoprocessingJobStatus")
    {
      try
      {
        // Call the method to heck whether the geoprocssing job is finished and process results if so
                bool downloadoutput;
                CheckGeoprocessingJobStatus(requestArgs["JobID"], requestArgs["TaskName"], requestArgs["OutputParameters"], bool.TryParse(requestArgs["DownloadOutput"], out downloadoutput));
      }
      catch (System.Exception exception)
      {
        ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult errorCallbackResult =
          Utility.CreateErrorCallbackResult(exception);
        _callbackResultCollection.Add(errorCallbackResult);
      }
      finally
      {
        PostbackManager1.CallbackResults.CopyFrom(_callbackResultCollection);
      }
    }
  }

  #endregion
     
    #region ASP.NET Web Control Event Handlers

    protected void bufferButton_Click(object sender, System.EventArgs e)
    {
        try
        {
            // Get the buffer operation parameters specified by the user
            string bufferDistanceString = bufferDistanceTextBox.Text;
            double bufferDistance = 0;
            double.TryParse(bufferDistanceString, out bufferDistance);

            bool downloadOutput = downloadCheckBox.Checked;
            // Invoke the method to initiate the geoprocessing task
            StartBufferAndClipJob(bufferDistance, unitsDropDownList.SelectedValue, downloadOutput);

        }
        catch (System.Exception exception)
        {
            ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult errorCallbackResult =
                Utility.CreateErrorCallbackResult(exception);
            _callbackResultCollection.Add(errorCallbackResult);
        }
        finally
        {
            ScriptManager1.RegisterDataItem(Page, _callbackResultCollection.ToString(), false);
        }
    }

    protected void ClearGraphicsButton_Click(object sender, System.EventArgs e)
    {
        try
        {
            // Invoke the method to clear the graphics from the map
            ClearGraphics();
        }
        catch (System.Exception exception)
        {
            ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult errorCallbackResult =
                Utility.CreateErrorCallbackResult(exception);
            _callbackResultCollection.Add(errorCallbackResult);
        }
        finally
        {
            ScriptManager1.RegisterDataItem(Page, _callbackResultCollection.ToString(), false);
        }
    }
    #endregion

    #region Instance Methods

    // Initiates the buffer and clip geoprocessing job with the passed-in parameters
    private void StartBufferAndClipJob(double bufferDistance, string units, bool downloadOutput)
    {
        #region Initialize resources and functionalities

        // Initialize GP resource and functionality
        ESRI.ArcGIS.ADF.Web.UI.WebControls.GeoprocessingResourceItem geoprocessingResourceItem =
            GeoprocessingResourceManager1.ResourceItems.Find(_geoprocessingResourceName);

        // Make sure the geoprocessing resource is initialized
        if (!geoprocessingResourceItem.Resource.Initialized)
            geoprocessingResourceItem.InitializeResource();

        // Get a reference to the GP resource and its functionality
        ESRI.ArcGIS.ADF.Web.DataSources.IGeoprocessingResource geoprocessingResource =
            (ESRI.ArcGIS.ADF.Web.DataSources.IGeoprocessingResource)geoprocessingResourceItem.Resource;
       
        ESRI.ArcGIS.ADF.Web.DataSources.IGeoprocessingFunctionality commonGeoprocessingFunctionality =
            (ESRI.ArcGIS.ADF.Web.DataSources.IGeoprocessingFunctionality)
            geoprocessingResource.CreateFunctionality(typeof
            (ESRI.ArcGIS.ADF.Web.DataSources.IGeoprocessingFunctionality), null);

        ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.GeoprocessingFunctionality 
            agsGeoprocessingFunctionality = commonGeoprocessingFunctionality as
            ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.GeoprocessingFunctionality;

        // Make sure the geoprocessing functionality is initialized
        if (!agsGeoprocessingFunctionality.Initialized)
            agsGeoprocessingFunctionality.Initialize();

        #endregion

        #region Prepare inputs and start the GP task

        // Set the name of the geoprocessing (GP) task
        string GPTaskName = "ClipCounties";

        // Get an array of the GP task's parameters
        ESRI.ArcGIS.ADF.Web.DataSources.GPToolInfo adfGPToolInfo = 
            agsGeoprocessingFunctionality.GetTask(GPTaskName);
        ESRI.ArcGIS.ADF.Web.DataSources.GPParameterInfo[] adfGPParamterInfoArray = 
            adfGPToolInfo.ParameterInfo;

        // Get a reference to the first input parameter (a feature set) as a Web ADF feature graphics layer
        ESRI.ArcGIS.ADF.Web.DataSources.GPFeatureGraphicsLayer adfGPFeatureGraphicsLayer =
            (ESRI.ArcGIS.ADF.Web.DataSources.GPFeatureGraphicsLayer)adfGPParamterInfoArray[0].Value;
        ESRI.ArcGIS.ADF.Web.Display.Graphics.FeatureGraphicsLayer featureGraphicsLayer = 
            adfGPFeatureGraphicsLayer.Layer;

        // Get the graphics resource containing the layer that holds the user-drawn lines
        ESRI.ArcGIS.ADF.Web.DataSources.Graphics.MapResource adfGraphicsMapResource = 
            Map1.GetFunctionality(_graphicsResourceName).Resource as 
            ESRI.ArcGIS.ADF.Web.DataSources.Graphics.MapResource;

        // Get the graphics layer containing the user-drawn lines
        ESRI.ArcGIS.ADF.Web.Display.Graphics.ElementGraphicsLayer polylineElementGraphicsLayer =
            adfGraphicsMapResource.Graphics.Tables[_polylineGraphicsLayerName] as
            ESRI.ArcGIS.ADF.Web.Display.Graphics.ElementGraphicsLayer;

        // If there are no lines in the layer, alert the user and exit the function
        if ((polylineElementGraphicsLayer == null) || (polylineElementGraphicsLayer.Rows.Count < 1))
        {
            ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult noLinesAlertCallbackResult =
                ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult.CreateJavaScript(
                "alert('No lines to buffer')");
            _callbackResultCollection.Add(noLinesAlertCallbackResult);
            _callbackResultCollection.Add(HideActivityIndicators());
            return;
        }

        // Add each polyline to the feature graphics layer that we're using as input to the GP task
        foreach (System.Data.DataRow dataRow in polylineElementGraphicsLayer.Rows)
        {
            ESRI.ArcGIS.ADF.Web.Geometry.Polyline adfPolyline =
                polylineElementGraphicsLayer.GeometryFromRow(dataRow) as 
                ESRI.ArcGIS.ADF.Web.Geometry.Polyline;
            featureGraphicsLayer.Add(adfPolyline);
        }

        // Get the Web ADF Unit enumeration value corresponding to the user-defined unit 
        ESRI.ArcGIS.ADF.Web.DataSources.GPLinearUnit adfGPLinearUnit =
            new ESRI.ArcGIS.ADF.Web.DataSources.GPLinearUnit();
        ESRI.ArcGIS.ADF.Web.DataSources.Units adfUnits =
            (ESRI.ArcGIS.ADF.Web.DataSources.Units)System.Enum.Parse(typeof(
            ESRI.ArcGIS.ADF.Web.DataSources.Units), units, true);

        // Set the GP task's second input parameter (linear unit) using the user-defined
        // distance and units 
        adfGPLinearUnit.Units = adfUnits;
        adfGPLinearUnit.Value = bufferDistance;

        // Put the parameters in an input array and start the geoprocessing job
        ESRI.ArcGIS.ADF.Web.DataSources.GPValue[] adfGPValueArray =
            new ESRI.ArcGIS.ADF.Web.DataSources.GPValue[2];
        adfGPValueArray[0] = adfGPFeatureGraphicsLayer;
        adfGPValueArray[1] = adfGPLinearUnit;
        string jobID = agsGeoprocessingFunctionality.SubmitJob(GPTaskName, adfGPValueArray);

        #endregion

        #region Construct a callback to check the GP task's status

        // Get the output parameter names to use when retrieving geoprocessing job results
        string outputTaskParameters = null;
        ESRI.ArcGIS.ADF.Web.DataSources.GPParameterInfo agsGPParameterInfo;
        for (int i = 0; i < adfGPParamterInfoArray.Length; i++)
        {
            agsGPParameterInfo = adfGPParamterInfoArray[i];

            // Only append output parameters to the parameter string
            if (agsGPParameterInfo.Direction ==
                ESRI.ArcGIS.ADF.Web.DataSources.GPParameterDirection.Output)
            {
                // Only append the zip file parameter name if the user has checked the download
                // checkbox
                if (agsGPParameterInfo.Name == "output_zip" && !downloadOutput)
                    continue;

                outputTaskParameters += agsGPParameterInfo.Name;
                if (i != adfGPParamterInfoArray.Length - 1)
                    outputTaskParameters += ";";
            }
        }

        ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult checkJobStatusCallbackResult =
            GetStatusCheckJavaScript(GPTaskName, jobID, outputTaskParameters, downloadOutput.ToString(), 1000);

        // Add the GP job status check callback to the callback results collection
        _callbackResultCollection.Add(checkJobStatusCallbackResult);

        #endregion
    }

    // Check the GP job status for the job with the passed-in parameters and take action accordingly
    protected void CheckGeoprocessingJobStatus(string jobID, string taskName,
        string TaskOutputParameters, bool downloadOutput)
    {
        #region Initialize geoprocessing resources and functionalities

        // Initialize GP resource and functionality
        ESRI.ArcGIS.ADF.Web.UI.WebControls.GeoprocessingResourceItem geoprocessingResourceItem =
            GeoprocessingResourceManager1.ResourceItems.Find(_geoprocessingResourceName);

        // Make sure the geoprocessing resource is initialized
        if (!geoprocessingResourceItem.Resource.Initialized)
            geoprocessingResourceItem.InitializeResource();

        // Get a reference to the GP resource and its functionality
        ESRI.ArcGIS.ADF.Web.DataSources.IGeoprocessingResource geoprocessingResource =
            (ESRI.ArcGIS.ADF.Web.DataSources.IGeoprocessingResource)geoprocessingResourceItem.Resource;

        ESRI.ArcGIS.ADF.Web.DataSources.IGeoprocessingFunctionality commonGeoprocessingFunctionality =
            (ESRI.ArcGIS.ADF.Web.DataSources.IGeoprocessingFunctionality)
            geoprocessingResource.CreateFunctionality(typeof
            (ESRI.ArcGIS.ADF.Web.DataSources.IGeoprocessingFunctionality), null);

        ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.GeoprocessingFunctionality
            agsGeoprocessingFunctionality = commonGeoprocessingFunctionality as
            ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.GeoprocessingFunctionality;

        // Make sure the geoprocessing functionality is initialized
        if (!agsGeoprocessingFunctionality.Initialized)
            agsGeoprocessingFunctionality.Initialize();

        #endregion

        #region Construct callback to alert user if GP task failed or to check job status again if task is not complete

        // Get the GP job's status
        ESRI.ArcGIS.ADF.Web.DataSources.JobStatus adfJobStatus = agsGeoprocessingFunctionality.GetJobStatus(jobID);

        // If GP job failed, get job's last message and return it in an alert box
        if (adfJobStatus == ESRI.ArcGIS.ADF.Web.DataSources.JobStatus.Failed || 
            adfJobStatus == ESRI.ArcGIS.ADF.Web.DataSources.JobStatus.TimedOut)
        {
            // Get the GP job's messages
            ESRI.ArcGIS.ADF.Web.DataSources.JobMessage[] adfJobMessageArray = 
                agsGeoprocessingFunctionality.GetJobMessages(jobID);
            int messageCount = adfJobMessageArray.Length;

            // Create the alert javascript and package it in a callback
            string jsAlertGPError = string.Format("alert('GP job failed: {0})'", 
                adfJobMessageArray[messageCount - 1].MessageDesc);
            ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult gpErrorAlertCallbackResult =
                ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult.CreateJavaScript(jsAlertGPError);

            // Add the error alert to the callback results collection
            _callbackResultCollection.Add(gpErrorAlertCallbackResult);

            // Add a callback result to hide the operaton activity indicators to the callback results collection
            _callbackResultCollection.Add(HideActivityIndicators());
            return;
        }
        else if (adfJobStatus != ESRI.ArcGIS.ADF.Web.DataSources.JobStatus.Succeeded)
        {
            _callbackResultCollection.Add(GetStatusCheckJavaScript(taskName, jobID, TaskOutputParameters, downloadOutput.ToString(), 5000));
            return;
        }

        #endregion

        #region Process the GP task's results

        // Parse the GP task's output parameter names
        string[] outputParametersArray = TaskOutputParameters.Split(';');
        // Get the result of the GP task
        ESRI.ArcGIS.ADF.Web.DataSources.GPResult adfGPResult =
            agsGeoprocessingFunctionality.GetJobResult(taskName, jobID, outputParametersArray, 
            _displayResultsInMapResource);

        // Iterate through the GP task results.  If the result is a data file and the user
        // has checked the download results checkbox, add a callback that will enable 
        // downloading of this file.  If the result is a graphics layer or a map resource
        // definition, add the contents of these to the map so the results are displayed.
        ESRI.ArcGIS.ADF.Web.DataSources.GPValue[] adfGPValueArray = adfGPResult.Values;
        foreach (ESRI.ArcGIS.ADF.Web.DataSources.GPValue adfGPValue in adfGPValueArray)
        {
            if (adfGPValue is ESRI.ArcGIS.ADF.Web.DataSources.GPDataFile && downloadOutput)
            {
                #region Make the results available for download

                // Get the URL of the data file
                ESRI.ArcGIS.ADF.Web.DataSources.GPDataFile adfGPDataFile =
                    (ESRI.ArcGIS.ADF.Web.DataSources.GPDataFile)adfGPValue;
                string urlZipFileString = adfGPDataFile.Data.URL;

                // Construct JavaScript to insert an iframe into the page that has the data file url 
                // as its source.  The iframe will attempt to load whatever is referenced as its
                // source, which in this case will trigger a download dialog.
                string jsDynamicDownload = "var ifrm = document.createElement('IFRAME');" +
                    "ifrm.setAttribute('src','{0}');" +
                    "ifrm.style.width='0px';" +
                    "ifrm.style.height='0px';" +
                    "ifrm.style.visibility='hidden';" +
                    "document.body.appendChild(ifrm);";                
                jsDynamicDownload = string.Format(jsDynamicDownload, urlZipFileString);

                // Package the download-triggering JavaScript in a callback and add to the callback results
                ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult dynamicDownloadCallbackResult =
                    ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult.CreateJavaScript(jsDynamicDownload);
                _callbackResultCollection.Add(dynamicDownloadCallbackResult);

                #endregion
            }
            else if (adfGPValue is ESRI.ArcGIS.ADF.Web.DataSources.GPFeatureGraphicsLayer)
            {
                #region Add the clipped features to the map as graphics

                // The clip results will be returned as a feature graphics layer if 
                // _displayResultsInMapResource is set to false
                
                // Get output feature set containing buffers as a Web ADF feature graphics layer
                ESRI.ArcGIS.ADF.Web.DataSources.GPFeatureGraphicsLayer adfGPFeatureGraphicsLayer =
                    adfGPValue as ESRI.ArcGIS.ADF.Web.DataSources.GPFeatureGraphicsLayer;
                ESRI.ArcGIS.ADF.Web.Display.Graphics.FeatureGraphicsLayer featureGraphicsLayer =
                    adfGPFeatureGraphicsLayer.Layer;

                // If the graphics layer has no features, alert the user that the operation did not create 
                // any output
                if ((featureGraphicsLayer == null) || (featureGraphicsLayer.Rows.Count < 1))
                {
                    ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult noResultsAlertCallbackResult =
                        ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult.CreateJavaScript(
                        "alert('No results returned');");
                    _callbackResultCollection.Add(noResultsAlertCallbackResult);

                    // Add a callback result to hide the operation activity indicators and enable the
                    // Buffer and Clip button
                    _callbackResultCollection.Add(HideActivityIndicators());
                    return;
                }
                
                // Get the graphics layer's symbols and set their transparency to 50%
                System.Collections.Generic.List<ESRI.ArcGIS.ADF.Web.Display.Symbol.FeatureSymbol> 
                    featureSymbolList = new System.Collections.Generic.List<
                        ESRI.ArcGIS.ADF.Web.Display.Symbol.FeatureSymbol>();
                featureGraphicsLayer.Renderer.GetAllSymbols(featureSymbolList);
                foreach (ESRI.ArcGIS.ADF.Web.Display.Symbol.FeatureSymbol featureSymbol in featureSymbolList)
                    featureSymbol.Transparency = 50;

                // Set the feature graphics layer's name
                featureGraphicsLayer.TableName = _clippedFeaturesLayerName;

                // Get the graphics resource to put the results layer in
                ESRI.ArcGIS.ADF.Web.DataSources.Graphics.MapResource graphicsMapResource = 
                    Map1.GetFunctionality(_graphicsResourceName).Resource as 
                    ESRI.ArcGIS.ADF.Web.DataSources.Graphics.MapResource;

                // If the resource already has a results graphics layer, remove it
                if (graphicsMapResource.Graphics.Tables.Contains(_clippedFeaturesLayerName))
                    graphicsMapResource.Graphics.Tables.Remove(_clippedFeaturesLayerName);

                // Add new graphics layer to display new buffers
                graphicsMapResource.Graphics.Tables.Add(featureGraphicsLayer);

                // Refresh the graphics resource and copy the map's callback results to the callback collection
                // so the operation results are displayed
                Map1.RefreshResource(graphicsMapResource.Name);
                _callbackResultCollection.CopyFrom(Map1.CallbackResults);

                #endregion
            }
            else if (adfGPValue is ESRI.ArcGIS.ADF.Web.DataSources.GPMapResourceDefinition)
            {
                #region Add the clipped features to the map as features                
                
                // The clip results will be returned in a map resource definition if 
                // _displayResultsInMapResource is set to true.
                //
                // Note that the code here makes use of the map resource definition created from the
                // GP results by the Web ADF.  For more information on how to associate GP results 
                // and ArcGIS Server map resources, see the LayerDescription help.
                
                // Get the GP map resource definition 
                ESRI.ArcGIS.ADF.Web.DataSources.GPMapResourceDefinition gpMapResourceDefinition =
                    adfGPValue as ESRI.ArcGIS.ADF.Web.DataSources.GPMapResourceDefinition;

                // Check whether a results map resource item has already been added
                ESRI.ArcGIS.ADF.Web.UI.WebControls.MapResourceItem mapResourceItem =
                    MapResourceManager1.ResourceItems.Find(_resultsMapResourceName);
                if (mapResourceItem == null)
                {
                    // Create a new map resource item to hold the results since one doesn't yet exist

                    // Instantiate the map resource item with the name specified in the class member.
                    mapResourceItem = new ESRI.ArcGIS.ADF.Web.UI.WebControls.MapResourceItem();
                    mapResourceItem.Name = _resultsMapResourceName;

                    // Apply the GP results resource definition to the map resource item
                    mapResourceItem.Definition = new
                        ESRI.ArcGIS.ADF.Web.UI.WebControls.GISResourceItemDefinition(gpMapResourceDefinition.Value);

                    // Initialize the map resource item's display properties
                    ESRI.ArcGIS.ADF.Web.DisplaySettings displaySettings = new ESRI.ArcGIS.ADF.Web.DisplaySettings();
                    displaySettings.Transparency = 50;
                    displaySettings.Visible = true;
                    displaySettings.ImageDescriptor.TransparentColor = System.Drawing.Color.White;
                    displaySettings.ImageDescriptor.TransparentBackground = true;
                    mapResourceItem.DisplaySettings = displaySettings;

                    // Add the new map resource item to the resource manager
                    MapResourceManager1.ResourceItems.Insert(0, mapResourceItem);

                    // Refresh the Toc and copy its callback results to the callback results collection so the new
                    // resource has a Toc node
                    Toc1.Refresh();
                    _callbackResultCollection.CopyFrom(Toc1.CallbackResults);
                }
                else
                {
                    // Since the map resource item to hold the results has already been created, simply apply
                    // the definition returned with the GP results
                    mapResourceItem.Definition = new
                        ESRI.ArcGIS.ADF.Web.UI.WebControls.GISResourceItemDefinition(gpMapResourceDefinition.Value);
                }

                // Refresh the results resource and copy the map's callback results to the callback results 
                // collection so the result features are displayed on the map
                Map1.RefreshResource(_resultsMapResourceName);
                _callbackResultCollection.CopyFrom(Map1.CallbackResults);

                #endregion
            }
        }

        _callbackResultCollection.Add(HideActivityIndicators());

        #endregion
    }

    // Creates a callback result with the client-side code needed to hide the operation activity indicator
    // and enable the operation execution button
    private ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult HideActivityIndicators()
    {
        string jsHideIndicators = "toggleActivityIndicators(false);";
        ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult hideIndicatorsCallbackResult =
            ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult.CreateJavaScript(jsHideIndicators);
        return hideIndicatorsCallbackResult;
    }

    // Clears features from all graphics layers in the resource specified by _graphicsResourceName
    private void ClearGraphics()
    {
        // Retrieve the resource and clear its graphics dataset
        ESRI.ArcGIS.ADF.Web.DataSources.Graphics.MapResource graphicsMapResource =
            Map1.GetFunctionality(_graphicsResourceName).Resource as
            ESRI.ArcGIS.ADF.Web.DataSources.Graphics.MapResource;        
        graphicsMapResource.Graphics.Clear();

        // Refresh the resource and copy the map's callback results to the callback results collection so
        // the graphics are removed from the map
        Map1.RefreshResource(graphicsMapResource.Name);
        _callbackResultCollection.CopyFrom(Map1.CallbackResults);
    }


    // Removes the layer matching the input name from the TOC
    private void RemoveLayerFromToc(string layerName)
    {
        // Iterate through the nodes at the top level of the Toc.  Each of these should correspond to a resource.
        foreach (ESRI.ArcGIS.ADF.Web.UI.WebControls.TreeViewPlusNode resourceTreeViewPlusNode in
            Toc1.Nodes)
        {
            ESRI.ArcGIS.ADF.Web.UI.WebControls.TreeViewPlusNode treeViewPlusNodeToRemove = null;

            // Iterate through the nodes below the current top-level node.  Each of these should 
            // correspond to a layer.
            foreach (ESRI.ArcGIS.ADF.Web.UI.WebControls.TreeViewPlusNode layerTreeViewPlusNode in
                resourceTreeViewPlusNode.Nodes)
            {
                // Attempt to retrieve a node with text matching the passed-in layer name.  This could be
                // the current node or any node below it.  Note that we need to search both the current node
                // and any child nodes to handle group layers.
                ESRI.ArcGIS.ADF.Web.UI.WebControls.TreeViewPlusNode matchingTreeViewPlusNode =
                    Utility.FindNodeRecursive(layerTreeViewPlusNode, layerName);

                // If a matching node was found, store a reference to it to be accessed outside the loop
                if (matchingTreeViewPlusNode != null)
                {
                    treeViewPlusNodeToRemove = matchingTreeViewPlusNode;
                    break;
                }
            }

            // Remove the matching node, if found
            if (treeViewPlusNodeToRemove != null)
                treeViewPlusNodeToRemove.Remove();
        }
    }
  
    private ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult GetStatusCheckJavaScript(
        string GPTaskName, string jobID, string TaskOutputParameters, string downloadoutput, int timeout)
    {
        string arguments =
            string.Format("EventArg=CheckGeoprocessingJobStatus&JobID={0}&TaskName={1}&OutputParameters={2}&DownloadOutput={3}",
            jobID, GPTaskName, TaskOutputParameters, downloadoutput);
        // Construct a callback with the JavaScript needed to trigger a second callback after one second 
        // (1000 milliseconds) that will execute logic on the server to check the GP job's status and
        // take action accordingly
        string jsCheckJobStatus =
            "window.setTimeout(\"ESRI.ADF.Samples.PostbackManager.doAsyncRequest('{0}');\",{1});";
        jsCheckJobStatus = string.Format(jsCheckJobStatus, arguments, timeout);
        ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult checkJobStatusCallbackResult =
            ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult.CreateJavaScript(jsCheckJobStatus);
        return checkJobStatusCallbackResult;
    }
    
    #endregion
   
}