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

发布于 2024-11-17 13:05:59 字数 107 浏览 2 评论 0原文

我有一个文本框,其中有一个名为“required”的 Css 类。当用户单击按钮时,我想向名为“error”的文本框添加额外的 Css 类,而不删除“required”类。我想从代码隐藏中完成此任务。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

乄_柒ぐ汐 2024-11-24 13:05:59

我决定为 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());
        }
    }
}

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

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());
        }
    }
}
我纯我任性 2024-11-24 13:05:59

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

MyTextBox.CssClass = "class1 class2";

您可以将其放在 OnClick 事件处理程序中:

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

然后在代码隐藏中:

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

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:

MyTextBox.CssClass = "class1 class2";

You can put this in your OnClick event handler:

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

Then in code-behind:

void MyTextBox_Click(Object sender, EventArgs e) {
    MyTextBox.CssClass = "class1 class2";
}
时光清浅 2024-11-24 13:05:59

这是一种使用函数删除 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 = "";
}

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

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 = "";
}
梦在深巷 2024-11-24 13:05:59

这是一个简单的 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();
    }

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

    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();
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文