Common_ContextMenu_VBNet\SimpleToc.aspx.vb
' 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. ' Imports Microsoft.VisualBasic Imports System Public Partial Class SimpleToc Inherits System.Web.UI.Page #Region "ASP.NET Page Life Cycle Event Handlers" Protected Sub Page_Init(ByVal sender As Object, ByVal eventArgs As System.EventArgs) ' Add an event handler for when the an item on the context menu is clicked AddHandler ContextMenu1.ItemClicked, AddressOf ContextMenu1_ItemClicked End Sub Protected Sub Page_Load(ByVal sender As Object, ByVal eventArgs As System.EventArgs) ' Dynamically create a div element and add it to the page Dim htmlDiv As System.Web.UI.HtmlControls.HtmlGenericControl = New System.Web.UI.HtmlControls.HtmlGenericControl("div") htmlDiv.ID = "fieldDiv" Page.Form.Controls.Add(htmlDiv) End Sub Protected Sub Page_PreRenderComplete(ByVal sender As Object, ByVal eventArgs As System.EventArgs) If Not IsPostBack Then ' Create a new context menu item and add it to the context menu Dim showFieldsContextMenuItem As ESRI.ArcGIS.ADF.Web.UI.WebControls.ContextMenuItem = 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. Dim commonMapFunctionality As ESRI.ArcGIS.ADF.Web.DataSources.IMapFunctionality = Nothing For Each gisFunctionality As ESRI.ArcGIS.ADF.Web.DataSources.IGISFunctionality In Map1.GetFunctionalities() If TypeOf gisFunctionality Is ESRI.ArcGIS.ADF.Web.DataSources.IMapFunctionality Then commonMapFunctionality = TryCast(gisFunctionality, ESRI.ArcGIS.ADF.Web.DataSources.IMapFunctionality) Exit For End If Next gisFunctionality ' Initialize the nodes corresponding to each MapTocFunctionality object For Each mapTocFunctionality As ESRI.ArcGIS.ADF.Web.DataSources.IMapTocFunctionality In Toc1.GetFunctionalities() ' Get the Toc data frames referenced by the current MapTocFunctionality Dim tocDataFrameArray As ESRI.ArcGIS.ADF.Web.TocDataFrame() = mapTocFunctionality.GetMapContents(commonMapFunctionality.Name, Map1.ImageFormat, Map1.UseMimeData, False) ' Initialize the nodes of each TocDataFrame in the array Dim i As Integer = 0 Do While i < tocDataFrameArray.Length ' Find the resource-level node corresponding to the current MapTocFunctionality's resource Dim layersTreeViewPlusNodeCollection As ESRI.ArcGIS.ADF.Web.UI.WebControls.TreeViewPlusNodeCollection = Nothing For Each resourceTreeViewPlusNode As ESRI.ArcGIS.ADF.Web.UI.WebControls.TreeViewPlusNode In Toc1.Nodes If resourceTreeViewPlusNode.Text = mapTocFunctionality.Resource.Name Then ' Get the layer nodes from the resource node layersTreeViewPlusNodeCollection = resourceTreeViewPlusNode.Nodes Exit For End If Next resourceTreeViewPlusNode ' Set the client-side events for each layer node For Each layerTreeViewPlusNode As ESRI.ArcGIS.ADF.Web.UI.WebControls.TreeViewPlusNode 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 Dim jsShowDataContextMenu As String = 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 Dim jsOnMouseOver As String = 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 Dim jsOnMouseOut As String = String.Format("this.style.color = '{0}';", System.Drawing.ColorTranslator.ToHtml(Toc1.ForeColor)) layerTreeViewPlusNode.TextCellAttributes.Add("onmouseout", jsOnMouseOut) Next layerTreeViewPlusNode i += 1 Loop Next mapTocFunctionality End If End Sub #End Region #Region "Web ADF Control Event Handlers" ' Fires when an item on the custom context menu is clicked Private Sub ContextMenu1_ItemClicked(ByVal sender As Object, ByVal contextMenuItemEventArgs As ESRI.ArcGIS.ADF.Web.UI.WebControls.ContextMenuItemEventArgs) ' Get the node's ID from the passed-in arguments Dim nodeID As String = contextMenuItemEventArgs.Context ' Get a reference to the node Dim treeViewPlusNode As ESRI.ArcGIS.ADF.Web.UI.WebControls.TreeViewPlusNode = Toc1.Nodes.FindByNodeID(nodeID) ' Get a reference to the Toc control from the passed-in arguments Dim adfToc As ESRI.ArcGIS.ADF.Web.UI.WebControls.Toc = TryCast(contextMenuItemEventArgs.Control, ESRI.ArcGIS.ADF.Web.UI.WebControls.Toc) ' Get the MapTocFunctionality that includes the current node Dim mapTocFunctionality As ESRI.ArcGIS.ADF.Web.DataSources.IMapTocFunctionality = adfToc.GetFunctionality(treeViewPlusNode.Parent.Value) ' Use the MapTocFunctionality to create a QueryFunctionality Dim commonQueryFunctionality As ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality = TryCast(mapTocFunctionality.Resource.CreateFunctionality(GetType(ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality), "mapTocQueryFunctionality"), ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality) ' Use the QueryFunctionality to get an array containing all the fields in the layer corresponding to the node Dim fieldArray As String() = commonQueryFunctionality.GetFields(Nothing, treeViewPlusNode.Value) ' Loop through the field array and build HTML markup that will display the fields Dim fieldListMarkup As String = "<div style='font-weight: bold'>" & treeViewPlusNode.Text & " Fields: </div>" For Each field As String In fieldArray fieldListMarkup &= field & "<br>" Next field ' 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 Dim displayFieldsCallbackResult As ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult = ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult.CreateSetInnerContent("fieldDiv", fieldListMarkup) ' Create JavaScript call to reset background color of the active layer node Dim resetNodeBackground As String = String.Format("var contextNodeCell = $get('{0}_textCell'); contextNodeCell.style.backgroundColor = '{1}';", nodeID, System.Drawing.ColorTranslator.ToHtml(Toc1.BackColor)) Dim resetNodeBackgroundCallbackResult As ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult = 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) End Sub #End Region End Class