Common_AddCustomTool_CSharp\Default.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 Default : System.Web.UI.Page, IBaseToolbarRefresh { #region ASP.NET Page Event Handlers protected void Page_Init(object sender, System.EventArgs e) { // Used to determine if a previous/next action occurred if (!IsPostBack) { Session["previousNext"] = false; Session["previousNextMapHandler"] = false; } } #endregion #region Web ADF Control Event Handlers protected void Map1_ExtentChanged(object sender, ESRI.ArcGIS.ADF.Web.UI.WebControls.ExtentEventArgs extentEventArgs) { // PreviousNext session variable tracks if a Previous or Next command was initiated to change extent if (Session["previousNext"] != null) { try { bool isPreviousNext = (bool)Session["previousNext"]; bool isPreviousNextMapHandler = (bool)Session["previousNextMapHandler"]; // If Previous or Next command, isPreviousNext is true. Reset session variable to false if (isPreviousNext) { // Reset to false Session["previousNext"] = false; } else if (isPreviousNextMapHandler) { Session["previousNextMapHandler"] = false; } // If another event changed the map extent, do the following else { // On initial load, create the hashtable to store extent Envelopes or get session variable System.Collections.Hashtable extentsHashTable = null; if (Session["extentHistory"] != null) { extentsHashTable = (System.Collections.Hashtable)Session["extentHistory"]; } else { extentsHashTable = new System.Collections.Hashtable(); } // Track the index of the current extent, if first extent the index is 0 int currentExtentIndex = 0; if (Session["currentExtentIndex"] != null) { currentExtentIndex = (int)Session["currentExtentIndex"]; } else { Session["currentExtentIndex"] = 0; } // If current index is less than highest index in the extent history, continue int removeFromExtentIndex = 1; if (currentExtentIndex < (extentsHashTable.Count - 1)) { // Since another event triggered the extent change (not Previous or Next) // we need to delete the extents in front of the current index for (int i = extentsHashTable.Count - 1; i > currentExtentIndex; i--) { extentsHashTable.Remove(i); removeFromExtentIndex++; } // Disable NextExtent command Map1.CallbackResults.CopyFrom(RefreshToolbar("nextExtent", true)); } // Add current extent to the hashtable, htExtents.Count is 0 for the first extent // added to the array extentsHashTable.Add(extentsHashTable.Count, extentEventArgs.NewExtent); // Extent index is a 0-based array Session["currentExtentIndex"] = extentsHashTable.Count - 1; // Store extent Envelopes in an hashtable Session["extentHistory"] = extentsHashTable; // Enable PreviousExtent command if number of extents is greater than 1 if (extentsHashTable.Count > 1) { Map1.CallbackResults.CopyFrom(RefreshToolbar("previousExtent", false)); } } } catch (System.Exception exception) { ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult errorCallbackResult = CustomToolLibrary.Utility.GetErrorCallback(exception); Map1.CallbackResults.Add(errorCallbackResult); } } } #endregion #region Instance Methods public ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResultCollection RefreshToolbar(string toolItemName, bool disabled) { try { // Enable\disable toolbar item on the server ESRI.ArcGIS.ADF.Web.UI.WebControls.ToolbarItemCollection toolbarItemCollection = Toolbar2.ToolbarItems; ESRI.ArcGIS.ADF.Web.UI.WebControls.Command command = null; foreach (ESRI.ArcGIS.ADF.Web.UI.WebControls.ToolbarItem toolbarItem in toolbarItemCollection) { if (toolbarItem.Name == toolItemName) { command = (ESRI.ArcGIS.ADF.Web.UI.WebControls.Command)toolbarItem; if (command.Disabled != disabled) { if (disabled) command.Disabled = true; else command.Disabled = false; Toolbar2.Refresh(); // Enable/disable toolbar item on the client (browser) string jsToolbarItemEnable = "var toolbar = Toolbars['" + Toolbar2.ClientID + "']; var toolbaritem = toolbar.items['" + command.Name + "'];toolbaritem.disabled = " + command.Disabled.ToString().ToLower() + ";"; ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult enableToolbarItemCallbackResult = ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult.CreateJavaScript( jsToolbarItemEnable); Toolbar2.CallbackResults.Add(enableToolbarItemCallbackResult); } } } } catch (System.Exception exception) { ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult errorCallbackResult = CustomToolLibrary.Utility.GetErrorCallback(exception); Toolbar2.CallbackResults.Add(errorCallbackResult); } return Toolbar2.CallbackResults; } #endregion }