Common_ContextMenu_CSharp\SimpleToc.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 SimpleToc : 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 the an item on the context menu is clicked ContextMenu1.ItemClicked += new ESRI.ArcGIS.ADF.Web.UI.WebControls.ContextMenuItemClickedEventHandler(ContextMenu1_ItemClicked); } protected void Page_Load(object sender, System.EventArgs eventArgs) { // Dynamically create a div element and add it to the page System.Web.UI.HtmlControls.HtmlGenericControl htmlDiv = new System.Web.UI.HtmlControls.HtmlGenericControl("div"); htmlDiv.ID = "fieldDiv"; Page.Form.Controls.Add(htmlDiv); } protected void Page_PreRenderComplete(object sender, System.EventArgs eventArgs) { if (!IsPostBack) { // Create a new context menu item and add it to the context menu ESRI.ArcGIS.ADF.Web.UI.WebControls.ContextMenuItem showFieldsContextMenuItem = new ESRI.ArcGIS.ADF.Web.UI.WebControls.ContextMenuItem(); showFieldsContextMenuItem.Text = "Show Fields"; showFieldsContextMenuItem.ImageUrl = "images/wrench.gif"; ContextMenu1.Items.Add(showFieldsContextMenuItem); // Get a map functionality object from the map. ESRI.ArcGIS.ADF.Web.DataSources.IMapFunctionality commonMapFunctionality = null; foreach (ESRI.ArcGIS.ADF.Web.DataSources.IGISFunctionality gisFunctionality in Map1.GetFunctionalities()) { if (gisFunctionality is ESRI.ArcGIS.ADF.Web.DataSources.IMapFunctionality) { commonMapFunctionality = gisFunctionality as ESRI.ArcGIS.ADF.Web.DataSources.IMapFunctionality; break; } } // Initialize the nodes corresponding to each MapTocFunctionality object foreach (ESRI.ArcGIS.ADF.Web.DataSources.IMapTocFunctionality mapTocFunctionality in Toc1.GetFunctionalities()) { // Get the Toc data frames referenced by the current MapTocFunctionality ESRI.ArcGIS.ADF.Web.TocDataFrame[] tocDataFrameArray = mapTocFunctionality.GetMapContents(commonMapFunctionality.Name, Map1.ImageFormat, Map1.UseMimeData, false); // Initialize the nodes of each TocDataFrame in the array for (int i = 0; i < tocDataFrameArray.Length; i++) { // Find the resource-level node corresponding to the current MapTocFunctionality's resource ESRI.ArcGIS.ADF.Web.UI.WebControls.TreeViewPlusNodeCollection layersTreeViewPlusNodeCollection = null; foreach (ESRI.ArcGIS.ADF.Web.UI.WebControls.TreeViewPlusNode resourceTreeViewPlusNode in Toc1.Nodes) { if (resourceTreeViewPlusNode.Text == mapTocFunctionality.Resource.Name) { // Get the layer nodes from the resource node layersTreeViewPlusNodeCollection = resourceTreeViewPlusNode.Nodes; break; } } // Set the client-side events for each layer node foreach (ESRI.ArcGIS.ADF.Web.UI.WebControls.TreeViewPlusNode layerTreeViewPlusNode in layersTreeViewPlusNodeCollection) { // Set the layer node to do nothing when clicked layerTreeViewPlusNode.ClickBehavior = ESRI.ArcGIS.ADF.Web.UI.WebControls.TreeViewPlusNodeClickBehavior.None; // Construct a JavaScript call to the Web ADF function that displays a context menu, specifying our custom context // menu (ContextMenu1) as the context menu to be displayed string jsShowDataContextMenu = string.Format("esriShowContextMenu(event,'{0}','{1}','{2}'); var contextNode = $find('{2}');if (contextNode){{ contextNode.style.backgroundColor = '{3}' }} this.style.backgroundColor = '{4}'; contextNode = this; return false;", ContextMenu1.ClientID, Toc1.UniqueID, layerTreeViewPlusNode.NodeID, System.Drawing.ColorTranslator.ToHtml(Toc1.BackColor), System.Drawing.ColorTranslator.ToHtml(System.Drawing.Color.LightBlue)); // Wire the context menu displaying JavaScript to the layer node's oncontextmenu event layerTreeViewPlusNode.TextCellAttributes.Add("oncontextmenu", jsShowDataContextMenu); // Construct the JavaScript needed to set the color of the layer node to the Toc's hover color and wire this JavaScript // to the node's onmouseover event string jsOnMouseOver = string.Format("this.style.color = '{0}';", System.Drawing.ColorTranslator.ToHtml(Toc1.HoverColor)); layerTreeViewPlusNode.TextCellAttributes.Add("onmouseover", jsOnMouseOver); // Construct the JavaScript needed to set the color of the layer node to the Toc's forecolor and wire this JavaScript // to the node's onmouseout event string jsOnMouseOut = string.Format("this.style.color = '{0}';", System.Drawing.ColorTranslator.ToHtml(Toc1.ForeColor)); layerTreeViewPlusNode.TextCellAttributes.Add("onmouseout", jsOnMouseOut); } } } } } #endregion #region Web ADF Control Event Handlers // Fires when an item on the custom context menu is clicked void ContextMenu1_ItemClicked(object sender, ESRI.ArcGIS.ADF.Web.UI.WebControls.ContextMenuItemEventArgs contextMenuItemEventArgs) { // Get the node's ID from the passed-in arguments string nodeID = contextMenuItemEventArgs.Context; // Get a reference to the node ESRI.ArcGIS.ADF.Web.UI.WebControls.TreeViewPlusNode treeViewPlusNode = Toc1.Nodes.FindByNodeID(nodeID); // Get a reference to the Toc control from the passed-in arguments ESRI.ArcGIS.ADF.Web.UI.WebControls.Toc adfToc = contextMenuItemEventArgs.Control as ESRI.ArcGIS.ADF.Web.UI.WebControls.Toc; // Get the MapTocFunctionality that includes the current node ESRI.ArcGIS.ADF.Web.DataSources.IMapTocFunctionality mapTocFunctionality = adfToc.GetFunctionality(treeViewPlusNode.Parent.Value); // Use the MapTocFunctionality to create a QueryFunctionality ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality commonQueryFunctionality = mapTocFunctionality.Resource.CreateFunctionality(typeof(ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality), "mapTocQueryFunctionality") as ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality; // Use the QueryFunctionality to get an array containing all the fields in the layer corresponding to the node string[] fieldArray = commonQueryFunctionality.GetFields(null, treeViewPlusNode.Value); // Loop through the field array and build HTML markup that will display the fields string fieldListMarkup = "<div style='font-weight: bold'>" + treeViewPlusNode.Text + " Fields: </div>"; foreach (string field in fieldArray) { fieldListMarkup += field + "<br>"; } // Create a callback result that will inject the field list markup into the fieldDiv and add it to the // context menu's callback results collection ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult displayFieldsCallbackResult = ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult.CreateSetInnerContent("fieldDiv", fieldListMarkup); // Create JavaScript call to reset background color of the active layer node string resetNodeBackground = string.Format("var contextNodeCell = $get('{0}_textCell'); contextNodeCell.style.backgroundColor = '{1}';", nodeID, System.Drawing.ColorTranslator.ToHtml(Toc1.BackColor)); ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult resetNodeBackgroundCallbackResult = ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult.CreateJavaScript(resetNodeBackground); // Add custom callback results to context menu. The context menu initiated the request thus its callback results will // be included in the response. ContextMenu1.CallbackResults.Add(displayFieldsCallbackResult); ContextMenu1.CallbackResults.Add(resetNodeBackgroundCallbackResult); } #endregion }