Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you modify a CSS style in the code behind file for divs in ASP.NET?

Tags:

c#

css

asp.net

I'm trying to modify a CSS style attribute for a div based on the information I get from a database table in the code behind of my aspx page. The following is essentially what I am trying to do, but I get errors.

Aspx:

<div id="testSpace" runat="server">     Test </div> 

Code Behind:

testSpace.Style = "display:none;"     testSpace.Style("display") = "none"; 

What am I doing wrong?

like image 245
EverTheLearner Avatar asked Mar 18 '09 06:03

EverTheLearner


People also ask

How do I change the CSS class for a div in code behind?

If your div has runat="server" then you directly use this: divAllItemList. Attributes["class"] = "hidden"; Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM.

How can we call CSS class in code behind in asp net?

If you want to add attributes, including the class, you need to set runat="server" on the tag. Thanks, I was sure it would be this simple. @Tyler, no. This adds a new class name to the control.

How do you add a style from code behind?

CSS style can be applied from code behind by accessing the properties of the control with the help of its Id. Most of the CSS styles can be applied using the Font property and its sub properties however you can use ForeColor and BackColor directly also.

Where is Div control in asp net code behind?

If you want to find the control from code behind you have to use runat="server" attribute on control. And then you can use Control. FindControl . If you use runat server and your control is inside the ContentPlaceHolder you have to know the ctrl name would not be portlet_tab1 anymore.


1 Answers

testSpace.Style.Add("display", "none"); 
like image 171
Andy White Avatar answered Sep 22 '22 12:09

Andy White