简体   繁体   English

在后面的代码中将 CSS 类添加到 div

[英]Add CSS class to a div in code behind

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我有一个 div,我试图在代码中向它添加一个 CSS 类,但是当我尝试时收到以下错误

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?任何人都可以帮助我吗?

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:保持 HTML 正确:

    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 ;)您应该将其包装在方法/属性中;)

<div runat="server"> is mapped to a HtmlGenericControl . <div runat="server">映射到HtmlGenericControl Try using BtnventCss.Attributes.Add("class", "hom_but_a");尝试使用BtnventCss.Attributes.Add("class", "hom_but_a");

For a non ASP.NET control, ie HTML controls like div, table, td, tr , etc. you need to first make them a server control, assign an ID, and then assign a property from server code:对于非 ASP.NET 控件,即div, table, td, tr等 HTML 控件,您需要首先使它们成为服务器控件,分配 ID,然后从服务器代码分配属性:

ASPX page ASPX 页面

<head>
    <style type="text/css">
        .top_rounded
        {
            height: 75px;
            width: 75px;
            border: 2px solid;
            border-radius: 5px;
            -moz-border-radius: 5px; /* Firefox 3.6 and earlier */
            border-color: #9c1c1f;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div runat="server" id="myDiv">This is my div</div>
    </form>
</body>

CS page CS页面

myDiv.Attributes.Add("class", "top_rounded");

The Style property gets a collection of all the cascading style sheet (CSS) properties; Style属性获取所有级联样式表 (CSS) 属性的集合; you cannot set it.你不能设置它。

Try BtnventCss.CssClass = "hom_but_a";试试BtnventCss.CssClass = "hom_but_a"; instead.相反。

I'm assuming BtnventCss is a WebControl.我假设 BtnventCss 是一个 WebControl。

I have just seen you're probably using <div runat="server"...我刚刚看到您可能正在使用<div runat="server"...

If so, you can try:如果是这样,您可以尝试:

BtnventCss.Attributes.Add("class", "hom_but_a");

You could make the div an asp:panel - they will render the same and you'll get better server-side support.您可以将 div 设为 asp:panel - 它们将呈现相同的效果,您将获得更好的服务器端支持。

Here are two extension methods you can use.以下是您可以使用的两种扩展方法。 They ensure any existing classes are preserved and do not duplicate classes being added.它们确保保留任何现有类,并且不会重复添加的类。

public static void RemoveCssClass(this WebControl control, String css) {
  control.CssClass = String.Join(" ", control.CssClass.Split(' ').Where(x => x != css).ToArray());
}

public static void AddCssClass(this WebControl control, String css) {
  control.RemoveCssClass(css);
  css += " " + control.CssClass;
  control.CssClass = css;
}

Usage: hlCreateNew.AddCssClass("disabled");用法: hlCreateNew.AddCssClass("disabled");

Usage: hlCreateNew.RemoveCssClass("disabled");用法: hlCreateNew.RemoveCssClass("disabled");

Button1.CssClass += " newClass";

This will not erase your original classes for that control.这不会删除该控件的原始类。 Try this, it should work.试试这个,它应该可以工作。

To ADD classes to html elements see how to add css class to html generic control div?要将类添加到 html 元素,请参阅如何将 css 类添加到 html 通用控件 div? . . There are answers similar to those given here but showing how to add classes to html elements.有类似于此处给出的答案,但显示了如何向html元素添加类。

I'm also looking for the same solution.我也在寻找相同的解决方案。 And I think of我想到

BtnventCss.CssClass = BtnventCss.CssClass + " hom_but_a";

It seems working fine but I'm not sure if it is a good practice似乎工作正常,但我不确定这是否是一个好习惯

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM