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


Tags: c#,.net,css

Problem :

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>


Solution :

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.


    CSS Howto..

    How does Mozilla create this animated effect?

    How to set the CSS class name dynamically in LessCSS?

    In GWT, how can I override DisclosurePanel's CSS using or ClientBundle

    how to make this “div” always visible to users

    How to get inline-blocks to sit side-by-side?

    How to load CSS filed inside the body in JSP?

    How to prevent this H2-tag from half disappearing?

    How to resize the clockpicker?

    How to create a floating mutlicolumn layout in CSS (Flexbox, float, inline-block)?

    How to use “all” property in CSS

    In CSS, how can I make a div as high as the entire page?

    I can not load the CSS pages which can be accessed after a login carry with spring security. How can I fix it?

    how to change li last anchor color

    How to set min-height for bootstrap container

    How can I make a fixed background image not stay fixed on scroll?

    how to display background color in outlook 2010 html email?

    How to style menu in CSS to look like a button

    How to make a CSS composite scalable

    Wordpress nav menu images not showing up in mobile safari

    Getting an class to show when item is clicked

    Hide categories div block, Expand slideshow 100% from responsive layout

    How to display scroll bar onto a html table

    HTML/CSS: How to get the id of sub menu

    how to position subheader right under header [closed]

    How to make a photo collage using html and css? [closed]

    how to show images with it's original smoothness in IE

    CSS Animation: how to trigger the reverse animation?

    How to format date elements (year, month, date) individually with CSS and Rails i18n?

    CSS flexbox: How to get column items to fill height

    How to edit the CSS of a div that does not contain a H1 tag?