C# 更改复选框列表中所选列表项的背景色
我在我的网页中使用复选框列表,如下所示:C# 更改复选框列表中所选列表项的背景色,c#,asp.net,checkboxlist,C#,Asp.net,Checkboxlist,我在我的网页中使用复选框列表,如下所示: <asp:CheckBoxList ID="chklstTelpas" runat="server" RepeatDirection="Horizontal" AutoPostBack="True" Width="594px" OnSelectedIndexChanged="chklstTelpas_SelectedIndexChanged"> <asp:
<asp:CheckBoxList ID="chklstTelpas" runat="server" RepeatDirection="Horizontal"
AutoPostBack="True" Width="594px"
OnSelectedIndexChanged="chklstTelpas_SelectedIndexChanged">
<asp:ListItem Text="TELPAS Speaking" Value="1"></asp:ListItem>
<asp:ListItem Text="TELPAS Listening" Value="2"></asp:ListItem>
<asp:ListItem Text="TELPAS Reading" Value="3"></asp:ListItem>
<asp:ListItem Text="TELPAS Writing" Value="4"></asp:ListItem>
</asp:CheckBoxList>
现在,如果我选中一个列表项,我想为特定的选定项应用一些背景色。如果取消选中,我希望背景颜色与最初显示的颜色保持一致,或者我希望删除背景颜色。您可以在
下拉菜单上的SelectedIndexChanged
事件上执行操作
chklstTelpas.Items[chklstTelpas.SelectedIndex].Attributes.Add("style", "background-color:blue;");
您可以在下拉列表中的SelectedIndexChanged
事件上执行此操作
chklstTelpas.Items[chklstTelpas.SelectedIndex].Attributes.Add("style", "background-color:blue;");
我模模糊糊地记得它可以通过循环项目来完成,对于选中的项目,您可以设置属性
CheckBoxItem.Attributes.Add("Style", "color: red;")
我模模糊糊地记得它可以通过循环项目来完成,对于选中的项目,您可以设置属性
CheckBoxItem.Attributes.Add("Style", "color: red;")
希望这有帮助
希望这有帮助 你可以这样做
for (int i = 0; i < chklstTelpas.Items.Count; i++)
{
if (chklstTelpas.Items[i].Selected)
{
chklstTelpas.Items[i].Attributes.Add("style", "background-color: red;");
}
else
{
chklstTelpas.Items[i].Attributes.Add("style", "background-color: white;");
}
}
for(int i=0;i
这将允许您为多个选项着色。如果你使用SelectedIndex,它只会给出最低的索引。你可以这样做
for (int i = 0; i < chklstTelpas.Items.Count; i++)
{
if (chklstTelpas.Items[i].Selected)
{
chklstTelpas.Items[i].Attributes.Add("style", "background-color: red;");
}
else
{
chklstTelpas.Items[i].Attributes.Add("style", "background-color: white;");
}
}
<asp:CheckBoxList ID="chklstTelpas" runat="server" RepeatDirection="Horizontal"
AutoPostBack="True" Width="594px"
OnSelectedIndexChanged="chklstTelpas_SelectedIndexChanged" CssClass="multiplus">
<asp:ListItem Text="TELPAS Speaking" Value="1"></asp:ListItem>
<asp:ListItem Text="TELPAS Listening" Value="2"></asp:ListItem>
<asp:ListItem Text="TELPAS Reading" Value="3"></asp:ListItem>
<asp:ListItem Text="TELPAS Writing" Value="4"></asp:ListItem>
for(int i=0;i
这将允许您为多个选项着色。如果你使用SelectedIndex,它只会给你最低的索引。一些css会让你达到那里。如果它是一个单一的,我可以继续,但我使用的是列表,那么这怎么可能呢?一些css会让你达到那里。如果它是一个单一的,我可以继续,但我使用的是列表,那么这怎么可能呢
<asp:CheckBoxList ID="chklstTelpas" runat="server" RepeatDirection="Horizontal"
AutoPostBack="True" Width="594px"
OnSelectedIndexChanged="chklstTelpas_SelectedIndexChanged" CssClass="multiplus">
<asp:ListItem Text="TELPAS Speaking" Value="1"></asp:ListItem>
<asp:ListItem Text="TELPAS Listening" Value="2"></asp:ListItem>
<asp:ListItem Text="TELPAS Reading" Value="3"></asp:ListItem>
<asp:ListItem Text="TELPAS Writing" Value="4"></asp:ListItem>