コード例 #1
0
        // https://stackoverflow.com/a/3742207/4134376

        /// <summary>
        /// Appends the <c>CSS</c> classes passed as parameters to the already existing ones in the control <see cref="WebControl.Attributes" /> collection.
        /// If there is no attribute named <c>"class"</c> in the collection, one will be added.
        /// </summary>
        /// <param name="webControl">The type the method operates on</param>
        /// <param name="classNameParams">The class name to add</param>
        public static void AddClass(this WebControl webControl, params string[] classNameParams)
        {
            IEnumerable <string> classEnumerable = classNameParams;

            if (webControl.Attributes["class"] != null)
            {
                classEnumerable = webControl.GetClassesExcept(classNameParams).Concat(classNameParams);
            }
            webControl.Attributes.Add("class", String.Join(" ", classEnumerable.ToArray()));
        }
コード例 #2
0
        ///// <summary>
        ///// Replaces the <c>CSS</c> classes of the attribute <c>"class"</c> defined in the control <see cref="WebControl.Attributes" /> collection.
        ///// If there is no attribute named <c>"class"</c> in the collection, one will be added.
        ///// </summary>
        ///// <param name="webControl">The type the method operates on</param>
        ///// <param name="classNameParams">The class name to add</param>
        //public static void Class(this WebControl webControl, params string[] classNameParams) {
        //	webControl.Attributes.Add("class", String.Join(" ", classNameParams));
        //}

        ///// <summary>
        ///// Replaces the control <see cref="WebControl.CssClass" /> property <c>CSS</c> classes with the passed as parameters.
        ///// </summary>
        ///// <param name="webControl">The type the method operates on</param>
        ///// <param name="classNameParams">The class name to add</param>
        //public static void CssClass(this WebControl webControl, params string[] classNameParams) {
        //	webControl.CssClass = String.Join(" ", classNameParams);
        //}

        /// <summary>
        /// Remove the <c>CSS</c> classes of the attribute <c>"class"</c> defined in the control <see cref="WebControl.Attributes" /> collection, that match the names passed as parameters.
        /// If the attribute <c>"class"</c> in the collection has no <c>CSS</c> classes it will be removed from the control.
        /// </summary>
        /// <param name="webControl">The type the method operates on</param>
        /// <param name="classNameParams">The class name to remove</param>
        public static void RemoveClass(this WebControl webControl, params string[] classNameParams)
        {
            IEnumerable <string> classEnumerable = classNameParams;

            if (webControl.Attributes["class"] != null)
            {
                classEnumerable = webControl.GetClassesExcept(classNameParams);
            }

            string[] classNames = classEnumerable.ToArray();
            if (classNames.Length > 0)
            {
                webControl.Attributes.Add("class", String.Join(" ", classNames));
            }
            else
            {
                webControl.Attributes.Remove("class");
            }
        }