I am using checkboxlist in my web page as follows:
<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>
Now if I check a list item, I would like to apply some back开发者_运维技巧ground color for that particular selected item. If i uncheck, I would like the background to remain the same color that was initially displayed, or I would like to remove the background color.
You can do something like this
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;");
}
}
This will allow you to color several of the choices. If you use the SelectedIndex it wil only give you the lowest index.
protected void chklstTelpas_SelectedIndexChanged(object sender, EventArgs e)
{
Control chk = ((Control)sender).FindControl("chk");
CheckBoxList ch=(CheckBoxList) chk;
if (ch.Items[ch.SelectedIndex].Selected)
ch.Items[ch.SelectedIndex].Attributes.Add("Style", "background-color: red;");
}
Hope this helps!!!
You can do on SelectedIndexChanged
Event on DropDown
.
chklstTelpas.Items[chklstTelpas.SelectedIndex].Attributes.Add("style", "background-color:blue;");
i vaguely remember it can be done by looping the items and for the checked item you set the attributes
CheckBoxItem.Attributes.Add("Style", "color: red;")
<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>
add css class to checkboxlist then write jquery code as:
var selectedBox = 0;
var lastChecked = null;
$(document).ready(function () {
$(".multiplus input:checkbox").click(
function () {
if ($(this).attr("checked")) {
$(lastChecked).attr("checked", false).parent().css({ "background-color": "#FFF", "font-weight": "normal" });
lastChecked = this;
}
if ($(this).attr("checked")) {
$(this).parent().css({ "background-color": "#FFC", "font-weight": "bold" });
selectedBox++;
} else {
$(this).parent().css({ "background-color": "#FFF", "font-weight": "normal" });
selectedBox--;
}
}
);
});
精彩评论