Common Custom tasks
Common_CustomTasks_CSharp\EmbeddedResTask_CSharp\EmbeddedResourcesTask.cs
// Copyright 2011 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.
// 

// Include the CustomJavaScript, spinnergreen, and spinnerblack files as WebResources.  This will make each of these
// files available via a Url, which can be retrieved at runtime via a call to Page.ClientScript.GetWebResourceUrl.
[assembly: System.Web.UI.WebResource("EmbeddedResourcesTask_CSharp.Resources.JS.CustomJavaScript.js", "text/javascript")]
[assembly: System.Web.UI.WebResource("EmbeddedResourcesTask_CSharp.Resources.Images.spinnergreen.gif", "image/gif")]
[assembly: System.Web.UI.WebResource("EmbeddedResourcesTask_CSharp.Resources.Images.spinnerblack.gif", "image/gif")]


namespace EmbeddedResourcesTask_CSharp
{
    [System.Drawing.ToolboxBitmap(typeof(EmbeddedResourcesTask))]
    [System.Web.UI.ToolboxData(@"<{0}:EmbeddedResourcesTask runat=""server"" Width=""200px"" Transparency=""35"" 
        BackColor=""White"" TitleBarColor=""WhiteSmoke"" TitleBarSeparatorLine=""True"" TitleBarHeight=""20px"" 
        BorderColor=""LightSteelBlue"" BorderStyle=""Outset"" BorderWidth=""1px"" Font-Names=""Verdana"" 
        Font-Size=""8pt"" ForeColor=""Black""> </{0}:EmbeddedResourcesTask>")]
    public class EmbeddedResourcesTask : ESRI.ArcGIS.ADF.Web.UI.WebControls.FloatingPanelTask
    {
        private System.Web.UI.HtmlControls.HtmlImage _htmlImage;

        #region ASP.NET WebControl Life Cycle Overrides

        protected override void CreateChildControls()
        {
            base.CreateChildControls();

            // Instantiate the image and add it to the task's controls
            _htmlImage = new System.Web.UI.HtmlControls.HtmlImage();
            Controls.Add(_htmlImage);

            // Instantiate the Change Image button and set its text
            System.Web.UI.HtmlControls.HtmlInputButton changeImageButton = 
                new System.Web.UI.HtmlControls.HtmlInputButton();
            changeImageButton.Value = "Change Image";

            // Wire a call to the JavaScript changeImage function to the button's click event.
            // This function is defined in CustomJavaScript.js.
            string changeImageJavaScript = "changeImage();";
            changeImageButton.Attributes.Add("onclick", changeImageJavaScript);

            // Add the button to the task's controls
            Controls.Add(changeImageButton);
        }

        #region Embedding Images and Custom JavaScript

        // Steps:
        //    - Set the "Build Action" property of image and JavaScript files to "Embedded Resource"
        //    - At the top of this class use the WebResource attribute to add the image and JavaScript files as 
        //          exposed resources
        //    - For JavaScript files, during OnPreRender, get the file's resource URL and register the script on 
        //          the page.
        protected override void OnPreRender(System.EventArgs e)
        {
            base.OnPreRender(e);

            // Check whether the embedded CustomJavaScript file has already been registered
            string embeddedJavaScriptKey = "embeddedCustomJavaScript";
            if (!Page.ClientScript.IsClientScriptBlockRegistered(embeddedJavaScriptKey))
            {
                // Get the URL for the CustomJavaScript file
                string customJavaScriptUrl = Page.ClientScript.GetWebResourceUrl(typeof(EmbeddedResourcesTask), 
                    "EmbeddedResourcesTask_CSharp.Resources.JS.CustomJavaScript.js");
                // Use the web resource URL to register the JavaScript on the page.  Note that we register the
                // script as a script block - not as a startup script.
                Page.ClientScript.RegisterClientScriptInclude(embeddedJavaScriptKey, customJavaScriptUrl);
            }

            // Check whether the JavaScript to be executed on startup has been registered
            string startupScriptKey = "embeddedResourcesTaskStartupScript";
            if (!this.Page.ClientScript.IsStartupScriptRegistered(GetType(), startupScriptKey))
            {
                // Get the web resource URLs referring to the green and black spinner images
                string greenSpinnerUrl = Page.ClientScript.GetWebResourceUrl(typeof(EmbeddedResourcesTask), 
                    "EmbeddedResourcesTask_CSharp.Resources.Images.spinnergreen.gif");
                string blackSpinnerUrl = Page.ClientScript.GetWebResourceUrl(typeof(EmbeddedResourcesTask), 
                    "EmbeddedResourcesTask_CSharp.Resources.Images.spinnerblack.gif");

                // Create JavaScript that will declare page variables storing the image URLs and the image
                // DOM element and add a call to changeImage to the AJAX init event
                string startupJavaScript = @"                    
                    // Array storing the URLs for the green and black spinner images
                    var _imageArray = new Array('{0}', '{1}');
                    // Array storing a reference to the task's image DOM element
                    var _imageElement = $get('{2}');
                    
                    // Specify that a call to changeImage be made during the AJAX init event
                    Sys.Application.add_init(changeImage);";

                // Substitute the image URLs and image DOM element ID into the script
                startupJavaScript = string.Format(startupJavaScript, greenSpinnerUrl, blackSpinnerUrl, 
                    _htmlImage.ClientID);
                // Register the JavaScript as a startup script, so it executes during application initialization
                this.Page.ClientScript.RegisterStartupScript(GetType(), startupScriptKey, startupJavaScript, true);
            }
        }

        #endregion

        #endregion

        #region FloatingPanelTask Members

        // All the action (switching images) happens on the client, so we have no need to implement ExecuteTask
        public override void ExecuteTask()
        { }

        // The task has no resource dependencies, so we implement GetGISResourceItemDependencies to return an empty list
        public override System.Collections.Generic.List<ESRI.ArcGIS.ADF.Web.UI.WebControls.GISResourceItemDependency> 
            GetGISResourceItemDependencies()
        {
            return new System.Collections.Generic.List<ESRI.ArcGIS.ADF.Web.UI.WebControls.GISResourceItemDependency>();
        }

        #endregion
    }
}