ASP.NET - 设置 CheckBoxList 输入标记“class”属性

发布于 2024-12-19 14:57:31 字数 779 浏览 4 评论 0原文

我正在使用 asp.net 4.0 应用程序,我想知道是否可以在由复选框列表控件创建的复选框(输入标记)上设置类属性。

以下代码的作用是将输入标记包装在 span 标记中,并将该类应用于该标记:

foreach (ListItem li in CheckBoxList1.Items)
{
   li.Attributes.Add("class", "check-if-dirty");
}

创建:

<span class="check-if-dirty"><input id="MainContent_CheckBoxList1_0" type="checkbox" name="ctl00$MainContent$CheckBoxList1$0" value="a" /><label for="MainContent_CheckBoxList1_0">a</label></span>

我想要的是:

<span><input class="check-if-dirty" id="MainContent_CheckBoxList1_0" type="checkbox" name="ctl00$MainContent$CheckBoxList1$0" value="a" /><label for="MainContent_CheckBoxList1_0">a</label></span>

谢谢。

I'm working with an asp.net 4.0 application and I am wondering if its possible to set the class attribute on the checkbox (the input tag) created by a checkbox list control.

What the following does is wrap the input tag in a span tag and apply the class to that:

foreach (ListItem li in CheckBoxList1.Items)
{
   li.Attributes.Add("class", "check-if-dirty");
}

Creates:

<span class="check-if-dirty"><input id="MainContent_CheckBoxList1_0" type="checkbox" name="ctl00$MainContent$CheckBoxList1$0" value="a" /><label for="MainContent_CheckBoxList1_0">a</label></span>

What I want is this:

<span><input class="check-if-dirty" id="MainContent_CheckBoxList1_0" type="checkbox" name="ctl00$MainContent$CheckBoxList1$0" value="a" /><label for="MainContent_CheckBoxList1_0">a</label></span>

Thank you.

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

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

发布评论

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

评论(2

无尽的现实 2024-12-26 14:57:31

我知道 jQuery 没有在这里标记,但如果您恰好可以选择使用这个库,这里有一些简单的代码可以实现这一点。

$("span.check-if-dirty").each(function() {
    $(this).children("input").addClass("check-if-dirty");
});

可能有一种方法可以通过扰乱页面生命周期的 PreRender 部分来让 asp.net 完成您想要的操作。如果这个答案不适合您,您可以研究一下。抱歉,我无法提供更好的帮助。

I know jQuery isn't tagged on here, but here's some simple code that will accomplish that, if using this library happens to be an option for you.

$("span.check-if-dirty").each(function() {
    $(this).children("input").addClass("check-if-dirty");
});

There might be a way to get asp.net asp.net to do what you want by messing around in the PreRender part of the page lifecycle. If this answer doesn't work for you, you might look into that. Sorry I can't be a better help.

昇り龍 2024-12-26 14:57:31

使用 CSS,只要该类只是在复选框上设置属性而不是用作某些 JQuery 的选择器,您就可以做到这一点。例如设置一个大复选框的属性:

css文件:

.BigCheckbox input[type="checkbox"] {
    width:20px;
    height:20px;
}

aspx页面:

<asp:CheckBox ID="CheckBoxSelect" runat="server" CssClass="BigCheckbox"/>

Using CSS you can do this as long as the class was just to set attributes on the checkbox and not for use as a selector for some JQuery. eg to set properties for a big checkbox:

css file:

.BigCheckbox input[type="checkbox"] {
    width:20px;
    height:20px;
}

aspx page:

<asp:CheckBox ID="CheckBoxSelect" runat="server" CssClass="BigCheckbox"/>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文