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>
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.
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!
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.
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.
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";
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