Common Context menu
Common_ContextMenu_VBNet\App_Code\SimpleTaskContextMenu_VBNet.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
Imports System.Collections.Generic
Imports System.Text
Imports System.Web.UI
Imports System.Drawing
Imports System.Web.UI.WebControls
Imports System.Web.UI.HtmlControls
Imports System.Collections.Specialized
Imports ESRI.ArcGIS.ADF.Web.UI.WebControls
Imports System.ComponentModel

Namespace SimpleTaskContextMenu
  <ToolboxData("<{0}:SimpleTaskContextMenu_VBNet runat=""server"" Width=""100px"" BackColor=""White"" BorderColor=""Blue"" BorderWidth=""1px""> </{0}:SimpleTaskContextMenu_VBNet>")> _
  Public Class SimpleTaskContextMenu_VBNet
    Inherits FloatingPanelTask
    Private textBox As TextBox = Nothing
    Private button As HtmlInputButton = Nothing
    Private customContextMenuSimpleResult As ContextMenu
    Private _taskResults As TaskResults = Nothing

    <Browsable(True), Category("Appearance"), DefaultValue("Execute"), PersistenceMode(PersistenceMode.Attribute)> _
    Public Property ButtonText() As String
      Get
        Dim o As Object = StateManager.GetProperty("buttonText")
        If (o Is Nothing) Then
          Return "Execute"
        Else
          Return TryCast(o, String)
        End If
      End Get
      Set
        StateManager.SetProperty("buttonText", Value)
      End Set
    End Property

    Private ReadOnly Property TaskResultsInstance() As TaskResults
      Get
        If (_taskResults Is Nothing) AndAlso (Not TaskResultsContainers(0) Is Nothing) Then
          _taskResults = TryCast(Utility.FindControl(TaskResultsContainers(0).Name, Page), TaskResults)
        End If
        Return _taskResults
      End Get
    End Property

    Protected Overrides Sub OnLoad(ByVal e As EventArgs)
      MyBase.OnLoad(e)

      AddHandler TaskResultsInstance.NodeClicked, AddressOf TaskResults_NodeClicked

    End Sub

    ' Handle an event on the TaskResults control
    Private Sub TaskResults_NodeClicked(ByVal sender As Object, ByVal args As TreeViewPlusNodeEventArgs)
      Dim nodeText As String = args.Node.Text

      Dim cr As CallbackResult = New CallbackResult(Nothing, Nothing, "javascript", String.Format("window.status = 'Clicked on {0} during millisecond value of {1}'", nodeText, DateTime.Now.Millisecond))
      TaskResultsInstance.CallbackResults.Add(cr)
    End Sub

    Protected Overrides Sub CreateChildControls()
      Controls.Clear()
      MyBase.CreateChildControls()

      textBox = New TextBox()
      textBox.ID = "textBox"

      button = New HtmlInputButton()
      button.ID = "button"
      button.Value = ButtonText

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

      Dim getArgumentJS As String = String.Format ("'textBoxValue=' + document.getElementById('{0}').value", textBox.ClientID)
      Dim onClick As String = 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
      Dim fontinfo As System.Web.UI.WebControls.FontInfo = customContextMenuSimpleResult.Font
      fontinfo.Name = "Verdana"
      fontinfo.Size = New System.Web.UI.WebControls.FontUnit(8)

      AddHandler customContextMenuSimpleResult.ItemClicked, AddressOf customContextMenuSimpleResult_ItemClicked
      AddHandler customContextMenuSimpleResult.Dismissed, AddressOf customContextMenuSimpleResult_Dismissed

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

      Dim item1 As ESRI.ArcGIS.ADF.Web.UI.WebControls.ContextMenuItem = New ESRI.ArcGIS.ADF.Web.UI.WebControls.ContextMenuItem()
      item1.Text = "Remove"
      item1.ImageUrl = ""
      customContextMenuSimpleResult.Items.Add(item1)

      Controls.Add(customContextMenuSimpleResult)

    End Sub

    Private Sub customContextMenuSimpleResult_Dismissed(ByVal sender As Object, ByVal args As ContextMenuDismissedEventArgs)
      Dim node As ESRI.ArcGIS.ADF.Web.UI.WebControls.TreeViewPlusNode = TaskResultsInstance.Nodes.FindByNodeID(args.Context)
      If Not node Is Nothing Then
        ' Unselect node in TaskResults
        customContextMenuSimpleResult.CallbackResults.Add(New ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult(Me, "javascript", String.Format("var node=document.getElementById('{0}_textCell');if(node!=null){{node.style.backgroundColor='{1}';}}", node.NodeID, System.Drawing.ColorTranslator.ToHtml(TaskResultsInstance.BackColor))))
      End If
    End Sub

    Private Sub customContextMenuSimpleResult_ItemClicked(ByVal sender As Object, ByVal args As ESRI.ArcGIS.ADF.Web.UI.WebControls.ContextMenuItemEventArgs)
      Dim node As ESRI.ArcGIS.ADF.Web.UI.WebControls.TreeViewPlusNode = TaskResultsInstance.Nodes.FindByNodeID(args.Context)

      Select Case args.Item.Text
        Case "Show Alert"
          Dim cr As ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult = New ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult(Nothing, Nothing, "javascript", String.Format ("alert('Worker process identity: {0}')", System.Security.Principal.WindowsIdentity.GetCurrent().Name.Replace("\", "\\")))
          customContextMenuSimpleResult.CallbackResults.Add(cr)
        Case "Remove"
          node.Parent.Nodes.Remove(node)
          TaskResultsInstance.Refresh()
          customContextMenuSimpleResult.CallbackResults.CopyFrom(TaskResultsInstance.CallbackResults)
      End Select
    End Sub

    Public Overrides Function GetCallbackResult() As String
      Dim keyValColl As NameValueCollection = CallbackUtility.ParseStringIntoNameValueCollection(_callbackArg)
      Input = keyValColl("textBoxValue")
      Return MyBase.GetCallbackResult()
    End Function

    Public Overrides Sub ExecuteTask()
      Results = Nothing
      If Input Is Nothing Then
      Return
      End If

      Dim textBoxValue As String = TryCast(Input, String)

      Dim trNode As ESRI.ArcGIS.ADF.Web.UI.WebControls.TaskResultNode = New ESRI.ArcGIS.ADF.Web.UI.WebControls.TaskResultNode ("Heading: " & textBoxValue)
      trNode.Expanded = False
      trNode.ClickBehavior = TreeViewPlusNodeClickBehavior.None

      Dim tvpn As ESRI.ArcGIS.ADF.Web.UI.WebControls.TreeViewPlusNode = 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
    End Sub

    Public Overrides Sub Refresh()
      Dim tmp As String = TryCast(Input, String)
      If (Not String.IsNullOrEmpty(tmp)) Then
      textBox.Text = tmp
      End If
      MyBase.Refresh()
    End Sub

    Public Overrides Function GetGISResourceItemDependencies() As List(Of GISResourceItemDependency)
      Dim list As List(Of GISResourceItemDependency) = New List(Of GISResourceItemDependency)()
      Return list
    End Function
  End Class
End Namespace