代码之家  ›  专栏  ›  技术社区  ›  Andrew Burgess

将样式应用于CheckBoxList中的ListItems

  •  23
  • Andrew Burgess  · 技术社区  · 17 年前

    <ItemStyle> ,似乎无法为每个控件指定样式。

    有什么工作吗?

    6 回复  |  直到 15 年前
        1
  •  27
  •   Cyberherbalist    17 年前

    您可以按如下方式以编程方式向ListItems添加属性。

    假设您有一个复选框列表,并且正在添加列表项。您可以在此过程中添加属性。

    ListItem li = new ListItem("Richard Byrd", "11");
    li.Selected = false;
    li.Attributes.Add("Style", "color: red;");
    CheckBoxList1.Items.Add(li);
    

        2
  •  14
  •   Andrew Burgess    17 年前

    似乎最好的方法是创建一个新的CssClass。ASP.NET将复选框列表转换为表结构。

    Style.css

    .chkboxlist td 
    {
        font-size:x-large;
    }
    

    Page.aspx

    <asp:CheckBoxList ID="chkboxlist1" runat="server" CssClass="chkboxlist" />
    

        3
  •  6
  •   Ulf Gjerdingen angelo.mastro    10 年前

    除了安德鲁的回答。。。

    取决于您在一个应用程序上添加的其他属性 CheckBoxList RadioButtonList Net将使用不同的结构呈现输出。例如,如果设置 RepeatLayout="Flow" ,它不会呈现为表,因此您必须小心在CSS文件中使用的后代选择器。

    在里面 在某些情况下,您可以在呈现的页面上执行“查看源代码”,可能在两种不同的浏览器上执行,然后了解ASP.Net正在做什么。不过,新版本的服务器控件或不同的浏览器将以不同的方式呈现它们,这是一种危险。

    jQuery 完全支持CSS 3样式的属性选择器,因此您可能还可以将其用于更广泛的浏览器支持。

        4
  •  5
  •   marc_s MisterSmith    13 年前

    您也可以在标记中实现这一点。

    <asp:ListItem Text="Good" Value="True" style="background-color:green;color:white" />
    <br />
    <asp:ListItem Text="Bad" Value="False" style="background-color:red;color:white" />
    

    单词样式将在下面加下划线,并警告 属性“style”不是元素“ListItem”的有效属性。 ,但项目的格式仍为所需格式。

        5
  •  2
  •   SubhoM    12 年前

    您甚至可以为每个单词使用不同的字体样式和颜色。

    <asp:ListItem Text="Other (<span style=font-weight:bold;>please </span><span>style=color:Red;font-weight:bold;>specify</span>):" Value="10"></asp:ListItem>
    
        6
  •  1
  •   kratenko Tinku George    14 年前
    public bool Repeater_Bind()
    {
        RadioButtonList objRadioButton = (RadioButtonList)eventArgs.Item.FindControl("rbList");
        if (curQuestionInfo.CorrectAnswer != -1) {
            objRadioButton.Items[curQuestionInfo.CorrectAnswer].Attributes.Add("Style", "color: #b4fbb1;");
        }
    }