Common_CustomTasks_VBNet\ScriptTask_VBNet\SimpleScriptTask.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 Namespace ESRI.ADF.Samples.CustomTasks <System.Web.UI.ToolboxData("<{0}:SimpleScriptTask runat=""server"" BackColor=""White""" & ControlChars.CrLf & " BorderColor=""LightSteelBlue"" BorderStyle=""Outset"" BorderWidth=""1px"" Font-Names=""Verdana""" & ControlChars.CrLf & " Font-Size=""8pt"" ForeColor=""Black"" TitleBarColor=""WhiteSmoke"" TitleBarHeight=""20px""" & ControlChars.CrLf & " TitleBarSeparatorLine=""True"" Transparency=""35"" Width=""130px"">" & ControlChars.CrLf & " </{0}:SimpleScriptTask>")> _ Public Class SimpleScriptTask Inherits ESRI.ADF.Samples.CustomTasks.ScriptTask #Region "Instance Variable Declarations" ' Stores the name of the ArcGIS Server host machine to connect to Private Const _hostName As String = "localhost" ' TextBox for users to input a map service name. The ClientMember attribute exposes this ' property on the client-side task object. <ESRI.ADF.Samples.CustomTasks.ScriptTask.ClientMember()> _ Private _mapServiceTextBox As System.Web.UI.WebControls.TextBox ' Button to initiate layer retrieval <ESRI.ADF.Samples.CustomTasks.ScriptTask.ClientMember()> _ Private _getLayersButton As System.Web.UI.HtmlControls.HtmlInputButton ' Div storing the task's activity indicator <ESRI.ADF.Samples.CustomTasks.ScriptTask.ClientMember()> _ Private _retrievingLayersDiv As System.Web.UI.HtmlControls.HtmlGenericControl ' Label for reporting the status of the last layer retrieval operation <ESRI.ADF.Samples.CustomTasks.ScriptTask.ClientMember()> _ Private _statusLabel As System.Web.UI.WebControls.Label #End Region #Region "ASP.NET WebControl Life Cycle Event Overrides - CreateChildControls, Render" Protected Overrides Sub CreateChildControls() MyBase.CreateChildControls() ' Add map service textbox and get layers button CreateMapServiceSection() ' Add status label CreateLabelSection() End Sub Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter) MyBase.Render(writer) ' Create JavaScript to initialize handlers for the client-side task. Note this must be ' done after base.Render is called, as the client-side task will otherwise not be ' instantiated yet. Dim initializeTaskJavaScript As String = "" & ControlChars.CrLf & " // Adds handlers to the client-side task" & ControlChars.CrLf & " function initializeScriptTask() {{" & ControlChars.CrLf & " // Get the task" & ControlChars.CrLf & " var task = $find('{0}');" & ControlChars.CrLf & ControlChars.CrLf & " // Add a handler for the Get Layer button's onclick event" & ControlChars.CrLf & " $addHandler(task.get__getLayersButton(), 'click', onGetLayersClick);" & ControlChars.CrLf & " " & ControlChars.CrLf & " // Add a handler for the onGetLayerNamesComplete event. Note that this" & ControlChars.CrLf & " // event is available because the GetLayerNames server-side method was" & ControlChars.CrLf & " // exposed on the client via the ClientMember attribute. This event " & ControlChars.CrLf & " // fires after a client-side call to GetLayerNames has executed on the" & ControlChars.CrLf & " // server and results have been returned to the client. " & ControlChars.CrLf & " task.add_onGetLayerNamesComplete(updateRetrievalStatus);" & ControlChars.CrLf & " }}" & ControlChars.CrLf & ControlChars.CrLf & " // Fires when the Get Layers button is clicked" & ControlChars.CrLf & " function onGetLayersClick() {{" & ControlChars.CrLf & " // Get the task" & ControlChars.CrLf & " var task = this.get_task();" & ControlChars.CrLf & ControlChars.CrLf & " // Hide the button and show the activity indicator" & ControlChars.CrLf & " task.get__getLayersButton().style.display = 'none';" & ControlChars.CrLf & " task.get__retrievingLayersDiv().style.display = 'inline';" & ControlChars.CrLf & " " & ControlChars.CrLf & " // Call the server-side GetLayerNames function, which has been exposed" & ControlChars.CrLf & " // to the client because the ClientMember attribute was added to " & ControlChars.CrLf & " // GetLayerNames' declaration" & ControlChars.CrLf & " task.GetLayerNames(task.get__mapServiceTextBox().value);" & ControlChars.CrLf & " }} " & ControlChars.CrLf & " " & ControlChars.CrLf & " // Fires after GetLayerNames has been called and results have been returned" & ControlChars.CrLf & " // to the client" & ControlChars.CrLf & " function updateRetrievalStatus(status) {{ " & ControlChars.CrLf & " // Display the passed-in value on the status label" & ControlChars.CrLf & " this.get__statusLabel().style.display = 'inline';" & ControlChars.CrLf & " this.get__statusLabel().innerHTML = status;" & ControlChars.CrLf & ControlChars.CrLf & " // Hide the activity indicator and show the Get Layers button" & ControlChars.CrLf & " this.get__getLayersButton().style.display = 'inline';" & ControlChars.CrLf & " this.get__retrievingLayersDiv().style.display = 'none';" & ControlChars.CrLf & " }} " & ControlChars.CrLf & " " & ControlChars.CrLf & " // Add the initialization function as a handler for the AJAX init event" & ControlChars.CrLf & " Sys.Application.add_init(initializeScriptTask);" ' Substitute the task's client ID into the script and register it on the client initializeTaskJavaScript = String.Format(initializeTaskJavaScript, Me.ClientID) System.Web.UI.ScriptManager.RegisterStartupScript(Me, Me.GetType(), Me.ClientID & "_initializeTask", initializeTaskJavaScript, True) End Sub #End Region #Region "Public Methods - GetLayerNames" ' Retrieves the layers belonging to the map service specified. The inclusion of the ' ClientMember attribute means this method can be called from the client-side task. <ESRI.ADF.Samples.CustomTasks.ScriptTask.ClientMember()> _ Public Function GetLayerNames(ByVal mapServiceName As String) As String ' Holds the text to display on the status label Dim retrievalStatus As String Try ' Connect to the map service Dim mapServiceUrl As String = String.Format("http://{0}/arcgis/services/{1}/MapServer", _hostName, mapServiceName) Dim mapServerProxy As New ESRI.ArcGIS.ADF.ArcGISServer.MapServerProxy(mapServiceUrl) ' Get the map service's info Dim mapServerInfo As ESRI.ArcGIS.ADF.ArcGISServer.MapServerInfo = mapServerProxy.GetServerInfo(mapServerProxy.GetDefaultMapName()) ' Iterate through the map service's layer info objects, adding the name of each ' layer to a string Dim layerList As String = "" For Each mapLayerInfo As ESRI.ArcGIS.ADF.ArcGISServer.MapLayerInfo In mapServerInfo.MapLayerInfos layerList &= mapLayerInfo.Name & "<br />" Next mapLayerInfo ' String to use as the heading of the task result Dim taskResultHeading As String = String.Format("Layers in the {0} map service", mapServiceName) ' Create a simple task result with the heading and list of layers Dim simpleTaskResult As New ESRI.ArcGIS.ADF.Web.UI.WebControls.SimpleTaskResult(taskResultHeading, layerList) ' Get the task's TaskResultsContainer Dim taskResultsContainer As ESRI.ArcGIS.ADF.Web.UI.WebControls.TaskResults = TryCast(ESRI.ArcGIS.ADF.Web.UI.WebControls.Utility.FindControl(Me.TaskResultsContainers(0).Name, Me.Page), ESRI.ArcGIS.ADF.Web.UI.WebControls.TaskResults) ' Display the simple task result taskResultsContainer.DisplayResults(Nothing, Nothing, Nothing, simpleTaskResult) ' Copy the task results callback results to the task so they are processed on the client. ' Without this, the task result wouldn't show up on the client. Me.CallbackResults.CopyFrom(taskResultsContainer.CallbackResults) ' Create a message indicating the operation was successful retrievalStatus = String.Format("Layers for <b>{0}</b> retrieved successfully", mapServiceName) Catch exception As System.Exception ' Create a message indicating the operation was unsuccessful. Include the exception's message. retrievalStatus = String.Format("Error retrieving layers for <b>{0}</b>:<br />{1}", mapServiceName, exception.Message) End Try ' Return the status. For methods declared with the ClientMember attribute, the value returned is ' passed to the handlers of the client-side on<methodName>Complete event. Return retrievalStatus End Function #End Region #Region "Instance Methods for UI creation" ' Creates the map service label, map service textbox, Get Layers button, and activity indicator Private Sub CreateMapServiceSection() ' Table for managing placment of the controls Dim table As New System.Web.UI.WebControls.Table() Me.Controls.Add(table) ' Table row for managing placement of the controls Dim tableRow As New System.Web.UI.WebControls.TableRow() table.Rows.Add(tableRow) ' Table cell to hold the map service label Dim tableCell As New System.Web.UI.WebControls.TableCell() tableRow.Cells.Add(tableCell) ' Initialize the map service label and place it inside a table cell Dim mapServiceLabel As New System.Web.UI.WebControls.Label() mapServiceLabel.Text = "Map Service:" mapServiceLabel.Style(System.Web.UI.HtmlTextWriterStyle.WhiteSpace) = "nowrap" tableCell.Controls.Add(mapServiceLabel) ' Table cell to hold the map service textbox tableCell = New System.Web.UI.WebControls.TableCell() tableRow.Cells.Add(tableCell) ' Instantiate the map service textbox and add it to the cell _mapServiceTextBox = New System.Web.UI.WebControls.TextBox() tableCell.Controls.Add(_mapServiceTextBox) ' Table cell to hold the Get Layers button and activity indicator tableCell = New System.Web.UI.WebControls.TableCell() tableCell.Style(System.Web.UI.HtmlTextWriterStyle.WhiteSpace) = "nowrap" tableRow.Cells.Add(tableCell) ' Initialize the Get Layers button and add it to the cell _getLayersButton = New System.Web.UI.HtmlControls.HtmlInputButton() _getLayersButton.Value = "Get Layers" tableCell.Controls.Add(_getLayersButton) ' Initialize a div to hold the activity indicator and add it to the cell. Specify that the ' div be hidden. _retrievingLayersDiv = New System.Web.UI.HtmlControls.HtmlGenericControl("div") _retrievingLayersDiv.Style(System.Web.UI.HtmlTextWriterStyle.Display) = "none" tableCell.Controls.Add(_retrievingLayersDiv) ' Initialize an image with an activity indicator and add it to the div Dim retrievingLayersImage As New System.Web.UI.WebControls.Image() retrievingLayersImage.ImageUrl = Me.Page.ClientScript.GetWebResourceUrl(GetType(SimpleScriptTask), "ESRI.ADF.Samples.CustomTasks.images.activity_indicator.gif") _retrievingLayersDiv.Controls.Add(retrievingLayersImage) ' Initialize a label to display along with the activity indicator image and add it to the div Dim retrievingLayersLabel As New System.Web.UI.WebControls.Label() retrievingLayersLabel.Text = "Retrieving Layers..." _retrievingLayersDiv.Controls.Add(retrievingLayersLabel) End Sub ' Creates the status label Private Sub CreateLabelSection() ' Table for managing placement of the controls Dim table As New System.Web.UI.WebControls.Table() Me.Controls.Add(table) ' Table row for managing placement of the controls Dim tableRow As New System.Web.UI.WebControls.TableRow() table.Rows.Add(tableRow) ' Table cell to hold the status label Dim tableCell As New System.Web.UI.WebControls.TableCell() tableRow.Cells.Add(tableCell) ' Initialize the status label and add it to the cell _statusLabel = New System.Web.UI.WebControls.Label() _statusLabel.Style(System.Web.UI.HtmlTextWriterStyle.Display) = "none" tableCell.Controls.Add(_statusLabel) End Sub #End Region End Class End Namespace