如何将 CSS 应用到 ASP.NET CheckBoxList 控件的内部元素
我需要操作 ASP.NET CheckBoxList 控件内 label
元素的属性。每个复选框标签需要有不同的类。我认为最好的选择是事后使用 jQuery 应用这些类。有谁知道更好的方法吗?
我发现这篇文章有点帮助,但是,向列表项添加属性仅将 input
元素和 label
元素包装在具有指定属性的 span
标记中。
I need to manipulate the attributes of a label
element inside an ASP.NET CheckBoxList control. Each checkbox label needs to have a different class. The best alternative I see is to apply the classes with jQuery after the fact. Does anyone know a better method?
I found this post marginally helpful, however, adding an attribute to the list item only wraps the input
element and the label
element in a span
tag with the denoted attributes.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Billy 的第二个答案的变体,但没有服务器端代码:
将类分配给 checkboxlist 本身
您应用于父元素的任何 CSS(在本例中为 checkboxlist,呈现为表格)都可以传递给其子元素:
Variation of Billy's second answer, but without server side code:
Assign the class to checkboxlist itself
Any CSS you apply to the parent element (in this case the checkboxlist, which renders as a table) can be passed to its child elements:
也许您已经考虑过这一点,但您可以尝试使用 ASP.NET 控件适配器。控件适配器允许您更改 ASP.NET 控件生成的 HTML 标记。
查看下面的链接以获取介绍:
Maybe you already thought about it, but you can try to make use of ASP.NET Control Adapters. Control adapters let you change the HTML markup produced by ASP.NET controls.
Checkout the links below for a introduction:
这可能是一种黑客行为,因为我不熟练使用 CSS,但它对我有用。
对复选框列表中的每个列表项执行 a 操作,为每个列表项添加一个 ID 属性。这将为包装标签标记的每个范围分配一个 ID。您将需要为每个列表项分配不同的 id,因为您说过每个列表项都需要不同的类。
现在,您需要为此 id 添加 CSS 样式,以便将其应用于 label 标记。
另一种方法是执行与上面相同的操作,但为每个列表项分配一个类,并且该类仅应用于标签标记。
将类分配给每个列表项
然后添加 CSS
This might be kind of a hack as I'm not skilled w/ CSS but it worked for me.
Do a for each on each of the list items in the check box list, add an ID attribute to each one. This will assign an ID to each span that is wrapping the label tag. You will want to assign a different id to each list item since you said each one needs a different class.
Now you'll need to add a CSS style for this id so it applies to the label tag.
Another way would be to do the same thing as above but assign a class to each list item and have the class only apply to the label tag.
Assign the class to each list item
And then add your CSS
所以我看到了几种不同的方法来做到这一点。第一个是通过 CSS 类。
服务器代码:
正如您所说,这将产生一个包含标签和输入元素的范围。所以,你的 css 应该是:
并且你可以根据需要重复这个。
另一种方法是 jQuery 方法。我不认为这种方法有什么问题,但在你的问题中,你负面地提到了这种方法。所以我假设你知道怎么做,但只是不想这样做。我提到它只是为了说这不是一个坏方法,如果其他方法都失败了,您可能需要重新考虑:)。
So I see several different ways of doing this. The first is through css classes.
Server Code:
This will result in, like you said, a span containing the label and input elements. So, you're css would be:
and you would repeat this as needed.
Another approach would be the jQuery approach. I don't see anything wrong with this approach, but in you're question, you mention this approach negatively. So I'll assume you know how to do it but just don't want to. I just mention it to say it's not a bad approach, and if all else fails, you might want to reconsider :).