Why is there no set Class on Controls?
It would be very useful to be able to set the class of a Control after it has been dumped as it seems HtmlElement.SetAttribute does nothing.
Creating complex controls using Styles or CssChildRules is really messy.
Comments
-
You can, but you need to use className instead of class:
Util.HtmlHead.AddStyles (".blue { color:blue }"); var btn = new Button ("Blue").Dump(); btn.HtmlElement["className"] = "blue";
This is because after the element has been dumped, the attribute must be set via JavaScript rather than HTML.
-
In the next build, I'll add a CssClass property to the Control class to make this easier.
-
Hi @JoeAlbahari, can you also add
HtmlStyles
to the control, sinceCssChildRules
seems only related to child elements. -
Isn't that what the
Styles
property does? -
Thanks Joe!
-
I use an extension method:
new Literal("h1", "Test").WithClass("text-green-500 text-3xl")
public static class Ext { public static T WithClass<T>(this T control, string classes) where T : Control { control.HtmlElement.SetAttribute("class", classes); return control; } }
-
There's now a CssClass property on the Control class, in the new beta:
https://www.linqpad.net/linqpad6.aspx#beta -
Thanks Joe!