Common Task results
Common_TaskResults_VBNet\TaskResultsWebSite\ContextMenu.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 ContextMenu
  Inherits System.Web.UI.Page
  #Region "ASP.NET Page Life Cycle Event Handlers - Page_Init, Page_PreRender"

  Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs)
    ' Add a handler to fire when an item on the custom context menu is selected
    AddHandler ContextMenu1.ItemClicked, AddressOf 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.
    AddHandler ContextMenu1.Dismissed, AddressOf ContextMenu1_Dismissed
  End Sub

  Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs)
    ' 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
    Dim showAlertContextMenuItem As ESRI.ArcGIS.ADF.Web.UI.WebControls.ContextMenuItem = 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))
  End Sub

  #End Region

  #Region "Web ADF Control Event Handlers - Toolbar1_CommandClick, ContextMenu1_ItemClicked, ContextMenu1_Dismissed"

  Protected Sub Toolbar1_CommandClick(ByVal sender As Object, ByVal toolbarCommandClickEventArgs As ESRI.ArcGIS.ADF.Web.UI.WebControls.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.
    Select Case toolbarCommandClickEventArgs.CommandName
      Case ("CreateTaskResult")
        ' Create a parent node for the result and assign it a remove-only context menu
        Dim headingTaskResultNode As ESRI.ArcGIS.ADF.Web.UI.WebControls.TaskResultNode = 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
        Dim detailTreeViewPlusNode As ESRI.ArcGIS.ADF.Web.UI.WebControls.TreeViewPlusNode = 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(Nothing, Nothing, Nothing, headingTaskResultNode)

        ' Copy the TaskResults control's callback results to the Toolbar so they are processed
        ' on the client
        Toolbar1.CallbackResults.CopyFrom(TaskResults1.CallbackResults)
    End Select
  End Sub

  Private Sub ContextMenu1_ItemClicked(ByVal sender As Object, ByVal contextMenuItemEventArgs As ESRI.ArcGIS.ADF.Web.UI.WebControls.ContextMenuItemEventArgs)
    ' Get the node that was right-clicked to display the context menu
    Dim treeViewPlusNode As ESRI.ArcGIS.ADF.Web.UI.WebControls.TreeViewPlusNode = TaskResults1.Nodes.FindByNodeID(contextMenuItemEventArgs.Context)

    ' Check the text of the context menu item that was clicked
    Select Case contextMenuItemEventArgs.Item.Text
      Case "Show Alert"
        ' Create a callback result that will show a JavaScript alert
        Dim callbackResult As ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult = ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult.CreateJavaScript("alert('Hello')")
        ContextMenu1.CallbackResults.Add(callbackResult)
      Case "Remove"
        ' Remove the selected node from the tree view
        treeViewPlusNode.Parent.Nodes.Remove(treeViewPlusNode)
        TaskResults1.Refresh()
        ContextMenu1.CallbackResults.CopyFrom(TaskResults1.CallbackResults)
    End Select
  End Sub

  Private Sub ContextMenu1_Dismissed(ByVal sender As Object, ByVal args As ESRI.ArcGIS.ADF.Web.UI.WebControls.ContextMenuDismissedEventArgs)
    ' Remove the highlight from the task results node that was right-clicked
    ' to display the context menu
    TaskResults1.ContextMenuDismissed(ContextMenu1, args)
  End Sub

  #End Region
End Class