Solved

asp.net add class to div

Posted on 2011-09-26
7
361 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

691 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