Common Task results
Common_TaskResults_CSharp\TaskResultsWebSite\ContextMenu.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 ContextMenu : System.Web.UI.Page
{
    #region ASP.NET Page Life Cycle Event Handlers - Page_Init, Page_PreRender

    protected void Page_Init(object sender, System.EventArgs e)
    {
        // Add a handler to fire when an item on the custom context menu is selected
        ContextMenu1.ItemClicked += 
            new ESRI.ArcGIS.ADF.Web.UI.WebControls.ContextMenuItemClickedEventHandler(
            ContextMenu1_ItemClicked);

        // Add a handler to fire when the custom context menu is hidden.  This handler removes
        // the highlight from the task result node that was right-clicked to display the menu.
        ContextMenu1.Dismissed += 
            new ESRI.ArcGIS.ADF.Web.UI.WebControls.ContextMenuDismissedEventHandler(
            ContextMenu1_Dismissed);
    }

    protected void Page_PreRender(object sender, System.EventArgs e)
    {
        // Initialize the custom menu's appearance
        ContextMenu1.BorderColor = System.Drawing.Color.Silver;
        ContextMenu1.BorderStyle = System.Web.UI.WebControls.BorderStyle.Solid;
        ContextMenu1.BorderWidth = new System.Web.UI.WebControls.Unit(1, 
            System.Web.UI.WebControls.UnitType.Pixel);
        ContextMenu1.HoverColor = System.Drawing.Color.Gainsboro;
        ContextMenu1.BackColor = System.Drawing.Color.White;

        // Create a custom item and add it to the custom menu
        ESRI.ArcGIS.ADF.Web.UI.WebControls.ContextMenuItem showAlertContextMenuItem = 
            new ESRI.ArcGIS.ADF.Web.UI.WebControls.ContextMenuItem();
        showAlertContextMenuItem.Text = "Show Alert";
        showAlertContextMenuItem.ImageUrl = "images/alertlink.gif";
        ContextMenu1.Items.Add(showAlertContextMenuItem);

        // Add the Web ADF Remove item to the context menu
        ContextMenu1.Items.Add(TaskResults1.RemoveOnlyContextMenu.Items[0]);
    }

    #endregion

    #region Web ADF Control Event Handlers - Toolbar1_CommandClick, ContextMenu1_ItemClicked, ContextMenu1_Dismissed

    protected void Toolbar1_CommandClick(object sender, 
        ESRI.ArcGIS.ADF.Web.UI.WebControls.ToolbarCommandClickEventArgs toolbarCommandClickEventArgs)
    {
        // Check to make sure our custom command was clicked.  This is not necessary in this case
        // since there is only one item on the toolbar, but is included here for demonstration.
        switch (toolbarCommandClickEventArgs.CommandName)
        {
            case ("CreateTaskResult"):
                // Create a parent node for the result and assign it a remove-only context menu
                ESRI.ArcGIS.ADF.Web.UI.WebControls.TaskResultNode headingTaskResultNode =
                    new ESRI.ArcGIS.ADF.Web.UI.WebControls.TaskResultNode("Heading");
                TaskResults1.SetupContextMenu(TaskResults1.RemoveOnlyContextMenu, headingTaskResultNode);

                // Create a child node and assign it the custom context menu
                ESRI.ArcGIS.ADF.Web.UI.WebControls.TreeViewPlusNode detailTreeViewPlusNode =
                    new ESRI.ArcGIS.ADF.Web.UI.WebControls.TreeViewPlusNode("Detail");
                TaskResults1.SetupContextMenu(ContextMenu1, detailTreeViewPlusNode);

                // Add the child node to the parent and call EnsureVisible to make sure the parent node
                // is not collapsed.  Note that EnsureVisible must be called after the node is added.
                headingTaskResultNode.Nodes.Add(detailTreeViewPlusNode);
                detailTreeViewPlusNode.EnsureVisible();

                // Display the custom node by passing it to DisplayResults
                TaskResults1.DisplayResults(null, null, null, headingTaskResultNode);

                // Copy the TaskResults control's callback results to the Toolbar so they are processed
                // on the client
                Toolbar1.CallbackResults.CopyFrom(TaskResults1.CallbackResults);
                break;
        }
    }

    void ContextMenu1_ItemClicked(object sender, 
        ESRI.ArcGIS.ADF.Web.UI.WebControls.ContextMenuItemEventArgs contextMenuItemEventArgs)
    {
        // Get the node that was right-clicked to display the context menu
        ESRI.ArcGIS.ADF.Web.UI.WebControls.TreeViewPlusNode treeViewPlusNode = 
            TaskResults1.Nodes.FindByNodeID(contextMenuItemEventArgs.Context);

        // Check the text of the context menu item that was clicked
        switch (contextMenuItemEventArgs.Item.Text)
        {
            case "Show Alert":
                // Create a callback result that will show a JavaScript alert
                ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult callbackResult =
                    ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult.CreateJavaScript("alert('Hello')");
                ContextMenu1.CallbackResults.Add(callbackResult);
                break;
            case "Remove":
                // Remove the selected node from the tree view
                treeViewPlusNode.Parent.Nodes.Remove(treeViewPlusNode);
                TaskResults1.Refresh();
                ContextMenu1.CallbackResults.CopyFrom(TaskResults1.CallbackResults);
                break;
        }
    }

    void ContextMenu1_Dismissed(object sender, 
        ESRI.ArcGIS.ADF.Web.UI.WebControls.ContextMenuDismissedEventArgs args)
    {
        // Remove the highlight from the task results node that was right-clicked
        // to display the context menu
        TaskResults1.ContextMenuDismissed(ContextMenu1, args);
    }

    #endregion
}