Solved

asp.net add class to div

Posted on 2011-09-26
7
358 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
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…

809 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