【问题标题】:Change the background color of list item selected in check box list更改复选框列表中选定列表项的背景颜色
【发布时间】:2011-06-20 09:26:48
【问题描述】:

我在我的网页中使用复选框列表如下:

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

现在,如果我检查一个列表项,我想为该特定选定项应用一些背景颜色。如果我取消选中,我希望背景保持与最初显示的颜色相同,或者我想删除背景颜色。

【问题讨论】:

  • 一些 css 应该能让你到达那里。
  • 如果是单人我可以继续,但我使用的是列表,所以怎么可能

标签: c# asp.net checkboxlist


【解决方案1】:

你可以这样做

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

这将允许您为多个选项着色。如果你使用 SelectedIndex,它只会给你最低的索引。

【讨论】:

    【解决方案2】:
     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;");
    
        }
    

    希望这会有所帮助!!!

    【讨论】:

      【解决方案3】:

      您可以在SelectedIndexChanged 上进行DropDown 上的活动。

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

      【讨论】:

        【解决方案4】:

        我依稀记得可以通过循环项目来完成,并为选中的项目设置属性

        CheckBoxItem.Attributes.Add("Style", "color: red;")
        

        【讨论】:

          【解决方案5】:
          <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>
          

          将 css 类添加到复选框列表中,然后将 jquery 代码编写为:

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

          【讨论】:

            猜你喜欢
            • 2011-01-09
            • 2015-05-17
            • 1970-01-01
            • 1970-01-01
            • 2016-01-15
            • 2015-12-31
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多