Yes you can - first capture the event using onmouseover , then set the class name using Element. className . If you like to add or remove classes - use the more convenient Element.
Or the HtmlGenericControl is used to control other HTML elements not specified by a specific HTML server control, like <body>, <div>, <span>, <font>, <p>, <ul>, <li> and so on. Suppose you create a menu with submenu items in HTML. In this situation the inner content of the HTML control tags will not changed.
If you want to add attributes, including the class, you need to set runat="server" on the tag. Thanks, I was sure it would be this simple. @Tyler, no. This adds a new class name to the control.
dynDiv.Attributes["class"] = "myCssClass";
To add a class to a div that is generated via the HtmlGenericControl
way you can use:
div1.Attributes.Add("class", "classname");
If you are using the Panel
option, it would be:
panel1.CssClass = "classname";
if you want to add a class to an existing list of classes for an element:
element.Attributes.Add("class", element.Attributes["class"] + " " + sType);
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);
}
}
I think the answer of Curt is correct, however, what if you want to add a class to a div that already has a class declared in the ASP.NET code.
Here is my solution for that, it is a generic method so you can call it directly as this:
Asp Net Div declaration:
<div id="divButtonWrapper" runat="server" class="text-center smallbutton fixPad">
Code to add class:
divButtonWrapper.AddClassToHtmlControl("nameOfYourCssClass")
Generic class:
public static class HtmlGenericControlExtensions
{
public static void AddClassToHtmlControl(this HtmlGenericControl htmlGenericControl, string className)
{
if (string.IsNullOrWhiteSpace(className))
return;
htmlGenericControl
.Attributes.Add("class", string.Join(" ", htmlGenericControl
.Attributes["class"]
.Split(' ')
.Except(new[] { "", className })
.Concat(new[] { className })
.ToArray()));
}
}
If you're going to be repeating this, might as well have an extension method:
// appends a string class to the html controls class attribute
public static void AddClass(this HtmlControl control, string newClass)
{
if (control.Attributes["class"].IsNotNullAndNotEmpty())
{
control.Attributes["class"] += " " + newClass;
}
else
{
control.Attributes["class"] = newClass;
}
}
You don't add the css file to the div, you add a class to it then put your import at the top of the HTML page like so:
<link href="../files/external.css" rel="stylesheet" type="text/css" />
Then add a class like the following to your code: 'myStyle'.
Then in the css file do something like:
.myStyle
{
border-style: 1px solid #DBE0E4;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With