Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add CSS class to a div in code behind

Tags:

c#

css

I have a div and I am trying to add a CSS class to it in code but I receive the following error when I try

Property or indexer 'System.Web.UI.HtmlControls.HtmlControl.Style' cannot be assigned to -- it is read only 

I am using the following code:

protected void BTNEvent_Click(object sender, ImageClickEventArgs e) {     BtnventCss.Style= "hom_but_a";                  } 

Can anyone please help me?

like image 585
Myworld Avatar asked Sep 18 '10 14:09

Myworld


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.

Can we add class from CSS?

addClass() - Adds one or more classes to the selected elements. removeClass() - Removes one or more classes from the selected elements. toggleClass() - Toggles between adding/removing classes from the selected elements. css() - Sets or returns the style attribute.

Can I add two class to a div?

HTML elements can belong to more than one class. To define multiple classes, separate the class names with a space, e.g. <div class="city main">. The element will be styled according to all the classes specified.


1 Answers

What if:

 <asp:Button ID="Button1" runat="server" CssClass="test1 test3 test-test" /> 

To add or remove a class, instead of overwriting all classes with

   BtnventCss.CssClass = "hom_but_a" 

keep the HTML correct:

    string classname = "TestClass";      // Add a class     BtnventCss.CssClass = String.Join(" ", Button1                .CssClass                .Split(' ')                .Except(new string[]{"",classname})                .Concat(new string[]{classname})                .ToArray()        );       // Remove a class      BtnventCss.CssClass = String.Join(" ", Button1                .CssClass                .Split(' ')                .Except(new string[]{"",classname})                .ToArray()        ); 

This assures

  • The original classnames remain.
  • There are no double classnames
  • There are no disturbing extra spaces

Especially when client-side development is using several classnames on one element.

In your example, use

   string classname = "TestClass";      // Add a class     Button1.Attributes.Add("class", String.Join(" ", Button1                .Attributes["class"]                .Split(' ')                .Except(new string[]{"",classname})                .Concat(new string[]{classname})                .ToArray()        ));       // Remove a class      Button1.Attributes.Add("class", String.Join(" ", Button1                .Attributes["class"]                .Split(' ')                .Except(new string[]{"",classname})                .ToArray()        )); 

You should wrap this in a method/property ;)

like image 135
Caspar Kleijne Avatar answered Sep 20 '22 18:09

Caspar Kleijne