English

Bug: The Map.CurrentToolItem in .NET Web ADF does not set the current tool

Description

The .NET Web ADF Map control has a CurrentToolItem property that can be used to set the current (active) tool for use in the Map at runtime. This property should match a Toolbar control's CurrentTool property. At runtime, both of these properties should be used to define the current tool to display in the Toolbar and the actions to apply when interacting with the Map. Unfortunately, the Web ADF does not use the Map.CurrentToolItem property to define the active tool for the Map. As a result, the current tool setting is ignored and the initial default current tool (Pan) is used.

Cause

This a bug in the the .NET Web ADF (see the Related Information section below). The Web ADF does not include the internal code necessary to use the Map.CurrentToolItem property to define the active tool for the map.

Workaround

Setting the current tool for a Toolbar and Map on both the client (JavaScript) and server (.NET) requires custom code. In general, it is necessary to define the display and function of the current tool.

To set the current tool when the application first initializes, add the following code to the PreRender event for the page in which the Map and Toolbar controls reside:

Code:

if (!IsPostBack)
{
// Set variables to Map and Toolbar controls in page.
// These references are merely provided for convenience.
ESRI.ArcGIS.ADF.Web.UI.WebControls.Map adfMap = Map1;
ESRI.ArcGIS.ADF.Web.UI.WebControls.Toolbar adfToolbar = Toolbar1;

// Use the Toolbar current tool to set the Map current tool
ESRI.ArcGIS.ADF.Web.UI.WebControls.MapToolItem mapToolItem =
adfMap.ToolItems.Find(adfToolbar.CurrentTool) as ESRI.ArcGIS.ADF.Web.UI.WebControls.MapToolItem;

adfMap.CurrentToolItem = mapToolItem;

string scriptKeyCustom = "customOnLoad";
if (!this.Page.ClientScript.IsStartupScriptRegistered(GetType(), scriptKeyCustom))
{
// Use ADF JavaScript to set the current tool for the map control on the client.
ESRI.ArcGIS.ADF.Web.UI.WebControls.ToolbarItem toolbarItem =
adfToolbar.ToolbarItems.Find(adfToolbar.CurrentTool);

ESRI.ArcGIS.ADF.Web.UI.WebControls.Tool currentTool =
toolbarItem as ESRI.ArcGIS.ADF.Web.UI.WebControls.Tool;

string startupScript = string.Format(@"

function onLoadFunction(){{
var clientAction = Toolbars['{0}'].items['{1}'].clientAction;
var clientFunction = clientAction + ""('{2}', '{1}', {3}, '{4}')"";
eval(clientFunction);
}}

Sys.Application.add_load(onLoadFunction);",
adfToolbar.ClientID,
currentTool.Name,
adfMap.ClientID,
currentTool.ShowLoading.ToString().ToLower(),
currentTool.Cursor);

this.Page.ClientScript.RegisterStartupScript(GetType(), scriptKeyCustom, startupScript, true);
}
}

To set the current tool during an operation at runtime (e.g., return results from a task), use the following code as a guide:

Code:

// Current tool name
string newCurrentTool = "MapPan";

ESRI.ArcGIS.ADF.Web.UI.WebControls.Map adfMap = Map1;
ESRI.ArcGIS.ADF.Web.UI.WebControls.Toolbar adfToolbar = Toolbar1;

// Set Toolbar current tool - server
adfToolbar.CurrentTool = newCurrentTool;

ESRI.ArcGIS.ADF.Web.UI.WebControls.ToolbarItem toolbarItem =
adfToolbar.ToolbarItems.Find(adfToolbar.CurrentTool);

ESRI.ArcGIS.ADF.Web.UI.WebControls.Tool currentTool =
toolbarItem as ESRI.ArcGIS.ADF.Web.UI.WebControls.Tool;

// Set Map Current tool - server
ESRI.ArcGIS.ADF.Web.UI.WebControls.MapToolItem mapToolItem =
adfMap.ToolItems.Find(adfToolbar.CurrentTool) as ESRI.ArcGIS.ADF.Web.UI.WebControls.MapToolItem;

adfMap.CurrentToolItem = mapToolItem;

// Set current tool on the client (sync toolbar and map controls in browser)
string setActiveToolScript = string.Format(@"
// Set toolbar current tool - client
var toolbar = Toolbars['{0}'];
var currentToolField = $get(toolbar.currentToolField);
currentToolField.value = '{1}';
toolbar.selectTool();
toolbar.refreshGroup();

// Set map current tool - server
var clientAction = toolbar.items['{1}'].clientAction;
var clientFunction = clientAction + ""('{2}', '{1}', {3}, '{4}')"";
eval(clientFunction);
",
adfToolbar.ClientID,
currentTool.Name,
adfMap.ClientID,
currentTool.ShowLoading.ToString().ToLower(),
currentTool.Cursor);

ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult cr =
ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult.CreateJavaScript(setActiveToolScript);

Related Information