1. 程式人生 > >C# CheckBoxList 實現全選/反選功能怎麼寫?

C# CheckBoxList 實現全選/反選功能怎麼寫?

首先我們用RadioButtonList控制元件,且必須包含OnSelectedIndexChanged事件和AutoPostBack=‘true’屬性,

<f:RadioItem Text="全選" Value="0" />
<f:RadioItem Text="取消全選" Value="1" />

 

然後在OnSelectedIndexChanged事件裡寫如下程式碼:

     if (checkboxlistName.Items[0].Selected == true)
            {
                for
(int i = 0; i < checkboxlistName.Items.Count; i++) { checkboxlistName.Items[i].Selected = true; } } else { for (int i = 0; i < checkboxlistName.Items.Count; i++) { checkboxlistName.Items[i].Selected
= false; } }

 ok,完成!