Common Task results
Common_TaskResults_CSharp\TaskResultsWebSite\SelectionSet.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 SelectionSet : System.Web.UI.Page
{
    #region Toc Control Event Handlers - NodesPopulated, NodeSelected

    // Fires after the TOC nodes have been initialized.  Makes layer nodes selectable.
    protected void Toc1_NodesPopulated(object sender, System.EventArgs e)
    {
        foreach (ESRI.ArcGIS.ADF.Web.UI.WebControls.TreeViewPlusNode node in Toc1.Nodes)
            this.MakeLayerNodesSelectable(node);
    }

    // Fires when a TOC node is selected.  Updates the active layer ID and resource name session variables and the
    // selection layer display in the browser's status bar.
    protected void Toc1_NodeSelected(object sender, ESRI.ArcGIS.ADF.Web.UI.WebControls.TreeViewPlusNodeEventArgs args)
    {
        // Make sure the node is a layer node
        if (!(args.Node.Data is ESRI.ArcGIS.ADF.Web.TocLayer))
            return;

        // Update the layer ID session variable
        ESRI.ArcGIS.ADF.Web.TocLayer tocLayer = args.Node.Data as ESRI.ArcGIS.ADF.Web.TocLayer;
        this.Session["ActiveLayerID"] = tocLayer.ID;

        // Get the data frame node containing the layer and use it to update the resource name session variable
        ESRI.ArcGIS.ADF.Web.UI.WebControls.TreeViewPlusNode dataFrameNode =
            this.GetParentDataFrameNode(args.Node.Parent as 
            ESRI.ArcGIS.ADF.Web.UI.WebControls.TreeViewPlusNode);
        this.Session["ActiveResourceName"] = dataFrameNode.Text;

        // Construct JavaScript to display the name of the active layer in the browser's status bar
        string statusBarUpdateScript = string.Format("window.status = 'Selection Layer: {0}'", args.Node.Text);

        // Embed the script in a callback result and add it to the TOC's collection so it is executed on the client
        Toc1.CallbackResults.Add(
            ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult.CreateJavaScript(statusBarUpdateScript));
    }

    #endregion

    #region Private Instance Methods
    
    // Finds TOC layer nodes that are descendants of the passed-in node and makes them selectable
    private void MakeLayerNodesSelectable(ESRI.ArcGIS.ADF.Web.UI.WebControls.TreeViewPlusNode node)
    {
        // Check whether the passed-in node is a layer node and make it selectable if so
        if (node.Data is ESRI.ArcGIS.ADF.Web.TocLayer)
            node.ClickBehavior = ESRI.ArcGIS.ADF.Web.UI.WebControls.TreeViewPlusNodeClickBehavior.Selectable;

        // Recursively call the method on the passed-in node's child nodes
        foreach (ESRI.ArcGIS.ADF.Web.UI.WebControls.TreeViewPlusNode childNode in node.Nodes)
            this.MakeLayerNodesSelectable(childNode);
    }

    // Retrieves the TOC data frame node that is an ancestor of the passed-in node
    private ESRI.ArcGIS.ADF.Web.UI.WebControls.TreeViewPlusNode GetParentDataFrameNode(
        ESRI.ArcGIS.ADF.Web.UI.WebControls.TreeViewPlusNode node)
    {
        // Declare a node variable to hold the function's return value
        ESRI.ArcGIS.ADF.Web.UI.WebControls.TreeViewPlusNode dataFrameNode = null;

        // If the current node is a data frame node, store it in the return variable.  Otherwise, call the
        // method recursively on the node's parent.
        if (node.Data is ESRI.ArcGIS.ADF.Web.TocDataFrame)
            dataFrameNode = node;
        else
            dataFrameNode = this.GetParentDataFrameNode(node.Parent as 
                ESRI.ArcGIS.ADF.Web.UI.WebControls.TreeViewPlusNode);

        return dataFrameNode;
    }

    #endregion
}