【问题标题】:How to disable a particular CheckBoxList ListItem in ASP.NET?如何在 ASP.NET 中禁用特定的 CheckBoxList ListItem?
【发布时间】:2014-05-07 11:24:35
【问题描述】:

在我的网页中,有两个复选框列表。

<asp:CheckBoxList ID="chk1" runat="server" AutoPostBack="True" onselectedindexchanged="chk1_SelectedIndexChanged" >
    <asp:ListItem Value="1">1</asp:ListItem>
    <asp:ListItem Value="2">2</asp:ListItem>
</asp:CheckBoxList>

<asp:CheckBoxList ID="ch2" runat="server" AutoPostBack="True" >
    <asp:ListItem Value="3">3</asp:ListItem>
    <asp:ListItem Value="4">4</asp:ListItem>
</asp:CheckBoxList>

如果我在 chk1 中选中 1,则在 chk2 中应禁用 3,如果我在 chk1 中选中 2,则应在 chk4 中禁用 4。

【问题讨论】:

    标签: asp.net checkboxlist


    【解决方案1】:

    试试这个,

    protected void chk1_SelectedIndexChanged(object sender, EventArgs e)
    {
       if(chk1.selectedIndex==0)
       {
          chk2.Items[0].enabled=false;
       }
        else if(chk1.selectedIndex==1)
       {
          chk2.Items[1].enabled=false;
       }
    }
    

    【讨论】:

      【解决方案2】:

      请尝试以下代码 sn-p。

      protected void chk1_SelectedIndexChanged(object sender, EventArgs e)
      {
          foreach (ListItem item in chk1.Items)
          {
              switch (item.Value)
              {
                  case "1":
                      if (chk2.Items.FindByValue("3") != null && item.Selected == true)
                          chk2.Items.FindByValue("3").Enabled = false;
                      else
                          chk2.Items.FindByValue("3").Enabled = true;
                      break;
                  case "2":
                      if (chk2.Items.FindByValue("4") != null && item.Selected == true)
                          chk2.Items.FindByValue("4").Enabled = false;
                      else
                          chk2.Items.FindByValue("4").Enabled = true;
                      break;
                  default:
                      break;
              }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2014-11-05
        • 1970-01-01
        • 2015-04-12
        • 2023-03-30
        • 1970-01-01
        • 1970-01-01
        • 2021-11-18
        • 1970-01-01
        • 2011-02-06
        相关资源
        最近更新 更多