Common_AddDynamicData_VBNet\PartialPostback_RegisterScriptBlock.aspx.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 Public Partial Class PartialPostback_RegisterScriptBlock Inherits System.Web.UI.Page #Region "Instance Variable Declarations" Private m_CheckBoxId As String = String.Empty Private Const AGSLocalName As String = "AGSLocalMapResource" Private Const AGSInternetName As String = "AGSInternetMapResource" Private Const IMSName As String = "IMSMapResource" #End Region #Region "ASP.NET Web Control Event Handlers" Public Sub CheckBoxList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) ' Declare the variable to hold the callback response Dim callbackResult As String = String.Empty Try ' Get a reference to CheckBoxList1 by casting the sender parameter to CheckBoxList Dim checkBoxList As System.Web.UI.WebControls.CheckBoxList = CType(sender, System.Web.UI.WebControls.CheckBoxList) ' Declare a list object to hold the list of visible map resources Dim visibleResourceList As System.Collections.Generic.List(Of String) ' Check whether the visible resource list exists in the session variables If Session("visibleResources") Is Nothing Then ' The visible resource list isn't among the session variables, so instantiate a ' new list object and assign it to visibleResourceList. visibleResourceList = New System.Collections.Generic.List(Of String)() Else ' Assign the visible resource list session variable to visibleResourceList visibleResourceList = CType(Session("visibleResources"), System.Collections.Generic.List(Of String)) End If ' Iterate through the checkbox list items and add/remove a map resource based ' on which item's selected state disagrees with the visibleResourceList Dim i As Integer = 0 Do While i < checkBoxList.Items.Count ' Get a reference to the current list item Dim listItem As System.Web.UI.WebControls.ListItem = checkBoxList.Items(i) ' Check whether the item's selected property matches whether it is ' contained in the list of visible resources. If the item is selected ' but not in the visible resource list, then the corresponding resource ' needs to be added to the map. If the item is in the visible resource ' list but not selected, then the corresponding resource needs to be ' removed from the map. If listItem.Selected <> visibleResourceList.Contains(listItem.Text) Then Dim resourceName As String = String.Empty ' Assign the resource name variable based on the text of the current list item Select Case listItem.Text Case "ArcGIS Server Local" resourceName = AGSLocalName Case "ArcGIS Server Internet" resourceName = AGSInternetName Case "ArcIMS" resourceName = IMSName End Select ' Call the MapResourceChange method, which adds/removes ' resources to/from the map callbackResult = AddOrRemoveMapResource(resourceName, listItem.Selected) ' Modify the list of visible resources so that it is in sync ' with which resources are visible on the map If visibleResourceList.Contains(listItem.Text) Then visibleResourceList.Remove(listItem.Text) Else visibleResourceList.Add(listItem.Text) End If ' The checked item has been found and action taken accordingly, ' so we can exit the loop iterating through the CheckBoxList items Exit Do End If i += 1 Loop ' Update the visible resource list session variable Session("visibleResources") = visibleResourceList Catch exception As System.Exception Dim errorCallbackResult As ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult = ErrorHandling.GetErrorCallback(exception) Map1.CallbackResults.Add(errorCallbackResult) callbackResult = Map1.CallbackResults.ToString() End Try ' Construct the JavaScript necessary to process the callback results generated by ' adding/removing a resource to/from the map. Note that we need to modify the ' escape sequencing on the default callback results string for the script to execute ' properly, hence the call to Replace on callbackResponse. Dim jsProcessCallbackResult As String = String.Format("ESRI.ADF.System.processCallbackResult('{0}');", callbackResult.Replace("\", "\\")) ' Register the call to processCallbackResult as a script on the client. System.Web.UI.ScriptManager.RegisterClientScriptBlock(Page, sender.GetType(), "updateResources", jsProcessCallbackResult, True) End Sub #End Region #Region "Instance Methods" Private Function AddOrRemoveMapResource(ByVal resourceName As String, ByVal isChecked As Boolean) As String Try Dim mapResourceItemCollection As ESRI.ArcGIS.ADF.Web.UI.WebControls.GISResourceItemCollection(Of ESRI.ArcGIS.ADF.Web.UI.WebControls.MapResourceItem) = MapResourceManager1.ResourceItems Dim mapResourceItem As ESRI.ArcGIS.ADF.Web.UI.WebControls.MapResourceItem = Nothing ' Get current resource item count to determine if the primary map resource needs to be set. Dim mapResourceCount As Integer = mapResourceItemCollection.Count ' If checked, add the resource. If unchecked, remove the resource. If (Not isChecked) Then mapResourceItem = mapResourceItemCollection.Find(resourceName) mapResourceItemCollection.Remove(mapResourceItem) Map1.Refresh() Else mapResourceItem = New ESRI.ArcGIS.ADF.Web.UI.WebControls.MapResourceItem() ' Map resource items consist of a definition and display settings. The definition ' will define the data source and resource parameters. Display settings will define ' map image properties and retrieval types. Dim gisResourceItemDefinition As ESRI.ArcGIS.ADF.Web.UI.WebControls.GISResourceItemDefinition = New ESRI.ArcGIS.ADF.Web.UI.WebControls.GISResourceItemDefinition() ' Check the name of the resource corresponding to the checkbox checked and initialize ' the resource definition accordingly Select Case resourceName Case (AGSLocalName) gisResourceItemDefinition.DataSourceDefinition = "localhost" gisResourceItemDefinition.DataSourceType = "ArcGIS Server Local" gisResourceItemDefinition.ResourceDefinition = "Layers@USA" Case (AGSInternetName) gisResourceItemDefinition.DataSourceDefinition = "http://serverapps.esri.com/arcgis/services/" gisResourceItemDefinition.DataSourceType = "ArcGIS Server Internet" gisResourceItemDefinition.ResourceDefinition = "Layers@SamplesNet/NorthAmerica" Case (IMSName) gisResourceItemDefinition.ResourceDefinition = "states" gisResourceItemDefinition.DataSourceDefinition = "localhost@5300" gisResourceItemDefinition.DataSourceType = "ArcIMS" End Select ' Associate the resource item definition with a map resource item mapResourceItem.Definition = gisResourceItemDefinition ' Set the resource item's name to the passed-in resource name mapResourceItem.Name = resourceName ' Initialize display settings Dim displaySettings As ESRI.ArcGIS.ADF.Web.DisplaySettings = New ESRI.ArcGIS.ADF.Web.DisplaySettings() displaySettings.Transparency = 50.0F displaySettings.Visible = True displaySettings.ImageDescriptor.ImageFormat = ESRI.ArcGIS.ADF.Web.ImageFormat.PNG8 displaySettings.ImageDescriptor.TransparentBackground = True displaySettings.ImageDescriptor.TransparentColor = System.Drawing.Color.White displaySettings.ImageDescriptor.ReturnMimeData = False ' Associate the map resource with the display settings mapResourceItem.DisplaySettings = displaySettings ' Insert the new resource item at the beginning (top). MapResourceManager1.ResourceItems.Insert(0, mapResourceItem) mapResourceItem.InitializeResource() ' Make sure that the resource item initialized properly. Call the ' GetResourceInitFailureCallback error handling method if the resource item ' did not initialize. If mapResourceItem.FailedToInitialize Then Dim exceptionMessage As String = mapResourceItem.InitializationFailure.Message Return ErrorHandling.GetResourceInitFailureCallback(exceptionMessage, m_CheckBoxId) End If End If ' Refresh the Toc and add to Map's callback result collection. Toc1.Refresh() Map1.CallbackResults.CopyFrom(Toc1.CallbackResults) Catch exception As System.Exception Dim errorCallbackResult As ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult = ErrorHandling.GetErrorCallback(exception) Map1.CallbackResults.Add(errorCallbackResult) End Try ' Return the Map's collection of callback results as a string Return Map1.CallbackResults.ToString() End Function #End Region End Class