Common Add dynamic data
Common_AddDynamicData_CSharp\DynamicDataWithContextMenu.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 DynamicDataWithContextMenu : System.Web.UI.Page, System.Web.UI.ICallbackEventHandler
{
    #region Instance Variable Declarations

    private System.Collections.Specialized.NameValueCollection m_NameValueCollection = null;
    private string m_CallbackArg = null;
    const string AGSLocalName = "AGSLocalMapResource";
    const string AGSInternetName = "AGSInternetMapResource";
    const string IMSName = "IMSMapResource";

    #endregion

    #region ASP.NET Page Event Handlers
    protected void Page_Init(object sender, System.EventArgs e)
    {
        // Assign functions to handle events on the custom context menus.  Handle the ItemClicked
        // event on the MapContextMenu and the ItemClicked and Dismissed events on the TocContextMenu.
        MapContextMenu.ItemClicked +=
            new ESRI.ArcGIS.ADF.Web.UI.WebControls.ContextMenuItemClickedEventHandler(MapContextMenu_ItemClicked);
        TocContextMenu.ItemClicked +=
            new ESRI.ArcGIS.ADF.Web.UI.WebControls.ContextMenuItemClickedEventHandler(TocContextMenu_ItemClicked);
        TocContextMenu.Dismissed +=
            new ESRI.ArcGIS.ADF.Web.UI.WebControls.ContextMenuDismissedEventHandler(TocContextMenu_Dismissed);

    }

    protected void Page_Load(object sender, System.EventArgs e)
    {
        // Create a string to store the JavaScript callback function for ASP.NET's callback framework
        string adfCallbackFunctionString =
            Page.ClientScript.GetCallbackEventReference(this, "argument", "processCallbackResult",
            "context", true);

        // Construct the JavaScript needed to get the ArcIMS service parameters from the ArcIMS
        // data source specification floating panel (ArcImsDataFloatingPanel), call the callback
        // function with those parameters, and hide the floating panel
        string jsAddArcIMSdata = string.Format("var argument='EventArg=AddIMS&host=' + " +
            "document.getElementById('imsHost').value + '&port=' + " +
            "document.getElementById('imsPort').value + '&service=' + " +
            "document.getElementById('imsService').value; var context=null; eval(\"{0}\"); " +
            "hideFloatingPanel('{1}',false);", adfCallbackFunctionString,
            ArcImsDataFloatingPanel.ClientID);
        // Wire the JavaScript to execute when the OK button on ArcImsDataFloatingPanel is clicked
        ImsOKButton.Attributes.Add("onclick", jsAddArcIMSdata);

        // Construct the JavaScript needed to get the display parameters from the display settings
        // floating panel (DisplaySettingsFloatingPanel), call the callback func                            tion with those
        // parameters, and hide the floating panel
        string jsChangeDisplaySettings = string.Format("var argument=" +
            "'EventArg=DisplaySettings&transparency=' + " +
            "document.getElementById('txtTransparency').value + '&transparentBackground=' + " +
            "document.getElementById('chkTransparentBackground').checked; var context=null; " +
            "eval(\"{0}\"); hideFloatingPanel('{1}',false);", adfCallbackFunctionString,
            DisplaySettingsFloatingPanel.ClientID);
        // Wire the JavaScript to execute when the OK button on DisplaySettingsFloatingPanel is clicked
        DisplaySettingsOKButton.Attributes.Add("onclick", jsChangeDisplaySettings);

        if (!Page.IsPostBack)
        {
            // Set up the custom context menu on the map control during initial page load
            SetupMapContextMenu();
        }
    }

    protected override void OnPreRenderComplete(System.EventArgs e)
    {
        base.OnPreRenderComplete(e);

        // Set up the custom context menu on the Toc.  This is done in PreRenderComplete
        // because the nodes in a Toc control are populated during the PreRender event. 
        SetupTocContextMenu();
    }

    #endregion

    #region Web ADF Control Event Handlers

    // Item clicked event for the custom context menu on the map control
    void MapContextMenu_ItemClicked(object sender,
        ESRI.ArcGIS.ADF.Web.UI.WebControls.ContextMenuItemEventArgs contextMenuItemEventArgs)
    {
        try
        {
            // Declare variables to store the name of the resource to be added and whether or not
            // a resource needs to be created and added
            string resourceName = "";
            bool createAndAddResource = false;

            // Check the text of the context menu item clicked and take action accordingly
            switch (contextMenuItemEventArgs.Item.Text)
            {
                case "Add ArcGIS Server Local Data":
                    // Set the resource name to the ArcGIS Server Local resource name
                    // and set the flag for creating and adding a resource to true
                    resourceName = AGSLocalName;
                    createAndAddResource = true;
                    break;
                case "Add ArcGIS Server Internet Data":
                    // Set the resource name to the ArcGIS Server Internet resource name
                    // and set the flag for creating and adding a resource to true
                    resourceName = AGSInternetName;
                    createAndAddResource = true;
                    break;
                case "Add ArcIMS Data":
                    // Set the resource name to the ArcIMS resource name and
                    // set the flag for creating and adding a resource to true
                    resourceName = IMSName;
                    createAndAddResource = true;
                    break;
                case "Add Custom ArcIMS Data":
                    // Construct the JavaScript necessary to show the ArcIMS data source specification
                    // floating panel (ArcImsDataFloatingPanel) and add it to the MapContextMenu's
                    // collection of callback results.  showFloatingPanel is the Web ADF JavaScript
                    // function to display floating panels.
                    string jsShowArcImsDataFloatingPanel = string.Format("showFloatingPanel('{0}',false);",
                        ArcImsDataFloatingPanel.ClientID);
                    MapContextMenu.CallbackResults.Add(this, "javascript", jsShowArcImsDataFloatingPanel);
                    break;
                case "Zoom In":
                    // Zoom the map in by a factor of two
                    Map1.Zoom(2);
                    break;
                case "Zoom To Full Extent":
                    // Zoom the map to its full extent
                    Map1.ZoomToFullExtent();
                    break;
                default:
                    break;
            }

            // Check whether the flag indicating a resource needs to be created and added is true
            if (createAndAddResource)
            {
                // Call CreateAndAddMapResource, which creates a map resource 
                // with the passed-in resource name and adds it to the map
                if (!CreateAndAddMapResource(resourceName))
                    return;

                // Refresh the Toc so the newly added map resource is shown
                Toc1.Refresh();

                // Set up the Toc's context menu again.  This needs to be done here
                // because the call to Refresh removes the association between the
                // context menu and the Toc.
                SetupTocContextMenu();
            }

            // Return the map's and Toc's callback results via the custom context menu.  
            // The callback results are returned this way because the context menu 
            // initiated the callback, and thus will handle the response.
            MapContextMenu.CallbackResults.CopyFrom(Map1.CallbackResults);

            // Refresh the html content of the Toc and add the result to the context
            // menu's callback results.  This will re-render the Toc with the context 
            // menu associations created in SetupTocContextMenu.
            MapContextMenu.CallbackResults.CopyFrom(Toc1.CallbackResults);
            //MapContextMenu.CallbackResults.Add(RefreshControlHtml(Toc1));
        }
        catch (System.Exception exception)
        {
            ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult errorCallbackResult =
                ErrorHandling.GetErrorCallback(exception);
            MapContextMenu.CallbackResults.Add(errorCallbackResult);
        }
    }

    // Item clicked event for parent nodes in a Toc control
    void TocContextMenu_ItemClicked(object sender,
        ESRI.ArcGIS.ADF.Web.UI.WebControls.ContextMenuItemEventArgs contextMenuItemEventArgs)
    {
        try
        {
            // Get the Toc node on which the context menu was displayed
            ESRI.ArcGIS.ADF.Web.UI.WebControls.TreeViewPlusNode treeViewPlusNode =
                Toc1.Nodes.FindByNodeID(contextMenuItemEventArgs.Context);
            // Make sure a node was found
            if (treeViewPlusNode != null)
            {
                // Declare an object variable to store a reference to a MapResourceItem
                ESRI.ArcGIS.ADF.Web.UI.WebControls.MapResourceItem mapResourceItem = null;
                int index = -1;

                // Check the text of the context menu item clicked and take action accordingly
                switch (contextMenuItemEventArgs.Item.Text)
                {
                    case "Zoom To Resource":
                        // Get a reference to the map resource indicated by the text of the Toc node
                        ESRI.ArcGIS.ADF.Web.DataSources.IMapResource mapResource =
                            Map1.MapResourceManagerInstance.GetResource(treeViewPlusNode.Text);
                        // Set the map's coordinate system to that of the map resource indicated by
                        // the Toc node.  This is done so that zooming to the full extent will work
                        // properly, even in cases where the units of the coordinate systems don't match.
                        Map1.SpatialReference.CoordinateSystem =
                            mapResource.MapInformation.DefaultSpatialReference.CoordinateSystem;
                        // Set the map extent to the full extent of the map resource
                        Map1.Extent = mapResource.MapInformation.FullExtent;
                        // Add the map's callback results to the context menu's callback results collection.
                        // This is done because the context menu initiated the callback, and will therefore
                        // handle the callback response.
                        TocContextMenu.CallbackResults.CopyFrom(Map1.CallbackResults);
                        break;
                    case "Remove Resource":
                        // Get a reference to the map resource item indicated by the text of the Toc node
                        mapResourceItem = MapResourceManager1.ResourceItems.Find(treeViewPlusNode.Text);
                        // Make sure the resource item was found
                        if (mapResourceItem != null)
                        {
                            // Remove the map resource item from the MapResourceManger
                            MapResourceManager1.ResourceItems.Remove(mapResourceItem);
                            // Add the map's callback results to the context menu's callback 
                            // results collection.
                            TocContextMenu.CallbackResults.CopyFrom(Map1.CallbackResults);
                            // Refresh the Toc so the removed map resource is also removed from the Toc
                            Toc1.Refresh();
                            // Set up the Toc's context menu again.  This needs to be done here
                            // because the call to Refresh removes the association between the
                            // context menu and the Toc.
                            SetupTocContextMenu();
                            // Refresh the html content of the Toc and add the result to the context
                            // menu's callback results.  This will re-render the Toc with the context 
                            // menu associations created in SetupTocContextMenu.
                            TocContextMenu.CallbackResults.Add(RefreshControlHtml(Toc1));
                            break;
                        }
                        break;
                    case "Move Down":
                        // Get a reference to the map resource item indicated by the text of the Toc node
                        mapResourceItem = MapResourceManager1.ResourceItems.Find(treeViewPlusNode.Text);
                        // Get the index of the map resource item. This specifies where the map resource
                        // is ordered in the map and Toc.
                        index = MapResourceManager1.ResourceItems.IndexOf(mapResourceItem);

                        // Make sure that the map resource item was found and that it is not already
                        // at the bottom of all the map resource items
                        if (mapResourceItem != null && index != MapResourceManager1.ResourceItems.Count - 1)
                        {
                            // Move the map resource item down one position
                            MapResourceManager1.ResourceItems.Move(index, index + 1);

                            // Add the map's callback results to the context menu's callback 
                            // results collection.
                            TocContextMenu.CallbackResults.CopyFrom(Map1.CallbackResults);
                            // Refresh the Toc so the removed map resource is also removed from the Toc
                            Toc1.Refresh();
                            // Set up the Toc's context menu again.  This needs to be done here
                            // because the call to Refresh removes the association between the
                            // context menu and the Toc.
                            SetupTocContextMenu();
                            // Refresh the html content of the Toc and add the result to the context
                            // menu's callback results.  This will re-render the Toc with the context 
                            // menu associations created in SetupTocContextMenu.
                            TocContextMenu.CallbackResults.Add(RefreshControlHtml(Toc1));
                            break;
                        }
                        break;
                    case "Move Up":
                        // Get a reference to the map resource item indicated by the text of the Toc node
                        mapResourceItem = MapResourceManager1.ResourceItems.Find(treeViewPlusNode.Text);
                        // Get the index of the map resource item. This specifies where the map resource
                        // is ordered in the map and Toc.
                        index = MapResourceManager1.ResourceItems.IndexOf(mapResourceItem);

                        // Make sure that the map resource item was found and that it is not already
                        // at the top of all the map resource items
                        if (mapResourceItem != null && index != 0)
                        {
                            // Move the map resource item up one position
                            MapResourceManager1.ResourceItems.Move(index, index - 1);

                            // Add the map's callback results to the context menu's callback 
                            // results collection.
                            TocContextMenu.CallbackResults.CopyFrom(Map1.CallbackResults);
                            // Refresh the Toc so the removed map resource is also removed from the Toc
                            Toc1.Refresh();
                            // Set up the Toc's context menu again.  This needs to be done here
                            // because the call to Refresh removes the association between the
                            // context menu and the Toc.
                            SetupTocContextMenu();
                            // Refresh the html content of the Toc and add the result to the context
                            // menu's callback results.  This will re-render the Toc with the context 
                            // menu associations created in SetupTocContextMenu.
                            TocContextMenu.CallbackResults.Add(RefreshControlHtml(Toc1));
                            break;
                        }
                        break;
                    case "Resource Display Settings":
                        // Construct the JavaScript necessary to show the display settings floating panel 
                        // (DisplaySettingsFloatingPanel) and add it to the TocContextMenu's collection 
                        // of callback results.  showFloatingPanel is the Web ADF JavaScript function to 
                        // display floating panels.
                        string jsShowDisplaySettingsPanel = string.Format("showFloatingPanel('{0}',false);",
                            DisplaySettingsFloatingPanel.ClientID);
                        TocContextMenu.CallbackResults.Add(this, "javascript", jsShowDisplaySettingsPanel);
                        // Store the text of the Toc node in a session variable.  This variable will be
                        // used to specify the resource the display settings will be applied to.
                        Session["displaySettingsResourceKey"] = treeViewPlusNode.Text;
                        break;
                    default:
                        break;
                }
            }
        }
        catch (System.Exception exception)
        {
            ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult errorCallbackResult =
                ErrorHandling.GetErrorCallback(exception);
            TocContextMenu.CallbackResults.Add(errorCallbackResult);
        }
    }

    // Dismissed event for context menu off a parent node in a Toc control
    void TocContextMenu_Dismissed(object sender,
        ESRI.ArcGIS.ADF.Web.UI.WebControls.ContextMenuDismissedEventArgs contextMenuDismissedEventArgs)
    {
        try
        {
            // Get a reference to the TocContextMenu via the sender argument
            ESRI.ArcGIS.ADF.Web.UI.WebControls.ContextMenu contextMenu =
                sender as ESRI.ArcGIS.ADF.Web.UI.WebControls.ContextMenu;

            // Make sure event occured within this Page
            if (contextMenuDismissedEventArgs.Control != this) return;

            // Get a reference to the Toc node that was right-clicked
            ESRI.ArcGIS.ADF.Web.UI.WebControls.TreeViewPlusNode treeViewPlusNode =
                Toc1.Nodes.FindByNodeID(contextMenuDismissedEventArgs.Context);

            // Make sure the node was found
            if (treeViewPlusNode != null)
            {
                // Construct the JavaScript necessary to set background color of the Toc node 
                // to that of the Toc (so it no longer appears selected)
                string jsSetTocNodeBackgroundColor =
                    string.Format("var node=document.getElementById('{0}');" +
                    "if(node!=null){{node.style.backgroundColor='{1}';}}",
                    treeViewPlusNode.NodeID + "_textCell",
                    System.Drawing.ColorTranslator.ToHtml(Toc1.BackColor));
                // Create a new CallbackResult object with the JavaScript and add it 
                // to the context menu's callback results collection.
                ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult setBackgroundCallbackResult =
                    ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult.CreateJavaScript(
                    jsSetTocNodeBackgroundColor);
                contextMenu.CallbackResults.Add(setBackgroundCallbackResult);
            }
        }
        catch (System.Exception exception)
        {
            ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult errorCallbackResult =
                ErrorHandling.GetErrorCallback(exception);
            TocContextMenu.CallbackResults.Add(errorCallbackResult);
        }
    }

    #endregion

    #region ICallbackEventHandler Members

    // The OK buttons on the "Add ArcIMS Data" and "Resource Display Settings" floating panels
    // were wired to invoke the JavaScript callback function in the Page_Load event.  So when either
    // of those buttons is clicked, a callback is initiated, causing RaiseCallbackEvent and
    // GetCallbackResult to be called.  We therefore add code to these methods to parse the arguments
    // passed in the callback and take action accordingly.
    public void RaiseCallbackEvent(string eventArgument)
    {
        // RaiseCallbackEvent fires first and ingests the callback string passed
        // from the client to the server.  Set a class-level variable to reference
        // the string so it can be accessed and handled in GetCallbackResult.
        m_CallbackArg = eventArgument;
    }

    public string GetCallbackResult()
    {
        // Create a new CallbackResultCollection object
        ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResultCollection callbackResultCollection =
            new ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResultCollection();

        try
        {
            // Use the Web ADF's callback parsing utility to parse the callback string
            // into its arguments and store it in a NameValueCollection object
            System.Collections.Specialized.NameValueCollection nameValueCollection =
                ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackUtility.ParseStringIntoNameValueCollection(m_CallbackArg);

            // Get the event argument from the collection of arguments
            string eventArg = nameValueCollection["EventArg"];

            // Declare a string variable to reference the name of the resource to be manipulated
            string resourceName = null;

            // Check the event argument and take action accordingly
            switch (eventArg)
            {
                case "AddIMS":
                    // Set a class-level NameValueCollection object to reference the collection
                    // of arguments.  This is done so the ArcIMS resource definition parameters
                    // can be accessed from the method CreateAndAddMapResource
                    m_NameValueCollection = nameValueCollection;

                    // Get the host and service from the collection of arguments
                    string host = nameValueCollection["host"];
                    string service = nameValueCollection["service"];

                    // Generate a random number between 0 and 1000
                    System.Random random = new System.Random();
                    int randomInt = random.Next(0, 1000);

                    // Create a unique name for the ArcIMS resource by concatenating the host,
                    // service, and random number
                    resourceName = string.Format("{0}-{1}-{2}", host, service, randomInt.ToString());

                    // Create the map resource specified and add it to the map
                    if (!CreateAndAddMapResource(resourceName))
                        return MapContextMenu.CallbackResults.ToString();

                    // Add the map's callback results to the custom callback results collection
                    callbackResultCollection.CopyFrom(Map1.CallbackResults);

                    // Refresh the Toc so the newly added map resource is also added to the Toc
                    Toc1.Refresh();

                    // Set up the Toc's context menu again.  This needs to be done here
                    // because the call to Refresh removes the association between the
                    // context menu and the Toc.
                    SetupTocContextMenu();

                    // Refresh the html content of the Toc and add the result to the context
                    // menu's callback results.  This will re-render the Toc with the context 
                    // menu associations created in SetupTocContextMenu.
                    callbackResultCollection.Add(RefreshControlHtml(Toc1));
                    break;
                case "DisplaySettings":
                    // Get the transparency input by the user from the collection of arguments
                    int transparency = System.Convert.ToInt32(nameValueCollection["transparency"]);

                    // Get whether or not the background transparency checkbox was checked from the
                    // collection of arguments
                    bool transparentBackground =
                        System.Convert.ToBoolean(nameValueCollection["transparentBackground"]);

                    // Get the resource name from the session variable that was set in the
                    // TocContextMenu's ItemClicked event and initialize a MapResourceItem
                    // based on the resource name.
                    resourceName = Session["displaySettingsResourceKey"] as string;
                    ESRI.ArcGIS.ADF.Web.UI.WebControls.MapResourceItem mapResourceItem =
                        MapResourceManager1.ResourceItems.Find(resourceName);

                    // Make sure a MapResourceItem was found
                    if (mapResourceItem != null)
                    {
                        // Instantiate a DisplaySettings object and initialize its properties
                        // based on the user input from the display settings floating panel
                        ESRI.ArcGIS.ADF.Web.DisplaySettings displaySettings =
                            new ESRI.ArcGIS.ADF.Web.DisplaySettings();
                        displaySettings.Transparency = transparency;
                        displaySettings.ImageDescriptor.TransparentColor = ColorPicker1.ChosenColor;
                        displaySettings.ImageDescriptor.TransparentBackground = transparentBackground;

                        // Pass the DisplaySettings object to the MapResourceItem to push the
                        // new settings to the map resource
                        mapResourceItem.DisplaySettings = displaySettings;
                        
                        Map1.RefreshResource(mapResourceItem.Name);
                    }

                    // Add the map's callback results to the custom callback results collection
                    callbackResultCollection.CopyFrom(Map1.CallbackResults);
                    break;
            }

            // Return the custom callback results collection for processing on the client
            return callbackResultCollection.ToString();
        }
        catch (System.Exception exception)
        {
            ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult errorCallbackResult =
                ErrorHandling.GetErrorCallback(exception);
            callbackResultCollection.Add(errorCallbackResult);
            return callbackResultCollection.ToString();
        }
    }

    #endregion

    #region Instance Methods

    #region Context Menu Setup Methods

    private void SetupMapContextMenu()
    {
        try
        {
            // Instantiate context menu items for zooming in, zooming to the full extent, adding
            // ArcIMS data, adding ArcGIS Server Local data, adding ArcGIS Server Internet ata, and 
            // adding user-defined ArcIMS Data.  For each, initialize the item's image and text.  
            // Once each item is instantiated, add it to the MapContextMenu
            MapContextMenu.Items.Clear();
            ESRI.ArcGIS.ADF.Web.UI.WebControls.ContextMenuItem item = null;
            item = new ESRI.ArcGIS.ADF.Web.UI.WebControls.ContextMenuItem("images/contextMenuZoomTo.png",
                "Zoom In", null);
            MapContextMenu.Items.Add(item);
            item = new ESRI.ArcGIS.ADF.Web.UI.WebControls.ContextMenuItem("images/ArcGIS-Globe-Service16.gif",
                "Zoom To Full Extent", null);
            MapContextMenu.Items.Add(item);
            item = new ESRI.ArcGIS.ADF.Web.UI.WebControls.ContextMenuItem("images/add-new-service_16x16.gif",
                "Add ArcIMS Data", null);
            MapContextMenu.Items.Add(item);
            item = new ESRI.ArcGIS.ADF.Web.UI.WebControls.ContextMenuItem("images/add-new-service_16x16.gif",
                "Add ArcGIS Server Local Data", null);
            MapContextMenu.Items.Add(item);
            item = new ESRI.ArcGIS.ADF.Web.UI.WebControls.ContextMenuItem("images/add-new-service_16x16.gif",
                "Add ArcGIS Server Internet Data", null);
            MapContextMenu.Items.Add(item);
            item = new ESRI.ArcGIS.ADF.Web.UI.WebControls.ContextMenuItem("images/add-folder_16x16.png",
                "Add Custom ArcIMS Data", null);
            MapContextMenu.Items.Add(item);

            // Construct a JavaScript call to a Web ADF function that shows a context menu.
            // The statement "return false;" must be included in this JavaScript to prevent 
            // the native browser context menu from displaying.
            string jsShowDataContextMenu = string.Format("esriShowContextMenu(event,'{0}','{1}','{2}');" +
                "return false;", MapContextMenu.ClientID, Map1.UniqueID, "");

            // MapDiv is the element id associated with a div in the page that contains the map.
            // Wire the context menu initialized above by adding the showDataContextMenu JavaScript
            // call to the div's oncontextmenu event
            MapDiv.Attributes.Add("oncontextmenu", jsShowDataContextMenu);
        }
        catch (System.Exception exception)
        {
            string jsErrorMessageAlert = string.Format("<script>{0}</script>",
                ErrorHandling.GetJavaScriptErrorString(exception));
            Response.Write(jsErrorMessageAlert);
        }
    }

    private void SetupTocContextMenu()
    {
        try
        {
            // Instantiate context menu items for zooming to a resource, removing a resource, moving
            // a resource down in display order, moving a resource up in display order, and modifying
            // a resource's display settings. For each, initialize the item's image and text.  Once
            // each item is instantiated, add it to the TocContextMenu
            ESRI.ArcGIS.ADF.Web.UI.WebControls.ContextMenuItem item = null;
            item = new ESRI.ArcGIS.ADF.Web.UI.WebControls.ContextMenuItem("images/contextMenuZoomTo.png",
                "Zoom To Resource", null);
            item = new ESRI.ArcGIS.ADF.Web.UI.WebControls.ContextMenuItem("images/delete.gif",
                "Remove Resource", null);
            TocContextMenu.Items.Add(item);
            item = new ESRI.ArcGIS.ADF.Web.UI.WebControls.ContextMenuItem("images/expand.png",
                "Move Down", null);
            TocContextMenu.Items.Add(item);
            item = new ESRI.ArcGIS.ADF.Web.UI.WebControls.ContextMenuItem("images/collapse.png",
                "Move Up", null);
            TocContextMenu.Items.Add(item);
            item = new ESRI.ArcGIS.ADF.Web.UI.WebControls.ContextMenuItem("images/properties_16x16.png",
                "Resource Display Settings", null);
            TocContextMenu.Items.Add(item);

            // Iterate through each parent node in the Toc and set the properties of each 
            // to show the custom context menu when right-clicked
            for (int i = 0; i < Toc1.Nodes.Count; i++)
            {
                // Get a reference to the current node and assign it to a TreeViewPlusNode object
                ESRI.ArcGIS.ADF.Web.UI.WebControls.TreeViewPlusNode treeViewPlusNode = Toc1.Nodes[i];

                // Set the ClickBehavior property of the node so nothing happens when the node is left-clicked
                treeViewPlusNode.ClickBehavior =
                    ESRI.ArcGIS.ADF.Web.UI.WebControls.TreeViewPlusNodeClickBehavior.None;

                // Construct a JavaScript call to a Web ADF JavaScript function that will show the Toc
                // context menu.  The statement "return false;" must be included in this JavaScript to
                // prevent the native browser context menu from displaying.
                string jsShowTocContextMenu = string.Format("esriShowContextMenu(event,'{0}','{1}','{2}');" +
                    "this.style.backgroundColor='{3}';return false;", TocContextMenu.ClientID,
                    UniqueID, treeViewPlusNode.NodeID,
                    System.Drawing.ColorTranslator.ToHtml(Toc1.SelectedColor));
                // Construct JavaScript to set the mouse cursor to look like an arrow
                string jsOnMouseOver = "this.style.cursor='arrow';";
                // Wire the JavaScript contained in jsOnMouseOver to the current node's onmouseover
                // event so that the cursor changes to an arrow when over the node.
                if (treeViewPlusNode.TextCellAttributes.ContainsKey("onmouseover"))
                    treeViewPlusNode.TextCellAttributes["onmouseover"] = jsOnMouseOver;
                else
                    treeViewPlusNode.TextCellAttributes.Add("onmouseover", jsOnMouseOver);

                // Wire the JavaScript to display the Toc context menu to the current node's
                // oncontextmenu event.
                if (treeViewPlusNode.TextCellAttributes.ContainsKey("oncontextmenu"))
                    treeViewPlusNode.TextCellAttributes["oncontextmenu"] = jsShowTocContextMenu;
                else
                    treeViewPlusNode.TextCellAttributes.Add("oncontextmenu", jsShowTocContextMenu);
                // Add a tooltip to the current node informing the user how to display the context menu
                treeViewPlusNode.ToolTip = "Right-click for context menu";
            }
        }
        catch (System.Exception exception)
        {
            string jsErrorMessageAlert = string.Format("<script>{0}</script>",
                ErrorHandling.GetJavaScriptErrorString(exception));
            Response.Write(jsErrorMessageAlert);
        }
    }

    #endregion

    private bool CreateAndAddMapResource(string resourceName)
    {
        bool addResourceSuccessful = false;
        try
        {
            // Map resource items consist of a definition and display settings.  The definition 
            // will define the data source and resource parameters.  Display settings will define
            // map image properties and retrieval types. 
            ESRI.ArcGIS.ADF.Web.UI.WebControls.GISResourceItemDefinition gisResourceItemDefinition =
                new ESRI.ArcGIS.ADF.Web.UI.WebControls.GISResourceItemDefinition();

            // Check the name of the resource corresponding to the checkbox checked and initialize
            // the resource definition accordingly
            switch (resourceName)
            {
                case (AGSLocalName):
                    gisResourceItemDefinition.DataSourceDefinition = "localhost";
                    gisResourceItemDefinition.DataSourceType = "ArcGIS Server Local";
                    gisResourceItemDefinition.ResourceDefinition = "Layers@USA";
                    
                    break;
                case (AGSInternetName):
                    gisResourceItemDefinition.DataSourceDefinition = "http://serverapps.esri.com/arcgis/services/";
                    gisResourceItemDefinition.DataSourceType = "ArcGIS Server Internet";
                    gisResourceItemDefinition.ResourceDefinition = "Layers@SamplesNet/NorthAmerica";
                    
                    break;
                case (IMSName):
                    gisResourceItemDefinition.ResourceDefinition = "states";
                    gisResourceItemDefinition.DataSourceDefinition = "localhost@5300";
                    gisResourceItemDefinition.DataSourceType = "ArcIMS";
                    
                    break;
                default:
                    // Since the passed-in resourceName didn't match any of the pre-defined constants,
                    // assume that the resource is a user-defined ArcIMS map resource.  Get the resource
                    // definition properties from the class level variable m_NameValueCollection, which
                    // was populated in the GetCallbackResult callback handling method
                    gisResourceItemDefinition.ResourceDefinition = m_NameValueCollection["service"];
                    gisResourceItemDefinition.DataSourceDefinition = string.Format("{0}@{1}",
                        m_NameValueCollection["host"], m_NameValueCollection["port"]);
                    gisResourceItemDefinition.DataSourceType = "ArcIMS";
                    
                    break;
            }

            // Instantiate a map resource item
            ESRI.ArcGIS.ADF.Web.UI.WebControls.MapResourceItem mapResourceItem =
                new ESRI.ArcGIS.ADF.Web.UI.WebControls.MapResourceItem();

            // Associate the resource item definition with the 
            // newly instantiated map resource item
            mapResourceItem.Definition = gisResourceItemDefinition;
            // Set the resource item's name to the passed-in resource name
            mapResourceItem.Name = resourceName;

            // Initialize display settings
            ESRI.ArcGIS.ADF.Web.DisplaySettings displaySettings = new ESRI.ArcGIS.ADF.Web.DisplaySettings();
            displaySettings.Visible = true;
            displaySettings.Transparency = 50.0F;
            displaySettings.ImageDescriptor.TransparentBackground = true;
            displaySettings.ImageDescriptor.TransparentColor = System.Drawing.Color.White;
            displaySettings.ImageDescriptor.ImageFormat = ESRI.ArcGIS.ADF.Web.ImageFormat.PNG8;
            displaySettings.ImageDescriptor.ReturnMimeData = false;

            // Associate the map resource with the display settings
            mapResourceItem.DisplaySettings = displaySettings;

            // Add the resource to the map's MapResourceManager and initialize it
            Map1.MapResourceManagerInstance.ResourceItems.Insert(0, mapResourceItem);
            mapResourceItem.InitializeResource();

            addResourceSuccessful = true;
        }
        catch (System.Exception exception)
        {
            ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult errorCallbackResult =
                ErrorHandling.GetErrorCallback(exception);
            MapContextMenu.CallbackResults.Add(errorCallbackResult);
        }

        return addResourceSuccessful;
    }

    // Utility function to refresh a web control's html and return it as a callback result
    private ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult RefreshControlHtml(
        System.Web.UI.Control webControl)
    {
        // Instantaite an HtmlTextWriter object
        System.IO.StringWriter stringWriter = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter htmlTextWriter = new System.Web.UI.HtmlTextWriter(stringWriter);

        // Render the passed-in control's html to the HtmlTextWriter
        webControl.RenderControl(htmlTextWriter);

        // Get the control's html as a string
        string htmlContent = stringWriter.ToString();

        // Close the StringWriter since we are done with it
        stringWriter.Close();

        // Instantiate and return a callback result with the control and the html.  Instantiating
        // a callback result with 3 arguments where "content" is the 2nd argument will create a
        // result instructing the out-of-the-box callback handler (ESRI.ADF.System.processCallbackResult)
        // to re-render the control specified by the 1st argument with the html contained in the 3rd
        // argument.
        return ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult.CreateSetContent(webControl, htmlContent);
    }

    #endregion
}