Common Custom tasks
Common_CustomTasks_CSharp\CustomTasksWebSite\QueryBuilderTaskWebPage.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 QueryBuilderTaskWebPage : System.Web.UI.Page
{
    protected void Page_Load(object sender, System.EventArgs e)
    {
        ScriptManager1.RegisterAsyncPostBackControl(SwitchMapsButton);
    }

    protected void Page_PreRender(object sender, System.EventArgs eventArgs)
    {
        string scriptKeyCustom = "customDataItemScript";
        // Check whether the data items processing script needs to be registered
        if (!this.Page.ClientScript.IsClientScriptBlockRegistered(GetType(), scriptKeyCustom) &&
        !ScriptManager1.IsInAsyncPostBack)
        {
            // Construct the JavaScript block that will be responsible for processing data items.
            string scriptBlock = @"
                
                function onLoadFunction(){{
                  Sys.WebForms.PageRequestManager.getInstance().add_pageLoading(AsyncResponseHandler);
                }}

                function AsyncResponseHandler(sender, args) {{
                  var dataItems = args.get_dataItems();
                  if (dataItems['{0}'] != null)
                    ESRI.ADF.System.processCallbackResult(dataItems['{0}']);
                }}

                Sys.Application.add_init(onLoadFunction);";

            // Insert the client ID of the page into the script block.  
            scriptBlock = string.Format(scriptBlock, Page.ClientID);

            // Register the script on the client.  
            this.Page.ClientScript.RegisterStartupScript(GetType(), scriptKeyCustom, scriptBlock, true);
        }
    }

    protected void SwitchMapsButton_Click(object sender, System.EventArgs e)
    {
        // Switch the map referenced by the TaskResults and QueryBuilderTask
        if (TaskResults1.Map == Map1.ID)
        {
            TaskResults1.Map = Map2.ID;
            QueryBuilderTask1.Map = Map2.ID;
        }
        else
        {
            TaskResults1.Map = Map1.ID;
            QueryBuilderTask1.Map = Map1.ID;
        }

        // Get any task results stored in session
        ESRI.ArcGIS.ADF.Web.UI.WebControls.TreeViewPlusNode[] savedTaskResultsArray =
            Session["TaskResultsNodes"] as ESRI.ArcGIS.ADF.Web.UI.WebControls.TreeViewPlusNode[];

        // Store the task results of the current map in session
        ESRI.ArcGIS.ADF.Web.UI.WebControls.TreeViewPlusNode[] currentTaskResultArray =
            new ESRI.ArcGIS.ADF.Web.UI.WebControls.TreeViewPlusNode[TaskResults1.Nodes.Count];
        TaskResults1.Nodes.CopyTo(currentTaskResultArray);
        Session["TaskResultsNodes"] = currentTaskResultArray;

        // Clear task results
        TaskResults1.Nodes.Clear();

        // Add saved task results
        if (savedTaskResultsArray != null)
        {
            for (int i = 0; i < savedTaskResultsArray.Length; i++)
                TaskResults1.Nodes.Add(savedTaskResultsArray[i]);
        }

        // Refresh the task results control to show updates
        TaskResults1.Refresh();

        // Construct JavaScript call to update the QueryBuilderTask
        string jsUpdateQueryBuilderTask = string.Format("var task = $find('{0}'); task.getLayers();",
            QueryBuilderTask1.ClientID);
        ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult updateQueryBuilderCallbackResult =
            ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult.CreateJavaScript(jsUpdateQueryBuilderTask);

        // Place callback results in one collection
        ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResultCollection callbackResults = 
            new ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResultCollection();
        callbackResults.CopyFrom(TaskResults1.CallbackResults);
        callbackResults.Add(updateQueryBuilderCallbackResult);

        // Register callback results as a data item so they are processed on the client
        ScriptManager1.RegisterDataItem(Page, callbackResults.ToString(), false);
    }
}