Html 将样式应用于 CheckBoxList 中的 ListItems
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/104458/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Applying Styles To ListItems in CheckBoxList
提问by Andrew Burgess
How can styles be applied to CheckBoxList ListItems. Unlike other controls, such as the Repeater where you can specify <ItemStyle>
, you can't seem to specify a style for each individual control.
如何将样式应用于 CheckBoxList ListItems。与其他控件(例如可以指定 的 Repeater)不同<ItemStyle>
,您似乎无法为每个单独的控件指定样式。
Is there some sort of work around?
有什么解决办法吗?
回答by Cyberherbalist
You can add Attributes to ListItems programmatically as follows.
您可以按如下方式以编程方式向 ListItems 添加属性。
Say you've got a CheckBoxList and you are adding ListItems. You can add Attributes along the way.
假设您有一个 CheckBoxList 并且您正在添加 ListItems。您可以沿途添加属性。
ListItem li = new ListItem("Richard Byrd", "11");
li.Selected = false;
li.Attributes.Add("Style", "color: red;");
CheckBoxList1.Items.Add(li);
This will make the color of the listitem text red. Experiment and have fun.
这将使列表项文本的颜色变为红色。尝试并玩得开心。
回答by Andrew Burgess
It seems the best way to do this is to create a new CssClass. ASP.NET translates CheckBoxList into a table structure.
似乎最好的方法是创建一个新的 CssClass。ASP.NET 将 CheckBoxList 转换为表结构。
Using something like
使用类似的东西
Style.css
样式文件
.chkboxlist td
{
font-size:x-large;
}
Page.aspx
页面.aspx
<asp:CheckBoxList ID="chkboxlist1" runat="server" CssClass="chkboxlist" />
will do the trick
会做的伎俩
回答by CMPalmer
In addition to Andrew's answer...
除了安德鲁的回答......
Depending on what other attributes you put on a CheckBoxList
or RadioButtonList
, or whatever, ASP.Net will render the output using different structures. For example, if you set RepeatLayout="Flow"
, it won't render as a TABLE, so you have to be careful of what descendant selectors you use in your CSS file.
根据您在 aCheckBoxList
或上放置的其他属性RadioButtonList
,ASP.Net 将使用不同的结构呈现输出。例如,如果您设置RepeatLayout="Flow"
,它不会呈现为 TABLE,因此您必须小心您在 CSS 文件中使用的后代选择器。
In mostcases, you can can just do a "View Source" on your rendered page, maybe on a couple of different browsers, and figure out what ASP.Net is doing. There is a danger, though, that new versions of the server controls or different browsers will render them differently.
在大多数情况下,您可以在呈现的页面上(可能在几个不同的浏览器上)执行“查看源代码”,然后弄清楚 ASP.Net 正在做什么。但是,存在一种危险,即新版本的服务器控件或不同的浏览器会以不同的方式呈现它们。
If you want to style a particular list item or set of list items differently without adding in attributes in the code-behind, you can use CSS attribute selectors. The only drawback to that is that they aren't supported in IE6. jQueryfully supports CSS 3 style attribute selectors, so you could probably also use it for wider browser support.
如果您想以不同的方式设置特定列表项或列表项集的样式而不在代码隐藏中添加属性,则可以使用 CSS 属性选择器。唯一的缺点是它们在 IE6 中不受支持。jQuery完全支持 CSS 3 样式属性选择器,因此您也可以将其用于更广泛的浏览器支持。
回答by Jon White
You can also achieve this in the markup.
您也可以在标记中实现这一点。
<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" />
The word Style will be underlined with the warning that Attribute 'style' is not a valid attribute of element 'ListItem'., but the items are formatted as desired anyway.
样式一词将带有下划线,警告属性“样式”不是元素“ListItem”的有效属性。,但这些项目无论如何都按需要格式化。
回答by SubhoM
You can even have different font styles and color for each word.
您甚至可以为每个单词设置不同的字体样式和颜色。
<asp:ListItem Text="Other (<span style=font-weight:bold;>please </span><span>style=color:Red;font-weight:bold;>specify</span>):" Value="10"></asp:ListItem>
回答by John
public bool Repeater_Bind()
{
RadioButtonList objRadioButton = (RadioButtonList)eventArgs.Item.FindControl("rbList");
if (curQuestionInfo.CorrectAnswer != -1) {
objRadioButton.Items[curQuestionInfo.CorrectAnswer].Attributes.Add("Style", "color: #b4fbb1;");
}
}