【问题标题】:CheckBoxList children CSSCheckBoxList 子 CSS
【发布时间】:2012-10-09 08:21:59
【问题描述】:

目前,我有这个:

<asp:CheckBoxList ID="chkSections" runat="server" onselectedindexchanged="chkSections_SelectedIndexChanged"></asp:CheckBoxList>

...生成这个:

<table border="0" id="ctl00_cpBody_chkSections">
    <tbody><tr>
        <td><input type="checkbox" name="ctl00$cpBody$chkSections$0" id="ctl00_cpBody_chkSections_0"><label for="ctl00_cpBody_chkSections_0">All Sections</label></td>
    </tr><tr>
        <td><input type="checkbox" name="ctl00$cpBody$chkSections$1" id="ctl00_cpBody_chkSections_1"><label for="ctl00_cpBody_chkSections_1">Personal Health Status and Health History</label></td>
    </tr><tr>
        <td><input type="checkbox" name="ctl00$cpBody$chkSections$2" id="ctl00_cpBody_chkSections_2"><label for="ctl00_cpBody_chkSections_2">Physical Activity and Fitness</label></td>
    </tr>
</tbody></table>

问题:我希望生成的所有input 字段都有一个特定的类。不想用jQuery。

【问题讨论】:

  • 添加了 ASP.NET 标记(因为您没有使用“经典 ASP”)。还删除了 jQuery 标记,因为您不想使用该框架。添加了 css.
  • @Cyborgx37 FWIW,jquery 是 javascript 语言的框架
  • @BrianWebster - 从技术上讲是的,但它确实看起来独一无二。 :) 更新我的评论。
  • @Cyborgx37 确实,很好的编辑。只是想指出这一点,以防您对框架不太熟悉

标签: c# asp.net css


【解决方案1】:

为什么不在 &lt;asp:CheckList&gt; 上设置 CssClass,在您的 CSS 中使用元素选择器?

<asp:CheckList CssClass="ChLst">
.ChLst INPUT
{
    background-color: red;
}

<!-- OR: -->

.ChLst TD
{
    background-color: red;
}

编辑:
啊,我现在明白了。看到这个问题:Applying Styles To ListItems in CheckBoxList

您可以添加类属性,而不是添加 Style 属性。

li.Attributes.Add("class", "chListItem");

编辑 2:
再想一想,使用Repeater 控件并构建自己的自定义解决方案可能会更好。依赖 CheckBoxList 然后尝试在 jQuery 中使用它显然不适合你。

【讨论】:

  • 我正在使用 jquery validate 插件,它使用输入名称作为选择器进行验证。由于列表生成,每个的name都加1;因此,我无法选择元素并使它们成为验证插件所必需的。
  • 所以,由于我不能为每个输入使用name 属性,我认为最好的方法是为所有复选框添加类"required"
  • 我想是的。我从未使用过 jQuery 验证插件。您可能希望将该规范添加到您的问题中,以便人们更好地了解您要完成的工作。不幸的是,我相信该属性将被添加到TD,而不是INPUT
  • 谢谢。我尝试添加它,但它导致在inputlabel 周围添加&lt;span /&gt;。我似乎找不到针对实际 input 字段的方法。
  • @zeddotes - 就我个人而言,我尽可能避免使用 ASP.NET 控件。我发现他们在任何重要的网站上都花费了我更多的时间来解决问题。我可以使用内联代码(foreach 而不是 Repeater 等)完成我需要的所有事情(使用更简洁的 HTML)
【解决方案2】:

您也可以使用 ListItem 的属性在服务器端设置类名:

protected void Page_Load(object sender, EventArgs e)
{
    foreach (ListItem item in chkSections.Items)
        item.Attributes["class"] = "class_item";
}

【讨论】:

  • 感谢您的帮助。我刚刚尝试过这样做,但该类并未添加到input 和/或label,而是添加到包含input 和`标签的span
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-06-08
  • 1970-01-01
  • 2015-10-31
  • 1970-01-01
  • 1970-01-01
  • 2011-08-18
  • 1970-01-01
相关资源
最近更新 更多