2014-01-16 36 views
1

我有一些工作代碼,但我不認爲這是實現我要做的最好的方法。CheckBoxList中的「Select All」

我有一個CheckBoxList有5個項目 - 4個單獨項目和1個「全選」項目。選擇全選後,我希望爲用戶檢查其他4。當Select All被取消選中時,我希望其他4對於用戶不被選中。

我在StackOverflow和Google上發現的一堆事情只是建議通過CheckBoxList.Items循環,但這不適用於我的情況。例如,說一個用戶不檢查項目#3 - 這會觸發OnSelectedIndexChanged事件,此時我將開始循環通過項目。我會發現「全選」項沒有被選中,所以我會取消選擇所有項目。因此,用戶僅檢查項目#3,以便立即取消它們的選擇。

下面是我的工作代碼,但它使用了一些奇怪的功能,我無法想象這是實現我所尋找的最佳方式。

protected void StatusCheckBoxListChanged(object sender, System.EventArgs e) 
    { 
     //Crazy code I found online to determine which item triggered the event 
     CheckBoxList list = (CheckBoxList)sender; 
     string[] control = Request.Form.Get("__EVENTTARGET").Split('$'); 
     int index = control.Length - 1; 
     ListItem li = (ListItem)list.Items[Int32.Parse(control[index])]; 

     if (li.ToString().Equals("Select All")) //If it was the "Select All" Item which triggered the event 
     { 
      //If it was checked, check off everything. If it was unchecked, uncheck everything. 
      for(int i = 0; i < StatusCheckBoxList.Items.Count; i++) 
      { 
       StatusCheckBoxList.Items[i].Selected = StatusCheckBoxList.Items.FindByValue("Select All").Selected; 
      } 
     } 
    } 
+0

你可以提供'CheckBoxList'的標記嗎? – Grundy

+0

當然,雖然它是相當標準的。 ''' – Jake

+1

似乎更好的使用另一個CheckBox代替CheckBoxList中的項目 – Grundy

回答

1

你可以嘗試添加字段,您頁,這是幻燈片前爲價值選擇全部這樣

bool SelectAll; 

初始化它PageLoad事件處理程序,像這樣的東西

protected void Page_Load(object sender, EventArgs e) 
{ 
    if(!IsPostback){ 
     SelectAll = StatusCheckBoxList.Items.Cast<ListItem>().FirstOrDefault(d => d.Text == "Select All" && d.Selected) != null; 

    } 
} 

,改變你的活動這樣

protected void StatusCheckBoxListChanged(object sender, System.EventArgs e) 
{ 
    if(SelectAll != (StatusCheckBoxList.Items.Cast<ListItem>().FirstOrDefault(d => d.Text == "Select All" && d.Selected) != null)){ 
     SelectAll = !SelectAll; 
     foreach(var li in StatusCheckBoxList.Items){ 
      li.Selected = SelectAll; 
     } 
    } 
} 

注:如果你知道項目的位置「全選」值可以不用LINQ這樣的事情

樣品的情況下做的時候「全選「是第一項

protected void Page_Load(object sender, EventArgs e) 
{ 
    if(!IsPostback){ 
     SelectAll = StatusCheckBoxList.Items[0].Selected; 
     foreach(var li in StatusCheckBoxList.Items){ 
      li.Selected = SelectAll; 
     } 
    } 
} 

,改變你的活動這樣

protected void StatusCheckBoxListChanged(object sender, System.EventArgs e) 
{ 
    if(SelectAll != StatusCheckBoxList.Items[0].Selected){ 
     SelectAll = !SelectAll; 
     foreach(var li in StatusCheckBoxList.Items){ 
      li.Selected = SelectAll; 
     } 
    } 
} 
+0

這看起來似乎是最好的解決方案,並且可以在沒有我提供的「瘋狂代碼」的情況下完成,儘管這些代碼儘管工作起來,但是有點破壞性。我更喜歡你的方法!謝謝! – Jake

0

我已經在過去通過設置一個標誌來解決這個問題,例如,

public class MyClass 
{ 
    bool _IsReady = true; 

    protected void StatusCheckBoxListChanged(object sender, System.EventArgs e) 
    { 
     if (!_IsReady) 
     { 
      return; 
     } 

     //Crazy code I found online to determine which item triggered the event 
     CheckBoxList list = (CheckBoxList)sender; 
     string[] control = Request.Form.Get("__EVENTTARGET").Split('$'); 
     int index = control.Length - 1; 
     ListItem li = (ListItem)list.Items[Int32.Parse(control[index])]; 

     if (li.ToString().Equals("Select All")) //If it was the "Select All" Item which triggered the event 
     { 
      SetAllStatusItemsSelected(StatusCheckBoxList.Items.FindByValue("Select All").Selected); 
     } 
    } 


    private void SetAllStatusItemsSelected(bool IsSelected) 
    { 
     _IsReady = false; 

     //If it was checked, check off everything. If it was unchecked, uncheck everything. 
     for(int i = 0; i < StatusCheckBoxList.Items.Count; i++) 
     { 
      StatusCheckBoxList.Items[i].Selected = StatusCheckBoxList.Items.FindByValue("Select All").Selected; 
     } 

     _IsReady = true; 
    } 

} 

我很想知道是否有更優雅的方式。

+0

我的代碼有點困惑 - 它遍歷每個Item並將它們的Selected值設置爲與「Select All」項目相同的值(不管「Select All」項是否是最近檢查的項)。是對的嗎? – Jake

+0

對不起傑克 - 我錯過了什麼。給我一分鐘... – MarkHone

+1

好像'_IsReady'總是'當你調用TRUE''StatusCheckBoxListChanged' – Grundy

-1

的路上我一直都解決了這個問題

 protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     CheckBoxList lstBx = (CheckBoxList)sender; 

     List<ListItem> list = lstBx.Items.Cast<ListItem>().ToList(); 
     string[] control = Request.Form.Get("__EVENTTARGET").Split('$'); 
     int index = Convert.ToInt32(control[control.Length - 1]); 

     ListItem selectAllControl = list.FirstOrDefault(x => x.Text == "Select All"); 
     ListItem selectAll = list.FirstOrDefault(x => x.Selected && x.Text == "Select All"); 

     if (index == 0) 
     {    
      if (selectAll != null) 
      { 
       foreach (var item in list) 
       { 
        item.Selected = true; 
       } 
      } 
     } 
     else 
     { 
      if (selectAllControl != null) 
      { 
       selectAllControl.Selected = false; 
      } 
     } 

    } 
+0

你的代碼看起來相當類似地雷,這是不幸的 – Jake

+0

@Jake,你在說什麼不幸?此代碼有效。 –

+1

@勞倫斯蘇爾曼,看起來你和我一樣,錯過了第一段和第四段的開頭:「我有一些工作代碼......」和「下面是我的工作代碼......」。它可以工作,但看起來並不高雅。 – MarkHone