Common Context menu
Common_ContextMenu_CSharp\App_Code\SimpleTaskContextMenu_CSharp.cs
// Copyright 2011 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.
// 

using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using System.Drawing;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Collections.Specialized;
using ESRI.ArcGIS.ADF.Web.UI.WebControls;
using System.ComponentModel;

namespace SimpleTaskContextMenu
{
    [ToolboxData("<{0}:SimpleTaskContextMenu_CSharp runat=\"server\" Width=\"100px\" BackColor=\"White\" BorderColor=\"Blue\" BorderWidth=\"1px\"> </{0}:SimpleTaskContextMenu_CSharp>")]
    public class SimpleTaskContextMenu_CSharp : FloatingPanelTask
    {
        private TextBox textBox = null;
        private HtmlInputButton button = null;
        ContextMenu customContextMenuSimpleResult;
        TaskResults _taskResults = null;

        [Browsable(true)]
        [Category("Appearance")]
        [DefaultValue("Execute")]
        [PersistenceMode(PersistenceMode.Attribute)]
        public string ButtonText
        {
            get
            {
                object o = StateManager.GetProperty("buttonText");
                return (o == null) ? "Execute" : o as string;
            }
            set
            {
                StateManager.SetProperty("buttonText", value);
            }
        }

        private TaskResults TaskResultsInstance
        {
            get
            {
                if ((_taskResults == null) && (TaskResultsContainers[0] != null))
                    _taskResults = Utility.FindControl(TaskResultsContainers[0].Name, Page) as TaskResults;
                return _taskResults;
            }
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            TaskResultsInstance.NodeClicked += new TreeViewPlusNodeClickedEventHandler(TaskResults_NodeClicked);

        }

        // Handle an event on the TaskResults control
        void TaskResults_NodeClicked(object sender, TreeViewPlusNodeEventArgs args)
        {
            string nodeText = args.Node.Text;

            CallbackResult cr = new CallbackResult(null, null, "javascript",
                string.Format("window.status = 'Clicked on {0} during millisecond value of {1}'", nodeText, DateTime.Now.Millisecond));
            TaskResultsInstance.CallbackResults.Add(cr);
        }

        protected override void CreateChildControls()
        {
            Controls.Clear();
            base.CreateChildControls();

            textBox = new TextBox();
            textBox.ID = "textBox";

            button = new HtmlInputButton();
            button.ID = "button";
            button.Value = ButtonText;

            Controls.Add(textBox);
            Controls.Add(button);

            string getArgumentJS = string.Format
                ("'textBoxValue=' + document.getElementById('{0}').value",
                textBox.ClientID);
            string onClick = string.Format
                ("executeTask({0},\"{1}\");",
                getArgumentJS, CallbackFunctionString);
            button.Attributes.Add("onclick", onClick);

            // Setup simple custom context menu
            customContextMenuSimpleResult = new ContextMenu();
            customContextMenuSimpleResult.ID = "ccm";
            customContextMenuSimpleResult.BorderColor = System.Drawing.Color.Silver;
            customContextMenuSimpleResult.BorderStyle = BorderStyle.Solid;
            customContextMenuSimpleResult.BorderWidth = new Unit(1, UnitType.Pixel);
            customContextMenuSimpleResult.HoverColor = System.Drawing.Color.Gainsboro;
            customContextMenuSimpleResult.BackColor = System.Drawing.Color.White;
            customContextMenuSimpleResult.ForeColor = System.Drawing.Color.Black;
            customContextMenuSimpleResult.UseDefaultWebResources = true;
            System.Web.UI.WebControls.FontInfo fontinfo = customContextMenuSimpleResult.Font;
            fontinfo.Name = "Verdana";
            fontinfo.Size = new System.Web.UI.WebControls.FontUnit(8);

            customContextMenuSimpleResult.ItemClicked += new ESRI.ArcGIS.ADF.Web.UI.WebControls.ContextMenuItemClickedEventHandler(customContextMenuSimpleResult_ItemClicked);
            customContextMenuSimpleResult.Dismissed += new ContextMenuDismissedEventHandler(customContextMenuSimpleResult_Dismissed);

            ESRI.ArcGIS.ADF.Web.UI.WebControls.ContextMenuItem item =
     new ESRI.ArcGIS.ADF.Web.UI.WebControls.ContextMenuItem();
            item.Text = "Show Alert";
            item.ImageUrl = "";
            customContextMenuSimpleResult.Items.Add(item);

            ESRI.ArcGIS.ADF.Web.UI.WebControls.ContextMenuItem item1 =
new ESRI.ArcGIS.ADF.Web.UI.WebControls.ContextMenuItem();
            item1.Text = "Remove";
            item1.ImageUrl = "";
            customContextMenuSimpleResult.Items.Add(item1);

            Controls.Add(customContextMenuSimpleResult);

        }

        void customContextMenuSimpleResult_Dismissed(object sender, ContextMenuDismissedEventArgs args)
        {
            ESRI.ArcGIS.ADF.Web.UI.WebControls.TreeViewPlusNode node = TaskResultsInstance.Nodes.FindByNodeID(args.Context);
            if (node != null)
            {
                // Unselect node in TaskResults
                customContextMenuSimpleResult.CallbackResults.Add(new ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult(this, "javascript",
                    string.Format("var node=document.getElementById('{0}_textCell');if(node!=null){{node.style.backgroundColor='{1}';}}",
                    node.NodeID, System.Drawing.ColorTranslator.ToHtml(TaskResultsInstance.BackColor))));
            }
        }

        void customContextMenuSimpleResult_ItemClicked(object sender, ESRI.ArcGIS.ADF.Web.UI.WebControls.ContextMenuItemEventArgs args)
        {
            ESRI.ArcGIS.ADF.Web.UI.WebControls.TreeViewPlusNode node =
                TaskResultsInstance.Nodes.FindByNodeID(args.Context);

            switch (args.Item.Text)
            {
                case "Show Alert":
                    ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult cr =
                        new ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult(null, null, "javascript", string.Format
                        ("alert('Worker process identity: {0}')", System.Security.Principal.WindowsIdentity.GetCurrent().Name.Replace("\\", "\\\\")));
                    customContextMenuSimpleResult.CallbackResults.Add(cr);
                    break;
                case "Remove":
                    node.Parent.Nodes.Remove(node);
                    TaskResultsInstance.Refresh();
                    customContextMenuSimpleResult.CallbackResults.CopyFrom(TaskResultsInstance.CallbackResults);
                    break;
            }
        }

        public override string GetCallbackResult()
        {
            NameValueCollection keyValColl =
                CallbackUtility.ParseStringIntoNameValueCollection(_callbackArg);
            Input = keyValColl["textBoxValue"];
            return base.GetCallbackResult();
        }

        public override void ExecuteTask()
        {
            Results = null;
            if (Input == null) return;

            string textBoxValue = Input as string;

            ESRI.ArcGIS.ADF.Web.UI.WebControls.TaskResultNode trNode =
                new ESRI.ArcGIS.ADF.Web.UI.WebControls.TaskResultNode
                ("Heading: " + textBoxValue);
            trNode.Expanded = false;
            trNode.ClickBehavior = TreeViewPlusNodeClickBehavior.None;

            ESRI.ArcGIS.ADF.Web.UI.WebControls.TreeViewPlusNode tvpn =
                new ESRI.ArcGIS.ADF.Web.UI.WebControls.TreeViewPlusNode
                ("Detail: Server Time is " + DateTime.Now);

            TaskResultsInstance.SetupContextMenu(customContextMenuSimpleResult, tvpn);
            tvpn.ShowCheckBox = false;

            trNode.Nodes.Add(tvpn);
            tvpn.EnsureVisible();

            Results = trNode;
        }

        public override void Refresh()
        {
            string tmp = Input as string;
            if (!string.IsNullOrEmpty(tmp)) textBox.Text = tmp;
            base.Refresh();
        }

        public override List<GISResourceItemDependency> GetGISResourceItemDependencies()
        {
            List<GISResourceItemDependency> list = new List<GISResourceItemDependency>();
            return list;
        }
    }
}