?
Solved

asp.net add class to div

Posted on 2011-09-26
7
Medium Priority
?
367 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 2000 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

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…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses
Course of the Month12 days, 10 hours left to enroll

777 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