Common Context menu
Common_ContextMenu_CSharp\SimpleMap.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 SimpleMap : System.Web.UI.Page
{
    #region ASP.NET Page Life Cycle Event Handlers

    protected void Page_Init(object sender, System.EventArgs eventArgs)
    {
        // Add an event handler for when an item is clicked on the context menu 
        ContextMenu1.ItemClicked += new ESRI.ArcGIS.ADF.Web.UI.WebControls.ContextMenuItemClickedEventHandler(ContextMenu1_ItemClicked);
    }

    protected void Page_PreRender(object sender, System.EventArgs eventArgs)
    {
        // Make sure the event is not being fired as a result of a PostBack (i.e. is being fired as part of initial page load)
        if (!IsPostBack)
        {
            // Make sure the context menu is clear of all items
            ContextMenu1.Items.Clear();

            // Create a new context menu item and add it to the menu
            ESRI.ArcGIS.ADF.Web.UI.WebControls.ContextMenuItem zoomInContextMenuItem = 
                new ESRI.ArcGIS.ADF.Web.UI.WebControls.ContextMenuItem();
            zoomInContextMenuItem.Text = "Zoom In";
            zoomInContextMenuItem.ImageUrl = "images/wrench.gif";
            ContextMenu1.Items.Add(zoomInContextMenuItem);

            // Create a JavaScript call to a Web ADF function that shows a context menu.  We return false after making 
            // the function call to prevent the default context menu from showing
            string showDataContextMenu = string.Format("esriShowContextMenu(event,'{0}','{1}','{2}');return false;", 
                ContextMenu1.ClientID, Map1.UniqueID, "");

            // Wire the JavaScript call to execute when the map control's oncontextmenu event is fired
            Map1.Attributes.Add("oncontextmenu", showDataContextMenu);
        }
    }

    #endregion

    #region Web ADF Control Event Handlers

    void ContextMenu1_ItemClicked(object sender, 
        ESRI.ArcGIS.ADF.Web.UI.WebControls.ContextMenuItemEventArgs contextMenuItemEventArgs)
    {
        // Check the text of the context menu item clicked
        if (contextMenuItemEventArgs.Item.Text == "Zoom In")
        {
            // Since the custom Zoom In menu item was clicked, call the map's Zoom function
            Map1.Zoom(2);
            // Since the context menu initiated the callback, the context menu's callback results are processed
            // when the callback returns to the client.  But since we executed Zoom on the map, it is that control
            // that has callback results.  So to have the map's callback results processed on the client, we copy
            // them to the context menu's callback results collection.
            ContextMenu1.CallbackResults.CopyFrom(Map1.CallbackResults);
        }
    }

    #endregion
}