Common Custom EditorTask
Common_CustomEditorTask_VBNet\CustomEditorTask_VBNet\SimpleQueryPanel.vb
' 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.
' 

Imports Microsoft.VisualBasic
Imports System
Namespace CustomEditorTask_VBNet
    <AjaxControlToolkit.ClientScriptResource("CustomEditorTask_VBNet.SimpleQueryPanel", "SimpleQueryPanel.js")> _
    Friend Class SimpleQueryPanel
        Inherits ESRI.ArcGIS.ADF.ArcGISServer.Editor.EditorPanel
#Region "Instance Variable Declarations"

        Private m_panelDiv As System.Web.UI.HtmlControls.HtmlGenericControl
        Private m_callbackArgsCollection As System.Collections.Specialized.NameValueCollection

#End Region

#Region "Constructor"

        Public Sub New(ByVal editorTask As ESRI.ArcGIS.ADF.ArcGISServer.Editor.EditorTask)
            MyBase.New("Simple Query", editorTask, "simpleQueryPanel")
            ' Add a handler that fires when a new editor layer is selected
            AddHandler ParentEditor.LayerChanged, AddressOf ParentEditor_LayerChanged
        End Sub

#End Region

#Region "WebControl Life Cycle Event Overrides"

        Protected Overrides Sub CreateChildControls()
            MyBase.CreateChildControls()

            ' Create a container div for the client-side UI component
            m_panelDiv = New System.Web.UI.HtmlControls.HtmlGenericControl("div")
            m_panelDiv.ID = "simpleQueryPanelDiv"
            Controls.Add(m_panelDiv)
        End Sub

        Protected Overrides Sub OnPreRender(ByVal e As System.EventArgs)
            MyBase.OnPreRender(e)

            ' Register the JavaScript file containing the SimpleQueryPanel JavaScript UI component and 
            ' functions to control interaction between the client panel and the server
            'System.Web.UI.ScriptManager.RegisterClientScriptResource((System.Web.UI.Control)this, 
            '    this.GetType(), "CustomEditorTask_CSharp.javascript.SimpleQueryPanel.js");

            ' Get a reference to the editor task's layers drop-down list
            Dim editorLayerList As System.Web.UI.WebControls.DropDownList = Nothing
            editorLayerList = TryCast(CustomUtilities.FindControl("editTaskLayerList", TryCast(Me.ParentEditor, System.Web.UI.Control)), System.Web.UI.WebControls.DropDownList)

            ' Get strings containing the numeric and text fields for the current layer
            Dim numericFields As String = CustomUtilities.GetFields(CustomEditorInstance, CustomUtilities.FieldType.Numeric)
            Dim textFields As String = CustomUtilities.GetFields(CustomEditorInstance, CustomUtilities.FieldType.Text)

            ' Get the URL of the activity_indicator image file
            Dim activityIndicatorUrl As String = Me.Page.ClientScript.GetWebResourceUrl(GetType(CustomEditorTask), "CustomEditorTask_CSharp.images.activity_indicator.gif")


            ' Construct the JavaScript necessary to initialize the client-side query panel with
            ' server-side properties
            Dim scriptBlock As String = "                " & ControlChars.CrLf & "                function invokeSimpleQueryPanelInitialization(){{" & ControlChars.CrLf & "                  initializeSimpleQueryPanel('{0}', '{1}', '{2}', '{3}', '{4}', ""{5}"");" & ControlChars.CrLf & "                }}" & ControlChars.CrLf & ControlChars.CrLf & "                Sys.Application.add_init(invokeSimpleQueryPanelInitialization);"
            scriptBlock = String.Format(scriptBlock, m_panelDiv.ClientID, editorLayerList.ClientID, numericFields, textFields, activityIndicatorUrl, Me.CallbackFunctionString)

            ' Register the initialization code as a startup script
            Me.Page.ClientScript.RegisterStartupScript(Me.GetType(), Me.ClientID & "_init", scriptBlock, True)

            ' Initialize the OIDFieldName property
            Me.OIDfieldName = CustomEditorInstance.FeatureLayer.FeatureClass.OIDFieldName
        End Sub

        Protected Overrides Sub RenderContents(ByVal writer As System.Web.UI.HtmlTextWriter)
            MyBase.RenderContents(writer)

            ' Create a callback result that will call the renderSimpleQueryPanel JavaScript function, which
            ' will expicitly re-render the client-side simple query panel.  This is necessary because the
            ' server has no knowledge of the client-side panel's elements, so when the panel is rendered
            ' (which is done with information stored in state on the server), these elements are lost.
            Dim redrawQueryPanelCallbackResult As ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult = ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult.CreateJavaScript("renderSimpleQueryPanel();")
            Me.ParentEditor.CallbackResults.Add(redrawQueryPanelCallbackResult)
        End Sub

#End Region

#Region "Web ADF Event Handlers"

        Private Sub ParentEditor_LayerChanged(ByVal featureLayer As ESRI.ArcGIS.Carto.IFeatureLayer)
            ' Get the object ID field of the newly selected layer
            Me.OIDfieldName = featureLayer.FeatureClass.OIDFieldName

            ' Get the numeric and text fields of the new layer
            Dim numericFields As String = CustomUtilities.GetFields(CustomEditorInstance, CustomUtilities.FieldType.Numeric)
            Dim textFields As String = CustomUtilities.GetFields(CustomEditorInstance, CustomUtilities.FieldType.Text)

            ' Construct a call to a JavaScript function that will update the client-side query
            ' panel with the new layer's fields
            Dim jsUpdateFields As String = String.Format("updateSimpleQueryPanelFields('{0}', '{1}');", textFields, numericFields)
            Dim updateFieldsCallbackResult As ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult = ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult.CreateJavaScript(jsUpdateFields)
            Me.ParentEditor.CallbackResults.Add(updateFieldsCallbackResult)
        End Sub

#End Region ';

#Region "Callback Handler"

        Public Overrides Sub RaiseCallbackEvent(ByVal eventArgs As String)
            ' Parse callback arguments using the CallbackUtility included with Web ADF.
            m_callbackArgsCollection = ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackUtility.ParseStringIntoNameValueCollection(eventArgs)

            ' Check whether the current event argument indicates that a query was initiated by the user
            If m_callbackArgsCollection("EventArg") = "query" Then
                ' Get the database name of the specified query field 
                Dim queryField As String = CustomUtilities.ReplaceAliases(m_callbackArgsCollection("Field"), CustomEditorInstance)

                ' Get the query value specified by the user
                Dim queryValue As String = m_callbackArgsCollection("value")

                ' Check the query type.  If text, enclose the query value in single quotes
                If m_callbackArgsCollection("Type") = "text" Then
                    queryValue = "'" & queryValue & "'"
                End If

                ' Construct a query string from the field, operator, and value specified by the user
                Dim queryString As String = String.Format("{0} {1} {2}", queryField, m_callbackArgsCollection("Operator"), queryValue)

                ' Get IDs of features satisfying query
                Dim matchingFeatureIDs() As Integer = CustomUtilities.DoQuery(queryString, Me.ParentEditor)

                ' Get LayerDescription object for currently selected layer
                Dim agsLayerDescription As ESRI.ArcGIS.ADF.ArcGISServer.LayerDescription = ESRI.ArcGIS.ADF.ArcGISServer.Editor.EditorUtilities.GetLayerDescription(Me.ParentEditor.MapFunctionality, Me.ParentEditor.SelectedLayerID)

                ' Combine the feature IDs from the query with those currently selected.  How this
                ' is done will depend on the selection option currently specified in the editor
                ' task's settings
                Dim selectionChanged As Boolean
                Dim selectedIDsList As System.Collections.Generic.List(Of Integer) = CustomUtilities.UpdateSelection(agsLayerDescription.SelectionFeatures, matchingFeatureIDs, selectionChanged, Me.ParentEditor.EditorTask)

                ' Check whether the selected IDs changed
                If selectionChanged Then
                    ' Set selected features on layer currently being edited
                    agsLayerDescription.SelectionFeatures = selectedIDsList.ToArray()

                    ' Get attributes panel and set to edit the attributes of selected features
                    Dim editAttributesPanel As ESRI.ArcGIS.ADF.ArcGISServer.Editor.EditAttributesPanel = TryCast(Me.ParentEditor.AttributesEditor, ESRI.ArcGIS.ADF.ArcGISServer.Editor.EditAttributesPanel)
                    Dim editAttributesPanelResults As ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResultCollection = editAttributesPanel.SetSelectedFeatures(agsLayerDescription.SelectionFeatures)
                    Me.CallbackResults.CopyFrom(editAttributesPanelResults)

                    ' Refresh map
                    ESRI.ArcGIS.ADF.ArcGISServer.Editor.EditorUtilities.RefreshMap(Me.ParentEditor, Nothing)
                    Me.CallbackResults.CopyFrom(Me.ParentEditor.Map.CallbackResults)

                    ' Refresh toolbars
                    Me.ParentEditor.RefreshToolbars(Me.CallbackResults)
                End If

                ' Create callback result that calls JavaScript to hide AJAX activity indicator
                Dim jsHideIndicator As String = String.Format("hideSimpleQueryIndicator();")
                Dim hideIndicatorCallbackResult As ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult = ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult.CreateJavaScript(jsHideIndicator)
                Me.CallbackResults.Add(hideIndicatorCallbackResult)
            Else
                MyBase.RaiseCallbackEvent(eventArgs)
            End If

        End Sub

        Public Overrides Function GetCallbackResult() As String
            ' If the callback was initiated by a query operation, return the panel's callback results
            If m_callbackArgsCollection("EventArg") = "query" Then
                Return Me.CallbackResults.ToString()
            Else
                Return MyBase.GetCallbackResult()
            End If
        End Function

#End Region

#Region "Instance Properties"

        ' Name of object ID field of selected layer
        Protected Property OIDfieldName() As String
            Get
                Return CStr(StateManager.GetProperty("selectedLayerOIDfield"))
            End Get
            Set(ByVal value As String)
                StateManager.SetProperty("selectedLayerOIDfield", value)
            End Set
        End Property

        ' Easy access to the CustomEditor containing the panel instance
        Protected ReadOnly Property CustomEditorInstance() As CustomEditor
            Get
                Return CType(Me.ParentEditor, CustomEditor)
            End Get
        End Property

#End Region
    End Class
End Namespace