Solved

asp.net add class to div

Posted on 2011-09-26
7
352 Views
Last Modified: 2012-05-12
I have a div that I want to add a css class "hideme" via code.  The id of the div is "mydiv".
How do I do this?
0
Comment
Question by:jackjohnson44
  • 4
  • 2
7 Comments
 
LVL 74

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
Comment Utility
If you have the runat="server" set on the <div>, then you can do:

this.mydiv.Attributes.Add("class", "hideme");

Open in new window

0
 
LVL 16

Expert Comment

by:disrupt
Comment Utility
Let's say you had a button you just do this:

<asp:Button ID="Button1" runat="server" CssClass="hideme" />

or in code behind

Button1.CssClass = "hideme";
0
 
LVL 16

Expert Comment

by:disrupt
Comment Utility
for regular div you can do this

<div runat="server" id="div1">...</div>

codebehind:
protected System.Web.UI.HtmlControls.HtmlGenericControl div1l;
div1.Styles.Add("class", "hideme");
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 16

Expert Comment

by:disrupt
Comment Utility
fix:

for regular div you can do this

<div runat="server" id="div1">...</div>

codebehind:
protected System.Web.UI.HtmlControls.HtmlGenericControl div1;
div1.Styles.Add("class", "hideme");

Open in new window

0
 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
@disrupt

This:

div1.Styles.Add("class", "hideme");

Open in new window


is not correct because class is not a valid CSS attribute. You would want to use Attributes vs Styles, as I demonstrated above.
0
 
LVL 16

Expert Comment

by:disrupt
Comment Utility
<div runat="server" id="div1"></div>

Yes my mistake should be like so:
protected System.Web.UI.HtmlControls.HtmlGenericControl div1;
div1.Attributes.Add("class", "hideme");

Open in new window

0
 
LVL 2

Expert Comment

by:yogsoft
Comment Utility
Make sure that your div has attribute runat="server" and access the same div in code-behind using its id and add "class" attribute to it.

Note: If your div is inside GridView or DataGrid, you need to cast div to HtmlGenericControl and then add "class" attribute to it in RowDataBound/ItemDataBound event.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now