这段代码是实现:当一个checkbox选中后,禁用ChekBoxlist的 某项的。
问题是,如果在选择某项后, 又取消掉该项的情况下,被禁用的项 仍然是禁用状态,怎么能返还成原Enabled状态?
大家试试看,谢谢
//作者: chen_ya_ping
protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            foreach (ListItem item in this.CheckBoxList1.Items)
            {
                if (item.Selected == true)
                {
                    if (Convert.ToInt32(item.Value) == 1)
                    {
                        this.CheckBoxList1.Items[2].Enabled = false;
                    }
                }
            }
        }<asp:CheckBoxList ID="CheckBoxList1" runat="server" AutoPostBack="True" 
            onselectedindexchanged="CheckBoxList1_SelectedIndexChanged" 
            RepeatDirection="Horizontal">
             <asp:ListItem Text="1" Value="1"></asp:ListItem>
             <asp:ListItem Text="2" Value="2"></asp:ListItem>
             <asp:ListItem Text="3" Value="3"></asp:ListItem>
        </asp:CheckBoxList>

解决方案 »

  1.   


     protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
            {
                foreach (ListItem item in this.CheckBoxList1.Items)
                {
                    if (item.Selected == true)
                    {
                        if (Convert.ToInt32(item.Value) == 1)
                        {
                            this.CheckBoxList1.Items[2].Enabled = false;
                        }
                    }
                    else
                    {
                        if (Convert.ToInt32(item.Value) == 1)
                        {
                            this.CheckBoxList1.Items[2].Enabled = true;
                        }
                    }
                }
            }
      

  2.   


    为什么我这样写,会出现问题: 当选 “1”的时候 "3" 无法禁用
    protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            foreach (ListItem item in this.CheckBoxList1.Items)
                {
                    if (item.Selected == true)
                    {
                        if (Convert.ToInt32(item.Value) == 1)                        this.CheckBoxList1.Items[2].Enabled = false;
      
                        else if (Convert.ToInt32(item.Value) == 2)                        this.CheckBoxList1.Items[2].Enabled = false;                }
                    else
                    {
                        if (Convert.ToInt32(item.Value) == 1)                        this.CheckBoxList1.Items[2].Enabled = true;                    else if (Convert.ToInt32(item.Value) == 2)                        this.CheckBoxList1.Items[2].Enabled = true;
                    }
                }
            }