简体   繁体   中英

Change the background color of list item selected in check box list

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 background 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!!!

您可以对DropDown SelectedIndexChanged事件进行操作。

chklstTelpas.Items[chklstTelpas.SelectedIndex].Attributes.Add("style", "background-color:blue;");

我模糊地记得可以通过循环这些项目来完成,对于选中的项目,您可以设置属性

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--;
                    }
                }
            );
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM