Common Simple UserControlTask
Common_SimpleUserControlTask_VBNET\UserControlConfigurators\SimpleTaskWebConfig\SimpleTaskWebConfigurator.ascx.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.Data
Imports System.Text
Imports System.Configuration
Imports System.Collections.Generic
Imports System.Collections
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Imports ESRI.ArcGIS.ADF.Web.UI.WebControls
Imports ESRI.ArcGIS.ADF.Web
Imports System.Drawing

Partial Public Class SimpleTaskWebConfigurator
    Inherits System.Web.UI.UserControl
    Implements IWebConfigurator, IWebConfiguratorValidation, IDefaultWebConfiguration

    Private defaultText As String = String.Empty
    Private backColor As Color

    Public Sub Page_Load(ByVal sender As Object, ByVal args As EventArgs)
        '    #Region "Populate UI from task's configuration settings"
        If ScriptManager.GetCurrent(Page) IsNot Nothing AndAlso (Not ScriptManager.GetCurrent(Page).IsInAsyncPostBack) Then
            If ControlToConfigure IsNot Nothing AndAlso (Not String.IsNullOrEmpty((TryCast(ControlToConfigure, UserControlTask)).Configuration)) Then
                FromJsonstring((TryCast(ControlToConfigure, UserControlTask)).Configuration)
            End If

            txtLabelText.Text = defaultText
            colorPicker.ChosenColor = backColor
        End If
        '    #End Region
    End Sub

#Region "Design Tag Creation"
    Private Function getMarkup(ByVal additionalControls As ControlCollection) As String ', string defaultConfigurationValue, Color backColor)
        '    #Region "Find task result control to buddy with"
        Dim taskResultControls As New ArrayList()
        If additionalControls IsNot Nothing AndAlso additionalControls.Count > 0 Then
            FindControls(GetType(TaskResults), additionalControls, taskResultControls)
        Else
            FindControls(GetType(TaskResults), Page.Controls, taskResultControls)
        End If

        Dim taskResultsID As String = String.Empty
        If taskResultControls IsNot Nothing AndAlso taskResultControls.Count > 0 Then
            taskResultsID = (TryCast(taskResultControls(0), TaskResults)).ID
        End If
        '    #End Region

        '    #Region "Create tag for task results property"
        Dim taskResultsTag As New StringBuilder()
        If (Not String.IsNullOrEmpty(taskResultsID)) Then
            taskResultsTag.Append("<TaskResultsContainers>")
            taskResultsTag.AppendFormat("<esri:BuddyControl Name=""{0}"" />", taskResultsID)
            taskResultsTag.Append("</TaskResultsContainers>")
        End If
        '    #End Region

        '    #Region "Create markup for the user control task"

        Dim color As String = If((IsNothing(backColor) OrElse backColor = System.Drawing.Color.Transparent OrElse backColor = System.Drawing.Color.Empty), "White", ColorTranslator.ToHtml(backColor))
        Dim str As String = String.Format("<esriTasks:UserControlTask runat=""server"" Width=""200px"" Transparency=""35"" BackColor=""{0}"" " & "TitleBarColor=""WhiteSmoke"" TitleBarSeparatorLine=""False"" TitleBarHeight=""20px"" Visible=""False"" " & "BorderColor=""LightSteelBlue"" BorderStyle=""Outset"" BorderWidth=""1px"" Font-Names=""Verdana"" " & "Font-Size=""8pt"" ForeColor=""Black"" TaskControl=""~/App_Data/UserControlTasks/SimpleTask/SimpleTask.ascx"" Configuration=""{1}"">", color, _controlToConfigure.Configuration) + taskResultsTag.ToString() & "</esriTasks:UserControlTask>"
        Return str
        '    #End Region
    End Function
#End Region

#Region "Event Handlers - On OK and Cancel"
    Protected Sub ok_Clicked(ByVal sender As Object, ByVal args As EventArgs)
        'Fire WebConfigurationCompleted event
        If WebConfigurationCompletedEvent IsNot Nothing Then
            ' parse the json string and set the default values
            _controlToConfigure.Configuration = ToJsonString()
            FromJsonstring(_controlToConfigure.Configuration)
            RaiseEvent WebConfigurationCompleted(sender, New WebConfigurationCompleteEventArgs(_controlToConfigure, getMarkup(AdditionalControls)))
        End If
    End Sub

    Protected Sub cancel_Clicked(ByVal sender As Object, ByVal args As EventArgs)
        'Fire WebConfigurationCanceled event
        RaiseEvent WebConfigurationCanceled(sender, EventArgs.Empty)
    End Sub

    Public Function ToJsonString() As String
        Dim json As New ESRI.ArcGIS.ADF.Web.Display.JsonSerializer(True)
        json.StartObject()
        json.WriteProperty("textboxvalue", txtLabelText.Text)
        json.AddSeparator()
        json.WriteProperty("color", colorPicker.ChosenColor)
        json.EndObject()
        Dim xmlhelper As New ESRI.ArcGIS.ADF.XmlHelper()
        Return xmlhelper.Encode(json.ToString())
    End Function

    'Read of the configuration by parsing JSON string
    Public Sub FromJsonstring(ByVal json As String)
        If String.IsNullOrEmpty(json) Then
            Return
        End If

        Dim xmlhelper As New ESRI.ArcGIS.ADF.XmlHelper()
        Dim decoded As String = xmlhelper.Decode(json)

        Dim jss As New System.Web.Script.Serialization.JavaScriptSerializer()
        Dim deserilizedjson As System.Collections.Generic.IDictionary(Of String, Object) = TryCast(jss.DeserializeObject(decoded), System.Collections.Generic.IDictionary(Of String, Object))
        'set the textbox value by reading the json string from configuration
        If deserilizedjson.ContainsKey("textboxvalue") Then
            defaultText = TryCast(deserilizedjson("textboxvalue"), String)
        End If
        If deserilizedjson.ContainsKey("color") Then
            backColor = System.Drawing.ColorTranslator.FromHtml(TryCast(deserilizedjson("color"), String))
        End If
        Return
    End Sub
#End Region

#Region "IWebConfigurator - Properties"
    Private _additionalControls As ControlCollection
    Public Property AdditionalControls() As System.Web.UI.ControlCollection Implements ESRI.ArcGIS.ADF.Web.UI.WebControls.IWebConfigurator.AdditionalControls
        Get
            Return _additionalControls
        End Get
        Set(ByVal value As System.Web.UI.ControlCollection)
            _additionalControls = value
        End Set
    End Property

    Private _controlToConfigure As UserControlTask
    Public Property ControlToConfigure() As Control Implements ESRI.ArcGIS.ADF.Web.UI.WebControls.IWebConfigurator.ControlToConfigure
        Get
            Return _controlToConfigure
        End Get
        Set(ByVal value As Control)
            If Not (TypeOf value Is UserControlTask) Then
                Throw New ArgumentException()
            End If
            _controlToConfigure = TryCast(value, UserControlTask)
        End Set
    End Property
#End Region

#Region "IWebConfigurator Methods/Events"


    Public Function ValidateResources(ByRef message As String) As Boolean Implements ESRI.ArcGIS.ADF.Web.UI.WebControls.IWebConfigurator.ValidateResources
        ' This task is not dependent on any resources
        message = Nothing
        Return True
    End Function

    Public Event WebConfigurationCanceled As WebConfigurationCanceledEventHandler Implements ESRI.ArcGIS.ADF.Web.UI.WebControls.IWebConfigurator.WebConfigurationCanceled

    Public Event WebConfigurationCompleted As WebConfigurationCompleteEventHandler Implements ESRI.ArcGIS.ADF.Web.UI.WebControls.IWebConfigurator.WebConfigurationCompleted

#End Region

#Region "IConfiguratorValidation Members - an opportunity to validate configuration"

    Public Function IsConfigurationValid(ByRef message As String) As Boolean Implements ESRI.ArcGIS.ADF.Web.UI.WebControls.IWebConfiguratorValidation.IsConfigurationValid
        message = String.Empty
        Return True
    End Function

#End Region

#Region "IDefaultWebConfiguration Members - to allow users to add task without configuring it"

    Public Function GetDefaultTag(ByVal AdditionalControls As ControlCollection) As String Implements ESRI.ArcGIS.ADF.Web.UI.WebControls.IDefaultWebConfiguration.GetDefaultTag
        Return getMarkup(AdditionalControls) ', string.Empty, Color.White);
    End Function
#End Region

#Region "Helper functions"
    Private Shared Sub FindControls(ByVal type As Type, ByVal controls As ControlCollection, ByRef foundControls As ArrayList)
        If controls Is Nothing Then
            Return
        End If
        Dim control As Control
        If foundControls Is Nothing Then
            foundControls = New ArrayList()
        End If
        For i As Integer = 0 To controls.Count - 1
            control = controls(i)
            If type.IsInstanceOfType(control) Then
                foundControls.Add(control)
            End If
            If control.Controls.Count > 0 Then
                FindControls(type, control.Controls, foundControls)
            End If
        Next i
    End Sub
#End Region










End Class