Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to edit CSS style of a div using C# in .NET

Tags:

c#

.net

css

I'm trying to grab a div's ID in the code behind (C#) and set some css on it. Can I grab it from the DOM or do I have to use some kind of control?

<div id="formSpinner">     <img src="images/spinner.gif" />     <p>Saving...</p> </div> 
like image 303
Jon Smock Avatar asked Oct 06 '08 17:10

Jon Smock


People also ask

How do I change the CSS class for a div in code behind?

If your div has runat="server" then you directly use this: divAllItemList. Attributes["class"] = "hidden"; Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM.

How do I give a div a style?

The <div> tag defines a division or a section in an HTML document. The <div> tag is used as a container for HTML elements - which is then styled with CSS or manipulated with JavaScript. The <div> tag is easily styled by using the class or id attribute. Any sort of content can be put inside the <div> tag!

How do I edit style in CSS?

In the editor, select the theme which you want to edit from “Select theme to edit” drop-down menu. Locate and double-click “Stylesheet (style. css)” under “Theme Files” under “Styles” section. Make the desired changes to the file and click “Update File” to reflect the changes made to the file.


2 Answers

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.

like image 178
Rob Avatar answered Sep 30 '22 14:09

Rob


Make sure that your div is set to runat="server", then simply reference it in the code-behind and set the "class" attribute.

<div runat="server" id="formSpinner">    ...content... </div> 

Code-behind

formSpinner.Attributes["class"] = "class-name"; 
like image 30
tvanfosson Avatar answered Sep 30 '22 13:09

tvanfosson