Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Register a style include from code

0.00/5 (No votes)
27 Sep 2010 1  
public static class PageExtensions { private const string styleInclude = $styleInclude; /// /// Registers a client style include. /// /// The page. /// The...
XML
public static class PageExtensions
    {
        private const string styleInclude = "$styleInclude";
        /// <summary>
        /// Registers a client style include.
        /// </summary>
        /// <param name="page">The page.</param>
        /// <param name="type">The type.</param>
        /// <param name="key">The key.</param>
        /// <param name="url">The URL.</param>
        /// <remarks>
        /// The link include will be registered in the header.
        /// The header must be marked as runat="Server"
        /// </remarks>
        public static void RegisterClientStyleInclude(this Page page, Type type, string key, string url)
        {
            var styleLink = new System.Web.UI.HtmlControls.HtmlLink();
            styleLink.Href = url;
            styleLink.Attributes.Add("rel", "stylesheet");
            styleLink.Attributes.Add("type", "text/css");
            page.Header.Controls.Add(styleLink);
            page.Items[styleInclude + type.ToString() + key] = true;
        }
        /// <summary>
        /// Determines whether the client style include is registered.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <param name="page">The page.</param>
        /// <param name="key">The key.</param>
        /// <returns>
        ///     <c>true</c> if the client style include is registered; otherwise, <c>false</c>.
        /// </returns>
        public static bool IsClientStyleIncludeRegistered(this Page page, Type type, string key)
        {
            return page.Items.Contains(styleInclude + type.ToString() + key);
        }
    }

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here