Common Custom tasks
Common_CustomTasks_VBNet\EmbeddedResourcesTask_VBNet\EmbeddedResourcesTask.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.
' 

' Include the CustomJavaScript, spinnergreen, and spinnerblack files as WebResources.  This will make each of these
' files available via a Url, which can be retrieved at runtime via a call to Page.ClientScript.GetWebResourceUrl.

Imports Microsoft.VisualBasic
Imports System
<Assembly: System.Web.UI.WebResource("CustomJavaScript.js", "text/javascript")> 
<Assembly: System.Web.UI.WebResource("spinnergreen.gif", "image/gif")> 
<Assembly: System.Web.UI.WebResource("spinnerblack.gif", "image/gif")> 


Namespace EmbeddedResourcesTask_VBNet
  <System.Drawing.ToolboxBitmap(GetType(EmbeddedResourcesTask)), System.Web.UI.ToolboxData("<{0}:EmbeddedResourcesTask runat=""server"" Width=""200px"" Transparency=""35"" " & ControlChars.CrLf & "        BackColor=""White"" TitleBarColor=""WhiteSmoke"" TitleBarSeparatorLine=""True"" TitleBarHeight=""20px"" " & ControlChars.CrLf & "        BorderColor=""LightSteelBlue"" BorderStyle=""Outset"" BorderWidth=""1px"" Font-Names=""Verdana"" " & ControlChars.CrLf & "        Font-Size=""8pt"" ForeColor=""Black""> </{0}:EmbeddedResourcesTask>")> _
  Public Class EmbeddedResourcesTask
    Inherits ESRI.ArcGIS.ADF.Web.UI.WebControls.FloatingPanelTask
    Private _htmlImage As System.Web.UI.HtmlControls.HtmlImage

    #Region "ASP.NET WebControl Life Cycle Overrides"

    Protected Overrides Overloads Sub CreateChildControls()

      MyBase.CreateChildControls()

      ' Instantiate the image and add it to the task's controls
      _htmlImage = New System.Web.UI.HtmlControls.HtmlImage()
      Controls.Add(_htmlImage)

      ' Instantiate the Change Image button and set its text
      Dim changeImageButton As System.Web.UI.HtmlControls.HtmlInputButton = New System.Web.UI.HtmlControls.HtmlInputButton()
      changeImageButton.Value = "Change Image"

      ' Wire a call to the JavaScript changeImage function to the button's click event.
      ' This function is defined in CustomJavaScript.js.
      Dim changeImageJavaScript As String = "changeImage();"
      changeImageButton.Attributes.Add("onclick", changeImageJavaScript)

      ' Add the button to the task's controls
      Controls.Add(changeImageButton)
    End Sub

    #Region "Embedding Images and Custom JavaScript"

    ' Steps:
    '    - Set the "Build Action" property of image and JavaScript files to "Embedded Resource"
    '    - At the top of this class use the WebResource attribute to add the image and JavaScript files as 
    '          exposed resources
    '    - For JavaScript files, during OnPreRender, get the file's resource URL and register the script on 
    '          the page.
    Protected Overrides Overloads Sub OnPreRender(ByVal e As System.EventArgs)
      MyBase.OnPreRender(e)

      ' Check whether the embedded CustomJavaScript file has already been registered
      Dim embeddedJavaScriptKey As String = "embeddedCustomJavaScript"
      If (Not Page.ClientScript.IsClientScriptBlockRegistered(embeddedJavaScriptKey)) Then
        ' Get the URL for the CustomJavaScript file
                Dim customJavaScriptUrl As String = Page.ClientScript.GetWebResourceUrl(GetType(EmbeddedResourcesTask), "CustomJavaScript.js")
        ' Use the web resource URL to register the JavaScript on the page.  Note that we register the
        ' script as a script block - not as a startup script.
        Page.ClientScript.RegisterClientScriptInclude(embeddedJavaScriptKey, customJavaScriptUrl)
      End If

      ' Check whether the JavaScript to be executed on startup has been registered
      Dim startupScriptKey As String = "embeddedResourcesTaskStartupScript"
      If (Not Me.Page.ClientScript.IsStartupScriptRegistered(Me.GetType(), startupScriptKey)) Then
        ' Get the web resource URLs referring to the green and black spinner images
                Dim greenSpinnerUrl As String = Page.ClientScript.GetWebResourceUrl(GetType(EmbeddedResourcesTask), "spinnergreen.gif")
                Dim blackSpinnerUrl As String = Page.ClientScript.GetWebResourceUrl(GetType(EmbeddedResourcesTask), "spinnerblack.gif")

        ' Create JavaScript that will declare page variables storing the image URLs and the image
        ' DOM element and add a call to changeImage to the AJAX init event
                Dim startupJavaScript As String = "                    // Array storing the URLs for the green and black spinner images" & ControlChars.CrLf & "                    var _imageArray = new Array('{0}', '{1}');" & ControlChars.CrLf & "                    // Array storing a reference to the task's image DOM element" & ControlChars.CrLf & "                    var _imageElement = $get('{2}');" & ControlChars.CrLf & "                    " & ControlChars.CrLf & "                    // Specify that a call to changeImage be made during the AJAX init event" & ControlChars.CrLf & "                    Sys.Application.add_init(changeImage);"

        ' Substitute the image URLs and image DOM element ID into the script
        startupJavaScript = String.Format(startupJavaScript, greenSpinnerUrl, blackSpinnerUrl, _htmlImage.ClientID)
        ' Register the JavaScript as a startup script, so it executes during application initialization
        Me.Page.ClientScript.RegisterStartupScript(Me.GetType(), startupScriptKey, startupJavaScript, True)
      End If
    End Sub

    #End Region

    #End Region

    #Region "FloatingPanelTask Members"

    ' All the action (switching images) happens on the client, so we have no need to implement ExecuteTask
    Public Overrides Overloads Sub ExecuteTask()
    End Sub

    ' The task has no resource dependencies, so we implement GetGISResourceItemDependencies to return an empty list
    Public Overrides Overloads Function GetGISResourceItemDependencies() As System.Collections.Generic.List(Of ESRI.ArcGIS.ADF.Web.UI.WebControls.GISResourceItemDependency)
      Return New System.Collections.Generic.List(Of ESRI.ArcGIS.ADF.Web.UI.WebControls.GISResourceItemDependency)()
    End Function

    #End Region
  End Class
End Namespace