Common Simple Server task
Common_SimpleServerTask_CSharp\SimpleServerTask_CSharp.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.
// 

using System;
using System.Collections.Generic;
//using System.Linq;
using System.Text;
using System.Web.UI;
using ESRI.ArcGIS.ADF.Web.UI.WebControls;
using System.ComponentModel;

namespace SimpleServerTask_CSharp
{
    [System.Web.UI.ToolboxData("<{0}:SimpleServerTask_CSharp runat=\"server\" Width=\"100px\" BorderWidth=\"1px\"><TaskResultsContainers><esri:BuddyControl Name=\"TaskResults1\" /></TaskResultsContainers> </{0}:SimpleServerTask_CSharp>")]

    [Designer(typeof(SimpleServerTaskDesigner_CSharp))]

    // Specifies the class used to configure the task in ArcGIS Manager
    [ESRI.ArcGIS.ADF.Web.UI.WebControls.WebConfigurator(typeof(SimpleServerTaskWebConfigurator_CSharp))]

    // Specifies the image used to represent the task in a container (e.g. the Visual Studio toolbox)
    [System.Drawing.ToolboxBitmap(typeof(SimpleServerTask_CSharp))]

    public class SimpleServerTask_CSharp: FloatingPanelTask
    {

        #region Instance Variable Declarations

        private System.Web.UI.HtmlControls.HtmlInputText m_textBox = null;
        private System.Web.UI.HtmlControls.HtmlInputButton m_executeButton = null;                
        
        private string m_taskTextKey = "textValue";        
        #endregion

        #region Instance Properties

        // Specifies that the property will appear in Visual Studio's properties pane
        [System.ComponentModel.Browsable(true)]
        // Specifies the category within which the property will be grouped
        [System.ComponentModel.Category("Appearance")]
        // Specifies the property's default value
        [System.ComponentModel.DefaultValue("Execute")]
        // Specifies that the property be persisted as an attribute
        [System.Web.UI.PersistenceMode(System.Web.UI.PersistenceMode.Attribute)]
        public string ButtonText
        {
            get
            {
                // Attempt to retrieve the button text from state
                object buttonTextObject = StateManager.GetProperty("buttonText");
                // If no button text was stored in state, return the default value.  Otherwise,
                // return the text as a string.
                return (buttonTextObject == null) ? "Execute" : buttonTextObject as string;
            }
            set
            {
                // Store the passed-in value in state
                StateManager.SetProperty("buttonText", value);
            }
        }

        #endregion

        

        // Configures the task's interface
        protected override void CreateChildControls()
        {            
            Controls.Clear();
            base.CreateChildControls();

            // Initialize the task's textbox
            m_textBox = new System.Web.UI.HtmlControls.HtmlInputText();
            m_textBox.ID = "txtTaskText";

            // Add textbox to the task's controls collection
            Controls.Add(m_textBox);

            // Initialize the task's execute button
            m_executeButton = new System.Web.UI.HtmlControls.HtmlInputButton();
            m_executeButton.ID = "btnExecute";
            m_executeButton.Value = ButtonText;

            // Format JavaScript string that will construct the task's callback argument when OK is clicked.
            // For instance, if "523" was typed in the task's textbox, this expression would evaluate to 
            // 'textValue=523'
            string jsGetTaskCallbackArgument = string.Format("'{0}=' + document.getElementById('{1}').value",
                m_taskTextKey, m_textBox.ClientID);

            // Format JavaScript to invoke the ADF's executeTask method when OK is clicked.  Pass the
            // task's argument and callback invocation to the method.

            string jsOnClick = string.Format("executeTask({0},\"{1}\");", jsGetTaskCallbackArgument, CallbackFunctionString);
                       
            // Wire the task execution javascript to the OK button's onclick event and the text box's
            // onkeydown event.
            m_executeButton.Attributes.Add("onclick", jsOnClick);

            // Add the button to the task's controls collection
            Controls.Add(m_executeButton);
        }
     
        public override string GetCallbackResult()
        {
            // Set the task's Input member to the value of the task's textbox 
            System.Collections.Specialized.NameValueCollection keyValColl =
                ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackUtility.ParseStringIntoNameValueCollection(
                CallbackEventArgument);                        
            Input = keyValColl[m_taskTextKey];
            return base.GetCallbackResult();
        }

        public override void ExecuteTask()
        {
            // Clear any left-over task results and make sure task input exists
            Results = null;
            if (Input == null) return;

            // Get the task's textbox value from the Input property
            string textBoxValue = Input as string;

            // Format the heading of the task result to display the current time on the server
            string taskResultHeading = string.Format("The time on the server is {0}",
                System.DateTime.Now.ToShortTimeString());

            // Format the detail of the task result to display the value of the task's textbox when
            // the task was executed
            string taskResultDetail = string.Format("The value in the textbox is: {0}", textBoxValue);

            // Create a simple task result with the heading and detail
            ESRI.ArcGIS.ADF.Web.UI.WebControls.SimpleTaskResult simpleTaskResult =
                new ESRI.ArcGIS.ADF.Web.UI.WebControls.SimpleTaskResult(taskResultHeading, taskResultDetail);

            // Set the task's Results property to the simple task result.  The contents of this property
            // will be passed to the task's results container.
            Results = simpleTaskResult; 
        }
        public override void Refresh()
        {
            string tmp = Input as string;
            if (!string.IsNullOrEmpty(tmp)) m_textBox.Value = tmp;
            base.Refresh();
        }
        
        public override List<GISResourceItemDependency> GetGISResourceItemDependencies()
        {
            System.Collections.Generic.List<ESRI.ArcGIS.ADF.Web.UI.WebControls.GISResourceItemDependency>
               list = new System.Collections.Generic.List<
               ESRI.ArcGIS.ADF.Web.UI.WebControls.GISResourceItemDependency>();
            return list;
        }      
    }
}