how to add css class to html generic control div?

前端 未结 9 2210
名媛妹妹
名媛妹妹 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:

    /// 
    /// Appends CSS Class seprated by a space character
    /// 
    /// Target control
    /// CSS class name to append
    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);
        }
    }
    

提交回复
热议问题