Solved

asp.net add class to div

Posted on 2011-09-26
7
356 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 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 36602305
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
ID: 36602338
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
ID: 36602359
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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 16

Expert Comment

by:disrupt
ID: 36602363
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 75

Expert Comment

by:käµfm³d 👽
ID: 36602518
@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
ID: 36602530
<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
ID: 36707849
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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

821 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