how to add css class to html generic control div?

前端 未结 9 2169
名媛妹妹
名媛妹妹 2020-12-13 08:21

I created a div tag like this:

System.Web.UI.HtmlControls.HtmlGenericControl dynDiv = 
    new System.Web.UI.HtmlControls.HtmlGenericControl(\"DIV\");


        
相关标签:
9条回答
  • 2020-12-13 08:49

    My approach would be:

    /// <summary>
    /// Appends CSS Class seprated by a space character
    /// </summary>
    /// <param name="control">Target control</param>
    /// <param name="cssClass">CSS class name to append</param>
    public static void AppendCss(HtmlGenericControl control, string cssClass)
    {
        // Ensure CSS class is definied
        if (string.IsNullOrEmpty(cssClass)) return;
    
        // Append CSS class
        if (string.IsNullOrEmpty(control.Attributes["class"]))
        {
            // Set our CSS Class as only one
            control.Attributes["class"] = cssClass;
        }
        else
        {
            // Append new CSS class with space as seprator
            control.Attributes["class"] += (" " + cssClass);
        }
    }
    
    0 讨论(0)
  • 2020-12-13 08:50

    if you want to add a class to an existing list of classes for an element:

    element.Attributes.Add("class", element.Attributes["class"] + " " + sType);
    
    0 讨论(0)
  • 2020-12-13 08:51

    Alternative approach if you want to add a class to an existing list of classes of an element:

    element.Attributes["class"] += " myCssClass";
    
    0 讨论(0)
提交回复
热议问题