Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding css class through aspx code behind

I am using aspx. If I have HTML as follows:

<div id="classMe"></div> 

I am hoping to dynamically add a css class through the code behind file, ie on Page_Load. Is it possible?

like image 593
DanDan Avatar asked Dec 14 '09 21:12

DanDan


People also ask

How can we call CSS class in code behind in asp net?

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.

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 you add a style tag in aspx?

<! DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server">

How do you add CSS to a class?

To select elements with a specific class, write a period (.) character, followed by the name of the class. You can also specify that only specific HTML elements should be affected by a class. To do this, start with the element name, then write the period (.)


1 Answers

If you want to add attributes, including the class, you need to set runat="server" on the tag.

    <div id="classMe" runat="server"></div> 

Then in the code-behind:

classMe.Attributes.Add("class", "some-class") 
like image 76
Chris Haas Avatar answered Nov 03 '22 00:11

Chris Haas