ControlHelper.cs :  » Bloggers » SubText » Subtext » Web » Controls » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Bloggers » SubText 
SubText » Subtext » Web » Controls » ControlHelper.cs
#region Disclaimer/Info

///////////////////////////////////////////////////////////////////////////////////////////////////
// Subtext WebLog
// 
// Subtext is an open source weblog system that is a fork of the .TEXT
// weblog system.
//
// For updated news and information please visit http://subtextproject.com/
// Subtext is hosted at Google Code at http://code.google.com/p/subtext/
// The development mailing list is at subtext@googlegroups.com 
//
// This project is licensed under the BSD license.  See the License.txt file for more information.
///////////////////////////////////////////////////////////////////////////////////////////////////

#endregion

using System;
using System.IO;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

namespace Subtext.Web.Controls{
    /// <summary>
    /// Static class containing helper methods for various controls 
    /// that can't be placed within the control hierarchy.
    /// </summary>
    public static class ControlHelper
    {
        public static void AddCssClass(HtmlControl control, string cssClass)
        {
            string classes = control.Attributes["class"] ?? string.Empty;

            var regex = new Regex(@"(^|\s)" + Regex.Escape(cssClass) + @"($|\s)", RegexOptions.IgnoreCase);
            if(regex.IsMatch(classes))
            {
                return;
            }

            if(classes.Length > 0)
            {
                control.Attributes["class"] += " " + cssClass;
            }
            else
            {
                control.Attributes["class"] = cssClass;
            }
        }

        public static void AddCssClass(WebControl control, string cssClass)
        {
            string classes = control.CssClass ?? string.Empty;

            var regex = new Regex(@"(^|\s)" + Regex.Escape(cssClass) + @"($|\s)", RegexOptions.IgnoreCase);
            if(regex.IsMatch(classes))
            {
                return;
            }

            if(control.CssClass != null)
            {
                if(control.CssClass.Length > 0)
                {
                    control.CssClass += " " + cssClass;
                }
                else
                {
                    control.CssClass = cssClass;
                }
            }
        }

        /// <summary>
        /// Returns true if the specified attribute is defined 
        /// on the control.
        /// </summary>
        /// <param name="control">Control.</param>
        /// <param name="name">Name.</param>
        /// <returns></returns>
        public static bool IsAttributeDefined(HtmlControl control, string name)
        {
            return !string.IsNullOrEmpty(control.Attributes[name]);
        }

        /// <summary>
        /// Returns true if the specified attribute is defined 
        /// on the control.
        /// </summary>
        /// <param name="control">Control.</param>
        /// <param name="name">Name.</param>
        /// <returns></returns>
        public static bool IsAttributeDefined(WebControl control, string name)
        {
            return !string.IsNullOrEmpty(control.Attributes[name]);
        }

        /// <summary>
        /// Applies the specified control action recursively.
        /// </summary>
        /// <param name="controlAction">The control action.</param>
        /// <param name="root">The root control.</param>
        public static void ApplyRecursively(Action<Control> controlAction, Control root)
        {
            foreach(Control control in root.Controls)
            {
                controlAction(control);
                ApplyRecursively(controlAction, control);
            }
        }

        /// <summary>
        /// Recursively searches for the server form.
        /// </summary>
        /// <param name="parent">The parent to start the recursive search from.</param>
        /// <param name="id">Id of the control to find.</param>
        /// <returns></returns>
        public static Control FindControlRecursively(Control parent, string id)
        {
            foreach(Control child in parent.Controls)
            {
                if(child.ID == id)
                {
                    return child;
                }
                Control foundControl = FindControlRecursively(child, id);
                if(foundControl != null)
                {
                    return foundControl;
                }
            }

            return null;
        }

        /// <summary>
        /// Recursively searches for the server form's client id.
        /// </summary>
        /// <param name="parent">The parent.</param>
        /// <returns></returns>
        public static string GetPageFormClientId(Control parent)
        {
            foreach(Control child in parent.Controls)
            {
                if(child is HtmlForm)
                {
                    return child.ClientID;
                }
                string id = GetPageFormClientId(child);
                if(id != null)
                {
                    return id;
                }
            }

            return null;
        }

        /// <summary>
        /// Exports the specified control to excel.
        /// </summary>
        /// <remarks>
        /// Calling this function will prompt the user with a dialog 
        /// to save the trade blotter grid as an Excel file named 
        /// TradeBlotter.xls.
        /// </remarks>
        public static void ExportToExcel(Control control, string fileName)
        {
            // Set the content type to Excel
            HttpContext.Current.Response.AddHeader("Content-Disposition", "filename=" + fileName);
            HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";

            control.Page.EnableViewState = false;

            //Remove the charset from the Content-Type header
            HttpContext.Current.Response.Charset = String.Empty;

            var writer = new StringWriter();
            var htmlWriter = new HtmlTextWriter(writer);

            //Get the HTML for the control
            control.RenderControl(htmlWriter);

            HttpContext.Current.Response.Write(writer.ToString());
            HttpContext.Current.Response.End();
        }

        public static void SetTitleIfNone(LinkButton link, string title)
        {
            SetTitleIfNoneInternal(link, title);
        }

        public static void SetTitleIfNone(HyperLink link, string title)
        {
            SetTitleIfNoneInternal(link, title);
        }

        /// <summary>
        /// Sets the title attribute for the hyperlink if none exists.
        /// </summary>
        /// <param name="link">The link.</param>
        /// <param name="title">The title.</param>
        private static void SetTitleIfNoneInternal(WebControl link, string title)
        {
            if(link == null)
            {
                throw new ArgumentNullException("link");
            }

            // Use the ToolTip propety since that is rendered as the title attribute.
            if(link.ToolTip.Length == 0)
            {
                link.ToolTip = title;
            }
        }
    }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.