以编程方式添加额外的 CSS 类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6497941/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 00:52:33  来源:igfitidea点击:

Add additional Css class programmatically

asp.netcsscode-behind

提问by Dragan

I have a textbox with a Css class called 'required'. When a user click a button, I'd like to add additional Css Class to the textbox called 'error' without removing the 'required' class. I want to accomplish this from code-behind.

我有一个带有名为“必需”的 Css 类的文本框。当用户单击一个按钮时,我想在不删除“必需”类的情况下向名为“错误”的文本框添加额外的 Css 类。我想从代码隐藏中完成这个。

回答by FSA

I decided to create extension methods for WebControl to have a generic solution. Here's my code:

我决定为 WebControl 创建扩展方法以获得通用解决方案。这是我的代码:

public static class WebControlExtensions
{
    public static void AddCssClass(this WebControl control, string cssClass)
    {
        if (string.IsNullOrEmpty(control.CssClass))
        {
            control.CssClass = cssClass;
        }
        else
        {
            string[] cssClasses = control.CssClass.Split(' ');
            bool classExists = cssClasses.Any(@class => @class == cssClass);

            if (!classExists)
            {
                control.CssClass += " " + cssClass;
            }
        }
    }

    public static void RemoveCssClass(this WebControl control, string cssClass)
    {
        if (!string.IsNullOrEmpty(control.CssClass))
        {
            string[] cssClasses = control.CssClass.Split(' ');
            control.CssClass = string.Join(" ", cssClasses.Where(@class => @class != cssClass).ToArray());
        }
    }
}

回答by Ken Pespisa

You can set the CssClass property of the ASP.NET Textbox control. To add more than one CSS class for an element, just separate it with a space:

您可以设置 ASP.NET 文本框控件的 CssClass 属性。要为一个元素添加多个 CSS 类,只需用空格分隔它:

MyTextBox.CssClass = "class1 class2";

You can put this in your OnClick event handler:

你可以把它放在你的 OnClick 事件处理程序中:

<asp:TextBox ID="MyTextBox" runat="server" OnClick="MyTextBox_Click" />

Then in code-behind:

然后在代码隐藏中:

void MyTextBox_Click(Object sender, EventArgs e) {
    MyTextBox.CssClass = "class1 class2";
}

回答by webtrifusion

Here is a way to remove css class using a function. Adding a class would be very similar.

这是一种使用函数删除 css 类的方法。添加一个类将非常相似。

public void RemoveCssClass(string className)
{
    string[] splitClasses = TextButton.CssClass.Split(' ');
    string separator = "";

    foreach (string _class in splitClasses)
    {
        if (_class != className)
        {
            TextButton.CssClass += separator + _class;
            separator = " ";
        }
    }

    if (TextButton.CssClass == className)
        TextButton.CssClass = "";
}

回答by Bruce Allen

Here's a simple C# method to add or remove a CssClass into a WebControl...

这是一个简单的 C# 方法,用于在 WebControl 中添加或删除 CssClass...

    public static void SetOrRemoveCssClass( WebControl control, string className, bool adding )
    {
        string[] splitClasses = control.CssClass.Split(' ');

        bool hasNow = splitClasses.Contains( className );
        if ( adding && !hasNow )
        {
            control.CssClass += " " + className;
        }
        else if ( !adding && hasNow )   // remove the CssClass attribute
        {
            control.CssClass = control.CssClass.Replace( className, "");
        }
        control.CssClass = control.CssClass.Replace("  "," ").Trim();
    }