Common_AddDynamicData_VBNet\Callback.aspx.vb
' Copyright 2011 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 Callback Inherits System.Web.UI.Page Implements System.Web.UI.ICallbackEventHandler #Region "Instance Variable Declarations" Private m_ADFCallbackFunctionString As String Private m_CallbackResults As String = String.Empty 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 Page Event Handlers" Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) ' Add an onclick event for each checkbox For Each checkBox As System.Web.UI.WebControls.ListItem In CheckBoxList1.Items checkBox.Attributes.Add("onclick", "AddOrRemoveMapResource(this)") Next checkBox ' Define the parameters for a browser call to the WebForm_DoCallback JavaScript function ' via the GetCallbackEventReference function. The GetCallbackEventReference function returns ' the syntax for initiating a callback (i.e a call to WebForm_DoCallback) with the passed-in ' parameters. Here the parameters are defined as follows: ' this - the Page will handle the callback request ' "message" - a JavaScript variable named "message" will contain argument-value pairs passed ' to the callback server-side ' "processCallbackResult" - name of the JavaScript function to process the callback ' results from the server. This function is the callback results handler included with ' the Web ADF JavaScript Library. ' "context" - a JavaScript variable named "context" which can be used by the client to ' indicate the origin of the callback ' "postBackError" - name of the JavaScript function that will process errors returned during ' callback processing. This is the callback error handler included with the Web ADF ' JavaScript Library ' true - define whether the callback is synchronous or asynchronous. True is asynchronous. m_ADFCallbackFunctionString = Page.ClientScript.GetCallbackEventReference (Me, "message", "processCallbackResult", "context", "postBackError", True) End Sub #End Region #Region "ICallbackEventHandler Members" ' Parse callback request to determine which checkbox was checked\unchecked and thus which ' resource will be added or removed. Public Sub RaiseCallbackEvent(ByVal eventArgs As String) Implements System.Web.UI.ICallbackEventHandler.RaiseCallbackEvent Try ' Parse the callback string passed from the client to the server into separate ' arguments using the Web ADF's callback parsing utility. Dim nameValueCollection As System.Collections.Specialized.NameValueCollection = Nothing nameValueCollection = ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackUtility.ParseStringIntoNameValueCollection(eventArgs) ' Check whether the EventArg matches that created by the custom callback ' and, if so, add the resource corresponding to the checkbox clicked If nameValueCollection("EventArg") = "ChangeResource" Then ' Get the type of datasource, whether the checkbox was checked or unchecked, ' and the control id of the checkbox clicked from the collection of arguments Dim dataSourceType As String = nameValueCollection("DataSource") Dim isChecked As Boolean = Boolean.Parse(nameValueCollection("IsChecked")) m_CheckBoxId = nameValueCollection("CheckboxID") Dim resourceName As String = String.Empty Select Case dataSourceType ' Set the resource name class variable based on the data source type Case "ArcGIS Server Local" resourceName = AGSLocalName Case "ArcGIS Server Internet" resourceName = AGSInternetName Case "ArcIMS" resourceName = IMSName Case Else End Select ' Call the method to add/remove the resource corresponding to the checkbox clicked m_CallbackResults = AddOrRemoveMapResource(resourceName, isChecked) End If Catch exception As System.Exception Dim errorCallbackResult As ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult = ErrorHandling.GetErrorCallback(exception) Map1.CallbackResults.Add(errorCallbackResult) m_CallbackResults = Map1.CallbackResults.ToString() End Try End Sub ' Return callback result to the browser. Public Function GetCallbackResult() As String Implements System.Web.UI.ICallbackEventHandler.GetCallbackResult Return m_CallbackResults End Function #End Region #Region "Instance Properties" ' Store the callback function string used by the browser to initiate a callback request. ' Added to the aspx page as a server variable - populated at runtime. Public Property ADFCallbackFunctionString() As String Get Return m_ADFCallbackFunctionString End Get Set m_ADFCallbackFunctionString = Value End Set End Property #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.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