ASP.NET - 设置 CheckBoxList 输入标记“class”属性
我正在使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我知道 jQuery 没有在这里标记,但如果您恰好可以选择使用这个库,这里有一些简单的代码可以实现这一点。
可能有一种方法可以通过扰乱页面生命周期的 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.
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.
使用 CSS,只要该类只是在复选框上设置属性而不是用作某些 JQuery 的选择器,您就可以做到这一点。例如设置一个大复选框的属性:
css文件:
aspx页面:
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:
aspx page: