Common_AddDynamicData_CSharp\PartialPostback_RegisterScriptBlock.aspx.cs
// 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. // public partial class PartialPostback_RegisterScriptBlock : System.Web.UI.Page { #region Instance Variable Declarations private string m_CheckBoxId = string.Empty; const string AGSLocalName = "AGSLocalMapResource"; const string AGSInternetName = "AGSInternetMapResource"; const string IMSName = "IMSMapResource"; #endregion #region ASP.NET Web Control Event Handlers public void CheckBoxList1_SelectedIndexChanged(object sender, System.EventArgs e) { // Declare the variable to hold the callback response string callbackResult = string.Empty; try { // Get a reference to CheckBoxList1 by casting the sender parameter to CheckBoxList System.Web.UI.WebControls.CheckBoxList checkBoxList = (System.Web.UI.WebControls.CheckBoxList)sender; // Declare a list object to hold the list of visible map resources System.Collections.Generic.List<string> visibleResourceList; // Check whether the visible resource list exists in the session variables if (Session["visibleResources"] == null) // 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<string>(); else // Assign the visible resource list session variable to visibleResourceList visibleResourceList = (System.Collections.Generic.List<string>)Session["visibleResources"]; // Iterate through the checkbox list items and add/remove a map resource based // on which item's selected state disagrees with the visibleResourceList for (int i = 0; i < checkBoxList.Items.Count; i++) { // Get a reference to the current list item System.Web.UI.WebControls.ListItem 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)) { string resourceName = string.Empty; // Assign the resource name variable based on the text of the current list item switch (listItem.Text) { case "ArcGIS Server Local": resourceName = AGSLocalName; break; case "ArcGIS Server Internet": resourceName = AGSInternetName; break; case "ArcIMS": resourceName = IMSName; break; } // 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)) visibleResourceList.Remove(listItem.Text); else visibleResourceList.Add(listItem.Text); // The checked item has been found and action taken accordingly, // so we can exit the loop iterating through the CheckBoxList items break; } } // Update the visible resource list session variable Session["visibleResources"] = visibleResourceList; } catch (System.Exception exception) { ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult errorCallbackResult = ErrorHandling.GetErrorCallback(exception); Map1.CallbackResults.Add(errorCallbackResult); callbackResult = Map1.CallbackResults.ToString(); } // 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. string jsProcessCallbackResult = 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); } #endregion #region Instance Methods string AddOrRemoveMapResource(string resourceName, bool isChecked) { try { ESRI.ArcGIS.ADF.Web.UI.WebControls.GISResourceItemCollection< ESRI.ArcGIS.ADF.Web.UI.WebControls.MapResourceItem> mapResourceItemCollection = MapResourceManager1.ResourceItems; ESRI.ArcGIS.ADF.Web.UI.WebControls.MapResourceItem mapResourceItem = null; // Get current resource item count to determine if the primary map resource needs to be set. int mapResourceCount = mapResourceItemCollection.Count; // If checked, add the resource. If unchecked, remove the resource. if (!isChecked) { 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. ESRI.ArcGIS.ADF.Web.UI.WebControls.GISResourceItemDefinition 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 switch (resourceName) { case (AGSLocalName): gisResourceItemDefinition.DataSourceDefinition = "localhost"; gisResourceItemDefinition.DataSourceType = "ArcGIS Server Local"; gisResourceItemDefinition.ResourceDefinition = "Layers@USA"; break; case (AGSInternetName): gisResourceItemDefinition.DataSourceDefinition = "http://serverapps.esri.com/arcgis/services/"; gisResourceItemDefinition.DataSourceType = "ArcGIS Server Internet"; gisResourceItemDefinition.ResourceDefinition = "Layers@SamplesNet/NorthAmerica"; break; case (IMSName): gisResourceItemDefinition.ResourceDefinition = "states"; gisResourceItemDefinition.DataSourceDefinition = "localhost@5300"; gisResourceItemDefinition.DataSourceType = "ArcIMS"; break; } // 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 ESRI.ArcGIS.ADF.Web.DisplaySettings 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) { string exceptionMessage = mapResourceItem.InitializationFailure.Message; return ErrorHandling.GetResourceInitFailureCallback(exceptionMessage, m_CheckBoxId); } } // Refresh the Toc and add to Map's callback result collection. Toc1.Refresh(); Map1.CallbackResults.CopyFrom(Toc1.CallbackResults); } catch (System.Exception exception) { ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult errorCallbackResult = ErrorHandling.GetErrorCallback(exception); Map1.CallbackResults.Add(errorCallbackResult); } // Return the Map's collection of callback results as a string return Map1.CallbackResults.ToString(); } #endregion }