Common Simple Server task
Common_SimpleServerTask_VBNet\SimpleServerTaskWebConfigurator_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.Web.UI.WebControls
Namespace Commom_SimpleServerTask_VBNet
    ' Specifies the simple task's configuration interface in ArcGIS Manager
    Public Class SimpleServerTaskWebConfigurator_VBNet
        Inherits ESRI.ArcGIS.ADF.Web.UI.WebControls.CompositeControl
        Implements ESRI.ArcGIS.ADF.Web.UI.WebControls.IWebConfigurator, ESRI.ArcGIS.ADF.Web.UI.WebControls.IBuddyControlSupport

#Region "Instance Variable Declarations"

        Private m_okButton As System.Web.UI.WebControls.Button = Nothing
        Private m_cancelButton As System.Web.UI.WebControls.Button = Nothing
        Private m_buttonTextBox As System.Web.UI.WebControls.TextBox = Nothing
        Private m_titleTextBox As System.Web.UI.WebControls.TextBox = Nothing
        Private m_backgroundColorPicker As ESRI.ArcGIS.ADF.Web.UI.WebControls.ColorPicker = Nothing
        Private m_titleColorPicker As ESRI.ArcGIS.ADF.Web.UI.WebControls.ColorPicker = Nothing
        Private m_additionalControls As System.Web.UI.ControlCollection = Nothing
        Private m_simpleTaskInstance As SimpleServerTask_VBNet = Nothing
        Private Event m_onWebConfigurationComplete As ESRI.ArcGIS.ADF.Web.UI.WebControls.WebConfigurationCompleteEventHandler
        Private Event m_onWebConfigurationCancel As ESRI.ArcGIS.ADF.Web.UI.WebControls.WebConfigurationCanceledEventHandler

#End Region

#Region "Constructor"

        Public Sub New()
            MyBase.New()
        End Sub

#End Region

#Region "Instance Members"

#Region "Utility Methods"

        ' Used by the WebConfigurationComplete event to retrieve the markup that is injected into the
        ' page at design time.  In this implementation, this event is fired by clicking the OK button
        ' on the configurator.
        Private Function getDesignTimeTag() As String
            ' Markup for the task's open tag, which includes the task's properties.  Note that some
            ' hard-coded default values are included, while other values are populated dynamically
            ' based on the task instance's properties.
            Dim simpleTaskOpenTag As String = String.Format("<simpleServerTaskVB:SimpleServerTask_VBNet ID=""{0}"" " & "runat=""server"" Style=""z-index: 10000; left: 100px; position: absolute; " & "top: 100px"" Width=""200px"" Visible=""False"" ButtonText=""{1}"" Title=""{2}"" " & "ToolTip=""{3}"" NavigationPath=""{4}"" BackColor=""{5}"" TitleBarColor=""{6}"" " & "BorderStyle=""Solid"" BorderWidth=""1px"" BorderColor=""Black"">", m_simpleTaskInstance.ID, m_simpleTaskInstance.ButtonText, m_simpleTaskInstance.Title, m_simpleTaskInstance.ToolTip, m_simpleTaskInstance.NavigationPath, System.Drawing.ColorTranslator.ToHtml(m_simpleTaskInstance.BackColor), System.Drawing.ColorTranslator.ToHtml(m_simpleTaskInstance.TitleBarColor))

            ' Markup for inclusion of a default task results container with an id of "TaskResults1"
            Dim taskResultsContainerTagStringBuilder As New System.Text.StringBuilder()
            taskResultsContainerTagStringBuilder.Append("<TaskResultsContainers>")
            taskResultsContainerTagStringBuilder.Append("<esri:BuddyControl Name=""TaskResults1"" />")
            taskResultsContainerTagStringBuilder.Append("</TaskResultsContainers>")

            ' Simple task close tag
            Dim simpleTaskCloseTag As String = "</simpleServerTaskVB:SimpleServerTask_VBNet>"

            ' A string builder is used to concatenate the simple task open tag, task results container
            ' tag, and the simple task close tag
            Dim completeTagStringBuilder As New System.Text.StringBuilder()
            completeTagStringBuilder.Append(simpleTaskOpenTag)
            completeTagStringBuilder.Append(taskResultsContainerTagStringBuilder.ToString())
            completeTagStringBuilder.Append(simpleTaskCloseTag)

            Return completeTagStringBuilder.ToString()
        End Function

        ' Copies properties from the task instance to the appropriate configurator web controls.  Called
        ' when the web configurator interface is loaded.
        Private Sub loadProperties()
            If m_simpleTaskInstance Is Nothing Then
                Return
            End If

            m_titleTextBox.Text = m_simpleTaskInstance.Title
            m_buttonTextBox.Text = m_simpleTaskInstance.ButtonText
            m_backgroundColorPicker.ChosenColor = m_simpleTaskInstance.BackColor
            m_titleColorPicker.ChosenColor = m_simpleTaskInstance.TitleBarColor
        End Sub

#End Region

#Region "UI Events"

        ' Invokes WebConfigurationCanceled event when the cancel button is clicked
        Private Sub cancelButton_Click(ByVal sender As Object, ByVal e As System.EventArgs)
            OnWebConfigurationCancel(New System.EventArgs())
        End Sub

        ' Saves user-specified properties to the task instance
        Private Sub okButton_Click(ByVal sender As Object, ByVal e As System.EventArgs)
            If m_simpleTaskInstance Is Nothing Then
                Return
            End If
            m_simpleTaskInstance.Title = m_titleTextBox.Text
            m_simpleTaskInstance.ButtonText = m_buttonTextBox.Text
            m_simpleTaskInstance.BackColor = m_backgroundColorPicker.ChosenColor
            m_simpleTaskInstance.TitleBarColor = m_titleColorPicker.ChosenColor

            OnWebConfigurationComplete(New ESRI.ArcGIS.ADF.Web.UI.WebControls.WebConfigurationCompleteEventArgs(m_simpleTaskInstance, getDesignTimeTag()))
        End Sub

#End Region

#Region "Overridable Configurator Events"

        ' Overridable wrapper for the WebConfigurationComplete event
        Protected Overridable Sub OnWebConfigurationComplete(ByVal args As ESRI.ArcGIS.ADF.Web.UI.WebControls.WebConfigurationCompleteEventArgs)
            RaiseEvent m_onWebConfigurationComplete(Me, args)
        End Sub

        ' Overridable wrapper for the WebConfigurationCancel event
        Protected Overridable Sub OnWebConfigurationCancel(ByVal args As System.EventArgs)
            RaiseEvent m_onWebConfigurationCancel(Me, args)
        End Sub

#End Region

#End Region

#Region "WebControl Members"

#Region "WebControl Life Cycle Event Overrides"

        ' Configures the web configurator's interface
        Protected Overrides Sub CreateChildControls()
            Controls.Clear()

            ' Create a table to hold the controls that will appear on the interface
            Dim taskConfiguratorTable As New Table()
            taskConfiguratorTable.Style(System.Web.UI.HtmlTextWriterStyle.Position) = "relative"
            Controls.Add(taskConfiguratorTable)

            ' Initialize the title label
            Dim titleTextLabel As New Label()
            titleTextLabel.ID = "lblTitle"
            titleTextLabel.Text = "Title:"

            ' Add the title label to a table cell
            Dim taskConfiguratorCell As New TableCell()
            taskConfiguratorCell.Controls.Add(titleTextLabel)

            ' Add the table cell to a table row
            Dim taskConfiguratorRow As New TableRow()
            taskConfiguratorRow.Controls.Add(taskConfiguratorCell)

            ' Initialize the title textbox
            m_titleTextBox = New TextBox()
            m_titleTextBox.ID = "txtTitle"

            ' Add the title textbox to a table cell and the table cell to a table row
            taskConfiguratorCell = New TableCell()
            taskConfiguratorCell.Controls.Add(m_titleTextBox)
            taskConfiguratorRow.Controls.Add(taskConfiguratorCell)

            ' Add the row containing the title label and textbox to the table
            taskConfiguratorTable.Controls.Add(taskConfiguratorRow)

            ' Initialize the button text label
            Dim buttonTextLabel As New Label()
            buttonTextLabel.ID = "lblButtonText"
            buttonTextLabel.Text = "Button Text:"
            buttonTextLabel.Style(System.Web.UI.HtmlTextWriterStyle.WhiteSpace) = "nowrap"

            ' Add the button text label to a table cell and the table cell to a table row
            taskConfiguratorCell = New TableCell()
            taskConfiguratorCell.Controls.Add(buttonTextLabel)
            taskConfiguratorRow = New TableRow()
            taskConfiguratorRow.Controls.Add(taskConfiguratorCell)

            ' Initialize the button text textbox
            m_buttonTextBox = New TextBox()
            m_buttonTextBox.Text = "Execute"
            m_buttonTextBox.ID = "txtButton"

            ' Add the button text textbox to a table cell and the cell to a table row
            taskConfiguratorCell = New TableCell()
            taskConfiguratorCell.Controls.Add(m_buttonTextBox)
            taskConfiguratorRow.Controls.Add(taskConfiguratorCell)

            ' Add the row containing the button text label and textbox to the table
            taskConfiguratorTable.Controls.Add(taskConfiguratorRow)

            ' Initialize the title bar color picker
            m_titleColorPicker = New ESRI.ArcGIS.ADF.Web.UI.WebControls.ColorPicker()
            m_titleColorPicker.ID = "clrPkrTitle"
            m_titleColorPicker.Font.Name = "Verdana"
            m_titleColorPicker.Font.Size = New FontUnit(New Unit(8, UnitType.Point))
            m_titleColorPicker.BackColor = System.Drawing.Color.White
            m_titleColorPicker.DropDownBorderColor = System.Drawing.Color.Silver
            m_titleColorPicker.DropDownBorderStyle = BorderStyle.Solid
            m_titleColorPicker.DropDownBorderWidth = New Unit(1, UnitType.Pixel)
            m_titleColorPicker.ChosenColor = System.Drawing.Color.White
            m_titleColorPicker.ShowColorNames = False
            m_titleColorPicker.DisplayText = "Title Bar Color:"

            ' Add the title color picker to the web configurator's control's collection.  Note that,
            ' for formatting purposes, we do not add the color picker to the table
            Controls.Add(m_titleColorPicker)

            ' Initialize the background color picker and add it to the web configurator's controls
            m_backgroundColorPicker = New ESRI.ArcGIS.ADF.Web.UI.WebControls.ColorPicker()
            m_backgroundColorPicker.ID = "clrPkrBackground"
            m_backgroundColorPicker.Font.Name = "Verdana"
            m_backgroundColorPicker.Font.Size = New FontUnit(New Unit(8, UnitType.Point))
            m_backgroundColorPicker.BackColor = System.Drawing.Color.White
            m_backgroundColorPicker.DropDownBorderColor = System.Drawing.Color.Silver
            m_backgroundColorPicker.DropDownBorderStyle = BorderStyle.Solid
            m_backgroundColorPicker.DropDownBorderWidth = New Unit(1, UnitType.Pixel)
            m_backgroundColorPicker.ChosenColor = System.Drawing.Color.White
            m_backgroundColorPicker.ShowColorNames = False
            m_backgroundColorPicker.DisplayText = "Background Color:"
            Controls.Add(m_backgroundColorPicker)

            ' Initialize the OK button
            m_okButton = New Button()
            m_okButton.ID = "btnOK"
            m_okButton.Text = "OK"
            AddHandler m_okButton.Click, AddressOf okButton_Click

            ' Add the OK button to a table cell and the cell to a table row
            taskConfiguratorCell = New TableCell()
            taskConfiguratorCell.Controls.Add(m_okButton)
            taskConfiguratorCell.Style(System.Web.UI.HtmlTextWriterStyle.Width) = "50%"
            taskConfiguratorRow = New TableRow()
            taskConfiguratorRow.Controls.Add(taskConfiguratorCell)

            ' Initialize the cancel button
            m_cancelButton = New Button()
            m_cancelButton.ID = "btnCancel"
            m_cancelButton.Text = "Cancel"
            AddHandler m_cancelButton.Click, AddressOf cancelButton_Click

            ' Add the cancel button to a table cell and the cell to a table row
            taskConfiguratorCell = New TableCell()
            taskConfiguratorCell.Controls.Add(m_cancelButton)
            taskConfiguratorCell.Style(System.Web.UI.HtmlTextWriterStyle.Width) = "50%"
            taskConfiguratorRow.Controls.Add(taskConfiguratorCell)

            ' Create a new table to hold the OK and cancel buttons
            taskConfiguratorTable = New Table()
            taskConfiguratorTable.Style(System.Web.UI.HtmlTextWriterStyle.Width) = "100%"
            taskConfiguratorTable.Style(System.Web.UI.HtmlTextWriterStyle.TextAlign) = "center"

            ' Add the row containing the OK and cancel buttons to the new table
            taskConfiguratorTable.Controls.Add(taskConfiguratorRow)

            ' Add the table to the web configurator's controls
            Controls.Add(taskConfiguratorTable)
        End Sub

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

            ' Make sure the configurator is not rendering asynchronously
            If (Not MyBase.IsAsync) Then
                ' load the current properties of the simple task
                loadProperties()
            End If
        End Sub

#End Region

#Region "WebControl Method Overrides"

        Public Overrides Sub Refresh()
            ' load the current properties of the simple server task
            loadProperties()
            MyBase.Refresh()
        End Sub

#End Region

#Region "WebControl Property Overrides"

        ' Specify the task's tag as a div
        Protected Overrides ReadOnly Property TagKey() As System.Web.UI.HtmlTextWriterTag
            Get
                Return System.Web.UI.HtmlTextWriterTag.Div
            End Get
        End Property

#End Region

#End Region

#Region "IWebConfigurator Members"

#Region "IWebConfigurator Properties"

        ' Controls aside from the task and page that may be used by the configurator
        Public Property AdditionalControls() As System.Web.UI.ControlCollection Implements ESRI.ArcGIS.ADF.Web.UI.WebControls.IWebConfigurator.AdditionalControls
            Get
                Return m_additionalControls
            End Get
            Set(ByVal value As System.Web.UI.ControlCollection)
                m_additionalControls = value
            End Set
        End Property

        ' Task instance that the configurator will configure
        Public Property ControlToConfigure() As System.Web.UI.Control Implements ESRI.ArcGIS.ADF.Web.UI.WebControls.IWebConfigurator.ControlToConfigure
            Get
                Return m_simpleTaskInstance
            End Get
            Set(ByVal value As System.Web.UI.Control)
                If Not (TypeOf value Is SimpleServerTask_VBNet) Then
                    Throw New System.ArgumentException()
                End If
                m_simpleTaskInstance = TryCast(value, SimpleServerTask_VBNet)
            End Set
        End Property

#End Region

#Region "IWebConfigurator Methods"

        ' Ensures the resources needed contain the necessary information or functionality.  No resources
        ' are used by this task, so the implementation here is trivial.
        Public Function ValidateResources(<System.Runtime.InteropServices.Out()> ByRef message As String) As Boolean Implements ESRI.ArcGIS.ADF.Web.UI.WebControls.IWebConfigurator.ValidateResources
            message = Nothing
            Return True
        End Function

#End Region

#Region "IWebConfigurator Events"

        ' Associates the internal WebConfigurationComplete event handler instance with the public 
        ' event
        Public Custom Event WebConfigurationCompleted As ESRI.ArcGIS.ADF.Web.UI.WebControls.WebConfigurationCompleteEventHandler Implements ESRI.ArcGIS.ADF.Web.UI.WebControls.IWebConfigurator.WebConfigurationCompleted
            AddHandler(ByVal value As ESRI.ArcGIS.ADF.Web.UI.WebControls.WebConfigurationCompleteEventHandler)
                AddHandler m_onWebConfigurationComplete, value
            End AddHandler
            RemoveHandler(ByVal value As ESRI.ArcGIS.ADF.Web.UI.WebControls.WebConfigurationCompleteEventHandler)
                RemoveHandler m_onWebConfigurationComplete, value
            End RemoveHandler
            RaiseEvent(ByVal sender As System.Object, ByVal args As ESRI.ArcGIS.ADF.Web.UI.WebControls.WebConfigurationCompleteEventArgs)
            End RaiseEvent
        End Event

        ' Associates the internal WebConfigurationCanceled event handler instance with the public
        ' event
        Public Custom Event WebConfigurationCanceled As ESRI.ArcGIS.ADF.Web.UI.WebControls.WebConfigurationCanceledEventHandler Implements ESRI.ArcGIS.ADF.Web.UI.WebControls.IWebConfigurator.WebConfigurationCanceled
            AddHandler(ByVal value As ESRI.ArcGIS.ADF.Web.UI.WebControls.WebConfigurationCanceledEventHandler)
                AddHandler m_onWebConfigurationCancel, value
            End AddHandler
            RemoveHandler(ByVal value As ESRI.ArcGIS.ADF.Web.UI.WebControls.WebConfigurationCanceledEventHandler)
                RemoveHandler m_onWebConfigurationCancel, value
            End RemoveHandler
            RaiseEvent(ByVal sender As System.Object, ByVal args As System.EventArgs)
            End RaiseEvent
        End Event

#End Region

#End Region

#Region "IBuddyControlSupport Members"

        ' Determines what types of controls the control configured by the configurator can be
        ' buddied with.  Here, no types other than the simple task can be buddied with, so the
        ' implementation is trivial.
        Public Function GetSupportedBuddyControlTypes() As System.Type() Implements ESRI.ArcGIS.ADF.Web.UI.WebControls.IBuddyControlSupport.GetSupportedBuddyControlTypes
            Dim types(0) As System.Type
            types(0) = GetType(SimpleServerTask_VBNet)
            Return types
        End Function

#End Region
    End Class
End Namespace