How to edit CSS style of a div using C# in .NET
Add the runat="server"
attribute to it so you have:
<div id="formSpinner" runat="server">
<img src="images/spinner.gif">
<p>Saving...</p>
</div>
That way you can access the class attribute by using:
formSpinner.Attributes["class"] = "classOfYourChoice";
It's also worth mentioning that the asp:Panel
control is virtually synonymous (at least as far as rendered markup is concerned) with div
, so you could also do:
<asp:Panel id="formSpinner" runat="server">
<img src="images/spinner.gif">
<p>Saving...</p>
</asp:Panel>
Which then enables you to write:
formSpinner.CssClass = "classOfYourChoice";
This gives you more defined access to the property and there are others that may, or may not, be of use to you.
How do you modify a CSS style in the code behind file for divs in ASP.NET?
testSpace.Style.Add("display", "none");
Set the css class of a div using code behind
C#
divControl.Attributes["class"] = "myClass";
VB
divControl.Attributes("class") = "myClass"
You'll need to have a rule like this one on your css file
.myClass
{
height:200px;
/*...more styles*/
}
Change CSS in backend
myDiv.Attributes.CssStyle.Add("color", "white");
This is how I do by adding CSS for the same based on the condition.
Div style changing using asp.net C# code
First give an id to your div like this
<div runat="server" id="div1"></div>
And then in your C# code write this to add style to div.
string style = div1.Style[HtmlTextWriterStyle.Display];
if(style.ToLower()=="none")
div1.Style.Add(HtmlTextWriterStyle.Display, "block");
And here is how you can remove style.
div1.Style.Remove(HtmlTextWriterStyle.Display);
Changing CSS style from ASP.NET code
C#, because I don't want to typo the VB syntax.
Markup:
<div runat="server" id="divControl">...</div>
Class of the Page:
protected System.Web.UI.HtmlControls.HtmlGenericControl divControl;
OnLoad/Other function:
divControl.Style.Add("height", number / anotherNumer);
How do you change the style of a div programmatically
If you want to alter the color of the div with client side code (javascript) running in the browser, you do something like the following:
<script>
var fooElement = document.getElementById("foo");
fooElement.style.color = "red"; //to change the font color
</script>
Add CSS class to a div in code behind
What if:
<asp:Button ID="Button1" runat="server" CssClass="test1 test3 test-test" />
To add or remove a class, instead of overwriting all classes with
BtnventCss.CssClass = "hom_but_a"
keep the HTML correct:
string classname = "TestClass";
// Add a class
BtnventCss.CssClass = String.Join(" ", Button1
.CssClass
.Split(' ')
.Except(new string[]{"",classname})
.Concat(new string[]{classname})
.ToArray()
);
// Remove a class
BtnventCss.CssClass = String.Join(" ", Button1
.CssClass
.Split(' ')
.Except(new string[]{"",classname})
.ToArray()
);
This assures
- The original classnames remain.
- There are no double classnames
- There are no disturbing extra spaces
Especially when client-side development is using several classnames on one element.
In your example, use
string classname = "TestClass";
// Add a class
Button1.Attributes.Add("class", String.Join(" ", Button1
.Attributes["class"]
.Split(' ')
.Except(new string[]{"",classname})
.Concat(new string[]{classname})
.ToArray()
));
// Remove a class
Button1.Attributes.Add("class", String.Join(" ", Button1
.Attributes["class"]
.Split(' ')
.Except(new string[]{"",classname})
.ToArray()
));
You should wrap this in a method/property ;)
Related Topics
How to Format a Datetime in a Different Format
Multipart Forms from C# Client
Algorithm for Simplifying Decimal to Fractions
Css, Images, Js Not Loading in Iis
Getting File Names Without Extensions
Converting String to Double in C#
How to Edit CSS Style of a Div Using C# in .Net
Nullable Type as a Generic Parameter Possible
Passing Variable Between Winforms
Pinvoke for C Function That Returns Char *