2013-04-11 32 views
0

我有複選框列出了我需要添加屬性,但我已經注意到,當我重複的控制,有沒有...DataBind不會生成控件?

var listType = SettingsManager.Get("CRMCaseTypes"); 
    var listStatus = SettingsManager.Get("CRMStatusReasons"); 

    var listTypeItems = ParseSettingList(listType); 
    var listStatusItems = ParseSettingList(listStatus); 

    cblCRMType.DataSource = listTypeItems; 
    cblCRMType.DataBind(); 
    cblCRMStatus.DataSource = listStatusItems; 
    cblCRMStatus.DataBind(); 

    int index = 0; 
    foreach (Control c in cblCRMStatus.Controls) 
    { 
     CheckBox cb = c as CheckBox; 
     if (cb != null) 
     { 
      cb.Attributes.Add("id", listStatusItems[index].Value); 

      if (cb.Attributes["id"] == "5") 
      { 
       cb.Checked = true; 
       cb.Enabled = false; 
      } 
     } 

     index++; 
    } 

    index = 0; 
    foreach (Control c in cblCRMType.Controls) 
    { 
     CheckBox cb = c as CheckBox; 
     if (cb != null) 
     { 
      cb.Attributes.Add("id", listTypeItems[index].Value); 
     } 

     index++; 
    } 

} 

有沒有辦法迫使複選框一代?

感謝

回答

1

當你調用DataBind()CheckBoxList,它填充其Items集合,而不是它的Controls集合。

foreach (ListItem item in cblCRMStatus.Items) 
{ 
    if (item.Value == "5") 
    { 
     item.Selected = true; 
     item.Enabled = false; 
    } 
} 

item.Attributes屬性允許您將其他屬性添加到複選框。但是,您無法設置id屬性; ASP.NET自動將其設置爲遞增整數。

+0

是的,我已經注意到了,那麼如何獲得複選框來賦予它們屬性? – jmasterx 2013-04-11 18:19:27

+0

我仍然需要以某種方式給他們一個價值,我不只是設置啓用/禁用。 – jmasterx 2013-04-11 18:20:48

+0

「ListItem.Selected」屬性對應於選中/未選中狀態。 – 2013-04-11 18:29:15