在 C# 中动态更改 css 样式?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/628115/
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-04 10:54:23  来源:igfitidea点击:

Dynamically changing css style in c#?

c#asp.net

提问by Xaisoft

I have some link buttons in which I am dynamically adding a style to it. I am doing the following in a method:

我有一些链接按钮,我正在向其中动态添加样式。我在一个方法中执行以下操作:

LinkButton lb = new LinkButton();
lb.Style["font-weight"] = "bold";

When the another link is clicked, it should unbold the link button that is bold and bold the currently clicked one, so in the method that is doing this, I have tried:

当另一个链接被点击时,它应该取消粗体链接按钮并将当前点击的链接按钮加粗,因此在执行此操作的方法中,我尝试过:

lb.Style["font-weight"] = "none";

The above does not work though, the previously selected link stays bold.

上面的方法不起作用,之前选择的链接保持粗体。

I just realized the possible problem. I am creating multiple links and what it looks like is that since all the links are named lb, it never removes the bold. I am trying to think of a way for it to remember the previously selected link and to only unbold that one.

我刚刚意识到可能的问题。我正在创建多个链接,看起来是因为所有链接都被命名为 lb,所以它永远不会删除粗体。我正在想办法让它记住以前选择的链接,并且只取消那个链接。

采纳答案by Armstrongest

Can I suggest an alternative approach?

我可以建议另一种方法吗?

Set a CSS Style:

设置 CSS 样式:

.selected { font-style: bold; }

When a link is clicked set that link's CSS class to "selected" and the others to "";

当一个链接被点击时,将该链接的 CSS 类设置为“selected”,将其他的设置为“”;

EDIT: To accommodate for existing Css Class

编辑:为了适应现有的 Css 类

const string MY_CLASS = "links";
lb1.CssClass = MY_CLASS + " selected"; // selected
lb.CssClass = MY_CLASS; // not selected

You can quickly get into trouble when defining inline styles, in that they're difficult to overwrite.

在定义内联样式时,您很快就会遇到麻烦,因为它们很难被覆盖。

EDIT 2:

编辑2:

Something like this code should work. You may have to loop through all the LinkButtons in the list, but I don't think so. I'd just turn off ViewState on the LinkButtons.

像这样的代码应该可以工作。您可能需要遍历列表中的所有 LinkBut​​ton,但我不这么认为。我只是在 LinkBut​​tons 上关闭 ViewState。

// container for links. so you can reference them 
// outside of the creation method if you wish. I'd probably call this method in the  
// Page_Init Event.

List<LinkButton> listOfLinks = new List<LinkButton>();
const string MY_LB_CLASS = "linkButton"; // generic lb class


private void createSomeLinks() {

    for (int i = 0; i < 10; i++) {
        // create 10 links.
        LinkButton lb = new LinkButton() 
        { 
            ID = "lb" + i, 
            CssClass = MY_LB_CLASS 
        };
        lb.Click += new EventHandler(lb_Click); // Add the click event
    }

    // You can bind the List of LinkButtons here, or do something with them.
}

void lb_Click(Object sender, EventArgs e) {

    LinkButton lb = sender as LinkButton; // cast the sender as LinkButton
    if (lb != null) {
        // Make the link you clicked selected.
        lb.CssClass = MY_LB_CLASS + " selected"; 
    }
}

回答by inspite

Try ListBox1.Attributes.Add("style","font-weight:bold");and ListBox1.Attributes.Add("style","font-weight:normal");

尝试ListBox1.Attributes.Add("style","font-weight:bold");ListBox1.Attributes.Add("style","font-weight:normal");

or even better is

甚至更好的是

// css

// css

.active {font-weight:bold}
.notactive {font-weight:normal}

//c#

//C#

ListBox1.CssClass = "active";
ListBox1.CssClass = "notactive ";

回答by Max Schmeling

Try lb.Style.Remove("font-weight"). I didn't test it, but you can try it out.

试试 lb.Style.Remove("font-weight")。我没有测试过,但你可以试试。

Alternatively, have you tried settings the Font.Bold property?

或者,您是否尝试过设置 Font.Bold 属性?

lb.Font.Bold = true;

回答by M. Mather

you could try lb.Style.Remove("font-weight");

你可以试试 lb.Style.Remove("font-weight");

回答by M. Mather

set the font bold in the click event of the link button and set the enable view state property to false in the click event itself which will reset the link to the normal foam in the other click

在链接按钮的点击事件中设置粗体字体,并在点击事件本身中将启用视图状态属性设置为 false,这将在其他点击中将链接重置为普通泡沫