Categories: development, sharepoint

An ASP.net AJAX UpdateProgress that can render Inline

I've been starting to pick up ASP.net AJAX again, mainly because as a SharePoint developer I will be "stuck" with WebForms 3.5 for the next few years (SharePoint 2010 will not be a .net 4.0 application). As part of that, I remembered one issue I had: The UpdateProgress renders a div with display:block set, which means it is not possible to have the updateProgress right next to a button. Joe Audette had the right idea: Grab the UpdateProgress from the Mono Project and modify it. The nice thing about the Class Libraries of Mono is that they are licensed under the very liberal MIT X11 license, making them good candidates as a base for modifications.Here is the source code of the UpdateProgressEx, which includes a new Property, DisplayInline. If this is set to true, then we render a span with display: inline. If this is set to false or not set at all, we have the default behavior of a div/block. If DisplayInline is set to true, then we force DynamicLayout to be false. Why? Because if DynamicLayout is true, then it seems like the MS AJAX JavaScript control the display, and they render it as a block. As I did not want to modify it any further, I just force DynamicLayout to false because for me, that works good enough. Also, I haven't tested if it works in visual design mode and if the property panel shows the new DisplayInline Property, as I work only in code view. YMMV, but it's Open Source after all, so feel free to properly fix it :PHere is the code:
// UpdateProgressEx
//
// Author:
//  Michael Stum <website@stum.de>
//  http://www.stum.de/2010/01/28/an-asp-net-ajax-updateprogress-that-can-render-inline
//
// Original Author:
//   Igor Zelmanovich <igorz@mainsoft.com>
//   (C) 2007 Mainsoft, Inc.  http://www.mainsoft.com
//
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Web.UI;

namespace StumDE.WebControls
{
    [PersistChildren(false)]
    [ParseChildren(true)]
    [DefaultProperty("AssociatedUpdatePanelID")]
    [Designer("System.Web.UI.Design.UpdateProgressDesigner, System.Web.Extensions.Design, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35")]
    public class UpdateProgressEx : Control, IScriptControl
    {
        ITemplate _progressTemplate;
        ScriptManager _scriptManager;

        [Category("Behavior")]
        [DefaultValue("")]
        [IDReferenceProperty(typeof(UpdatePanel))]
        public string AssociatedUpdatePanelID
        {
            get
            {
                return (string)ViewState["AssociatedUpdatePanelID"] ?? String.Empty;
            }
            set
            {
                ViewState["AssociatedUpdatePanelID"] = value;
            }
        }

        [Category("Behavior")]
        [DefaultValue(false)]
        public bool DisplayInline
        {
            get
            {
                object o = ViewState["DisplayInline"];
                if (o == null)
                    return false;
                return (bool)o;
            }
            set
            {
                ViewState["DisplayInline"] = value;
            }
        }

        [Category("Behavior")]
        [DefaultValue(500)]
        public int DisplayAfter
        {
            get
            {
                object o = ViewState["DisplayAfter"];
                if (o == null)
                    return 500;
                return (int)o;
            }
            set
            {
                ViewState["DisplayAfter"] = value;
            }
        }

        [Category("Behavior")]
        [DefaultValue(true)]
        public bool DynamicLayout
        {
            get
            {
                // If DisplayInline is set, force dynamic layout to be false
                if(DisplayInline) return false;
                object o = ViewState["DynamicLayout"];
                if (o == null)
                    return true;
                return (bool)o;
            }
            set
            {
                ViewState["DynamicLayout"] = value;
            }
        }

        [PersistenceMode(PersistenceMode.InnerProperty)]
        [Browsable(false)]
        public ITemplate ProgressTemplate
        {
            get
            {
                return _progressTemplate;
            }
            set
            {
                _progressTemplate = value;
            }
        }

        ScriptManager ScriptManager
        {
            get
            {
                if (_scriptManager == null)
                {
                    _scriptManager = ScriptManager.GetCurrent(Page);
                    if (_scriptManager == null)
                        throw new InvalidOperationException(String.Format("The control with ID '{0}' requires a ScriptManager on the page. The ScriptManager must appear before any controls that need it.", ID));
                }
                return _scriptManager;
            }
        }

        protected virtual IEnumerable<ScriptDescriptor> GetScriptDescriptors()
        {
            string updatePanelClientId;
            if (String.IsNullOrEmpty(AssociatedUpdatePanelID))
                updatePanelClientId = null;
            else
            {
                var updatePanel = FindControl(AssociatedUpdatePanelID) as UpdatePanel;
                if (updatePanel == null)
                    throw new InvalidOperationException("No UpdatePanel found for AssociatedUpdatePanelID '" + AssociatedUpdatePanelID + "'.");
                updatePanelClientId = updatePanel.ClientID;
            }
            var descriptor = new ScriptControlDescriptor("Sys.UI._UpdateProgress", this.ClientID);
            descriptor.AddProperty("associatedUpdatePanelId", updatePanelClientId);
            descriptor.AddProperty("displayAfter", DisplayAfter);
            descriptor.AddProperty("dynamicLayout", DynamicLayout);
            descriptor.AddProperty("displayInline", DisplayInline);
            yield return descriptor;
        }

        protected virtual IEnumerable<ScriptReference> GetScriptReferences()
        {
            yield break;
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            ScriptManager.RegisterScriptControl(this);

            if (_progressTemplate == null)
                throw new InvalidOperationException(String.Format("A ProgressTemplate must be specified on UpdateProgress control with ID '{0}'.", ID));

            var container = new Control();
            _progressTemplate.InstantiateIn(container);
            Controls.Add(container);
        }

        protected override void Render(HtmlTextWriter writer)
        {
            if (DynamicLayout)
                writer.AddStyleAttribute(HtmlTextWriterStyle.Display, "none");
            else
            {

                writer.AddStyleAttribute(HtmlTextWriterStyle.Display, DisplayInline ? "inline" : "block");
                writer.AddStyleAttribute(HtmlTextWriterStyle.Visibility, "hidden");
            }
            writer.AddAttribute(HtmlTextWriterAttribute.Id, ClientID);
            if (DisplayInline)
            {
                writer.RenderBeginTag(HtmlTextWriterTag.Span);
            }
            else
            {
                writer.RenderBeginTag(HtmlTextWriterTag.Div);
            }
            base.Render(writer);
            writer.RenderEndTag();

            ScriptManager.RegisterScriptDescriptors(this);
        }

        #region IScriptControl Members

        IEnumerable<ScriptDescriptor> IScriptControl.GetScriptDescriptors()
        {
            return GetScriptDescriptors();
        }

        IEnumerable<ScriptReference> IScriptControl.GetScriptReferences()
        {
            return GetScriptReferences();
        }

        #endregion
    }
}