2013-03-03 48 views
0

我生成了一個CheckBoxList,它有多個使用C#的項目。現在我想設置CheckBoxList中允許的最大檢查項目數。如果用戶檢查超過最大允許的項目,將會有警報或其他項目將自動取消選中,以防止用戶檢查允許的最大項目數。CheckBoxList中允許的最大值checked

檢查項目的最大數量將被設置爲ChecokBoxList在後臺代碼(C#)或用javascript做到這一點,但也應該在C#中也可以產生的JavaScript。

我需要一些幫助來解決這個問題。

例如代碼:

CheckBoxList chkl = new CheckBoxList(); 
string[] items = {"item1", "item2", "item3", "item4", "item5"}; 
foreach (string item in items) 
      { 
       chkl.Items.Add(new ListItem(item)); 
      } 
chkl.MaximumCheck = 3; 

在後臺代碼生成後,將的CheckBoxList將只允許用戶檢查僅三個項目。如果用戶檢查三個以上的項目,其他項目將自動取消選中或至少顯示一個警報,以防止用戶檢查三個以上的項目。

+0

如果試圖檢查第四項,會有警報顯示,以用戶,他們不能做更多的檢查,或第四檢查不能做,因爲它是一個可禁用。 – 2013-03-03 03:14:33

+0

我將嘗試在C#中使用回發​​功能編寫代碼,以便每當用戶檢查checkboxlist上的任何項時執行此操作。稍後我會告訴你在我的問題上的進一步幫助。 – 2013-03-03 03:20:57

回答

1
int x = 0; 
foreach (var li in ListBox1.Items) { 
if (li.Selected == true) 
{ 
    x = x + 1; 
} 

或類似:

ListBox1.GetSelectedIndices().Length 

在Javascript中:

<script type="text/javascript" language="javascript"> 
function CheckCheck() 
{ 
    var chkBoxList=document.getElementById('<%=CheckBoxList1.ClientID %>');  var chkBoxCount=chkBoxList.getElementsByTagName("input"); 

    var btn=document.getElementById('<%=btnSubmit.ClientID %>'); 
    var i=0; 
    var tot=0; 
    for(i=0;i<chkBoxCount.length;i++) 
    { 
     if(chkBoxCount[i].checked) 
     { 
     tot=tot+1; 
     } 
    } 

if(tot > 3) 
{ 
    alert('Cannot check more than 3 check boxes');    
}  
</script> 
<form id="form1" runat="server"> 
<div> 
<table> 
<tr> 
<td> 
<asp:CheckBoxList ID="CheckBoxList1" runat="server" onclick="javascript:CheckCheck();"> 
<asp:ListItem>One</asp:ListItem> 
<asp:ListItem>Two</asp:ListItem> 
<asp:ListItem>Three</asp:ListItem> 
<asp:ListItem>Four</asp:ListItem> 
<asp:ListItem>Five</asp:ListItem> 
</asp:CheckBoxList> 
</td> 
<td> 
<asp:Button ID="btnSubmit" runat="server" Text="Submit" /> 
</td> 
</tr> 
</table> 

+0

這是我想要的,因爲沒有必要檢查後回事件。但是有一些東西錯過了我的目標,動態控件有很多不同的ID,以及javascript如何識別哪個checkbolist會調用它?以及它如何知道任何單個複選框列表的最大檢查屬性? – 2013-03-03 04:12:28

+0

我自己的答案已經解決了這個問題,但它需要一個回發事件。這就是我要避免的,因爲我將動態控制添加到updatepanel。因此,任何回傳可能會破壞aspx頁面的當前視圖狀態。 – 2013-03-03 04:16:25

0

我對這個問題的一個很好的解決方案:

在C#中,我將生成使用此代碼5項的CheckBoxList:

CheckBoxList chkl = new CheckBoxList(); 
    string[] items = { "item1", "item2", "item3", "item4", "item5" }; 
    foreach (string item in items) 
    { 
     chkl.Items.Add(new ListItem(item)); 
    } 
    chkl.AutoPostBack = true; 
    chkl.CssClass = "3"; 
    chkl.SelectedIndexChanged += new EventHandler(BoxChecked); 

正如你所看到的,CheckBoxList的有5項,最大檢測項目通過的CheckBoxList的的CssClass屬性seted,假設將有在CheckBoxList中不需要CssClass。所以我會通過這個屬性來設置最大的檢查項目,以使其更加清晰。這裏的關鍵是要增加對CheckBoxList的一個事件處理程序,因此,如果用戶要檢查比最大的項目較多,其他項目將被禁用。

的EventHander將被寫入如下:

protected void BoxChecked(object sender, EventArgs e) 
{ 
    try 
    { 
     int maximumCheck = -1; 
     CheckBoxList CheckExpertiseList = (CheckBoxList)sender; 
     try { 
      maximumCheck = Convert.ToInt32(CheckExpertiseList.CssClass); 
     } 
     catch { } 
     if (maximumCheck > -1) 
     { 
      if (CheckExpertiseList.Items.Cast<ListItem>().Where(i => (i.Selected == true)).Count() == maximumCheck) 
      { 
       CheckExpertiseList.Items.Cast<ListItem>().Where(i => (i.Selected == false)).ToList().ConvertAll(i => i.Enabled = false).ToList(); 
      } 
      else if (CheckExpertiseList.Items.Cast<ListItem>().Where(i => (i.Selected == true)).Count() == maximumCheck - 1) 
       CheckExpertiseList.Items.Cast<ListItem>().Where(i => (i.Selected == false)).ToList().ConvertAll(i => i.Enabled = true).ToList(); 
     } 
    } 
    catch { } 
} 

事件處理事件將檢查的CheckBoxList擁有超過限制的項目檢查的話將禁用其他物品,否則將重新啓用其他項目。

0

首先,我感謝你傑里米·湯普森。他爲我的問題給了我一個好主意。有點改變,我有我想要的。只使用JavaScript解決我的問題。

<title>Untitled Page</title> 
</head> 
<body> 
<form id="form1" runat="server"> 
<div> 
<script type="text/javascript" language="javascript"> 
    function CheckCheck() { 
     var chkBoxCount = this.getElementsByTagName("input"); 
     var max = parseInt(this.className); 
     var i = 0; 
     var tot = 0; 
     for (i = 0; i < chkBoxCount.length; i++) { 
      if (chkBoxCount[i].checked) { 
       tot = tot + 1; 
      } 
     } 

     if (tot > max) { 
      var k = 0; 
      for (i = 0; i < chkBoxCount.length; i++) { 
       if (chkBoxCount[i].checked) { 
        k++; 
        if (k > max) { 
         chkBoxCount[i].checked = false; 
         alert('Cannot check more than ' + max + ' check boxes'); 
        } 
       } 
      }    
     } 
    } 
</script> 
<div> 
<table> 
<tr> 
<td> 
<asp:CheckBoxList ID="CheckBoxList1" runat="server" CssClass="3" onclick="javascript:CheckCheck.call(this);"> 
<asp:ListItem>One</asp:ListItem> 
<asp:ListItem>Two</asp:ListItem> 
<asp:ListItem>Three</asp:ListItem> 
<asp:ListItem>Four</asp:ListItem> 
<asp:ListItem>Five</asp:ListItem> 
</asp:CheckBoxList> 
</td> 
<td> 
<asp:Button ID="btnSubmit" runat="server" Text="Submit" /> 
</td> 
</tr> 
</table> 
</div> 
</form> 
</body> 
</html> 

的JavaScript將自動識別哪些控制會調用它,它也可以通過控制屬性的CssClass得到最大的檢查項目。通過這樣做,我也阻止用戶檢查比CheckBoxList的最大項目多而不做一些後回。

0
private void cb_magia_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     int maxNumber = 4; 
     int iSelectedIndex = cb_magia.SelectedIndex; 
     if (cb_magia.CheckedItems.Count > maxNumber) 
     { 
      cb_magia.SetItemCheckState(iSelectedIndex, CheckState.Unchecked); 
      MessageBox.Show("you had checked the maximum checkbox value allowed"); 

     } 
    } 
相關問題