ASPForums.Net RSS Feedhttp://www.aspforums.net/Handlers/RSS.ashxLatest additions to the content that appears on ASPForums.Net(c) 2017 www.aspforums.com. All rights reserved.Allow user to select no of CheckBoxes based on ListBox selected value using C# and VB.Net in ASP.Net Hi @droiddeve,<br />Please try the following<br /><br /><a href = 'link(Threads/101635/Allow-only-one-CheckBox-to-be-checked-form-CheckBoxList-in-ASPNet/)'>Allow only one CheckBox to be checked form CheckBoxList in ASP.Net</a><br /><br />It might help you.<br /><br />Cheers Andrea.http://www.aspforums.net/Threads/880114/Allow-user-to-select-no-of-CheckBoxes-based-on-ListBox-selected-value-using-C-and-VBNet-in-ASPNet/http://www.aspforums.net/Threads/880114/Allow-user-to-select-no-of-CheckBoxes-based-on-ListBox-selected-value-using-C-and-VBNet-in-ASPNet/Mon, 09 Jan 2017 01:10:29 GMTAllow user to select no of CheckBoxes based on ListBox selected value using C# and VB.Net in ASP.Net <p>hi Andrea,</p> <p>the senerio is like if a one select a text from ListBox is Quaterly&nbsp;</p> <p>then it should only allow to select 3 checkbox</p> <p>&nbsp;</p>http://www.aspforums.net/Threads/880114/Allow-user-to-select-no-of-CheckBoxes-based-on-ListBox-selected-value-using-C-and-VBNet-in-ASPNet/http://www.aspforums.net/Threads/880114/Allow-user-to-select-no-of-CheckBoxes-based-on-ListBox-selected-value-using-C-and-VBNet-in-ASPNet/Mon, 09 Jan 2017 01:35:03 GMTAllow user to select no of CheckBoxes based on ListBox selected value using C# and VB.Net in ASP.Net <p>Hi <a class="username" rel="droiddeve"> droiddeve</a>,</p> <p>Refer the below sample.</p> <p><span style="text-decoration: underline;">HTML</span></p> <pre class="brush: html">&lt;div&gt; &lt;asp:ListBox runat=&#34;server&#34; ID=&#34;lbPayOption&#34; OnSelectedIndexChanged=&#34;PayOptionChanged&#34; AutoPostBack=&#34;true&#34;&gt; &lt;asp:ListItem Text=&#34;Monthly&#34; Value=&#34;0&#34; /&gt; &lt;asp:ListItem Text=&#34;Quarterly&#34; Value=&#34;1&#34; /&gt; &lt;asp:ListItem Text=&#34;Halfyearly&#34; Value=&#34;2&#34; /&gt; &lt;asp:ListItem Text=&#34;Yearly&#34; Value=&#34;3&#34; /&gt; &lt;/asp:ListBox&gt; &lt;br /&gt; &lt;br /&gt; &lt;asp:CheckBoxList runat=&#34;server&#34; ID=&#34;cblItems&#34; OnSelectedIndexChanged=&#34;ItemsSelected&#34; RepeatDirection=&#34;Horizontal&#34; RepeatColumns=&#34;4&#34; AutoPostBack=&#34;true&#34;&gt; &lt;asp:ListItem Text=&#34;Test 1&#34; Value=&#34;1&#34; /&gt; &lt;asp:ListItem Text=&#34;Test 2&#34; Value=&#34;2&#34; /&gt; &lt;asp:ListItem Text=&#34;Test 3&#34; Value=&#34;3&#34; /&gt; &lt;asp:ListItem Text=&#34;Test 4&#34; Value=&#34;4&#34; /&gt; &lt;asp:ListItem Text=&#34;Test 5&#34; Value=&#34;5&#34; /&gt; &lt;asp:ListItem Text=&#34;Test 6&#34; Value=&#34;6&#34; /&gt; &lt;asp:ListItem Text=&#34;Test 7&#34; Value=&#34;7&#34; /&gt; &lt;asp:ListItem Text=&#34;Test 8&#34; Value=&#34;8&#34; /&gt; &lt;asp:ListItem Text=&#34;Test 9&#34; Value=&#34;9&#34; /&gt; &lt;asp:ListItem Text=&#34;Test 10&#34; Value=&#34;10&#34; /&gt; &lt;asp:ListItem Text=&#34;Test 11&#34; Value=&#34;11&#34; /&gt; &lt;asp:ListItem Text=&#34;Test 12&#34; Value=&#34;12&#34; /&gt; &lt;/asp:CheckBoxList&gt; &lt;/div&gt;</pre> <p><span style="text-decoration: underline;">Code</span></p> <p><span style="text-decoration: underline;">C#</span></p> <pre class="brush: csharp">public int CheckedCount(CheckBoxList checkBox) { int count = 0; foreach (ListItem item in checkBox.Items) { if (item.Selected) { count++; } } return count; } public void DisableOtherCheckBoxs(CheckBoxList checkBox) { foreach (ListItem item in checkBox.Items) { if (!item.Selected) { item.Enabled = false; } } } public void EnableOtherCheckBoxs(CheckBoxList checkBox) { foreach (ListItem item in checkBox.Items) { item.Enabled = true; } } protected void ItemsSelected(object sender, EventArgs e) { if (lbPayOption.SelectedItem != null) { switch (lbPayOption.SelectedItem.Text.Trim().ToUpper()) { case &#34;MONTHLY&#34;: if (CheckedCount(cblItems) &gt;= 1) { DisableOtherCheckBoxs(cblItems); } else { EnableOtherCheckBoxs(cblItems); } break; case &#34;QUARTERLY&#34;: if (CheckedCount(cblItems) &gt;= 3) { DisableOtherCheckBoxs(cblItems); } else { EnableOtherCheckBoxs(cblItems); } break; case &#34;HALFYEARLY&#34;: if (CheckedCount(cblItems) &gt;= 6) { DisableOtherCheckBoxs(cblItems); } else { EnableOtherCheckBoxs(cblItems); } break; default: break; } } } protected void PayOptionChanged(object sender, EventArgs e) { foreach (ListItem item in cblItems.Items) { item.Enabled = true; item.Selected = false; } }</pre> <p><span style="text-decoration: underline;">VB.Net</span></p> <pre class="brush: vb">Public Function CheckedCount(checkBox As CheckBoxList) As Integer Dim count As Integer = 0 For Each item As ListItem In checkBox.Items If item.Selected Then count += 1 End If Next Return count End Function Public Sub DisableOtherCheckBoxs(checkBox As CheckBoxList) For Each item As ListItem In checkBox.Items If Not item.Selected Then item.Enabled = False End If Next End Sub Public Sub EnableOtherCheckBoxs(checkBox As CheckBoxList) For Each item As ListItem In checkBox.Items item.Enabled = True Next End Sub Protected Sub ItemsSelected(sender As Object, e As EventArgs) If lbPayOption.SelectedItem IsNot Nothing Then Select Case lbPayOption.SelectedItem.Text.Trim().ToUpper() Case &#34;MONTHLY&#34; If CheckedCount(cblItems) &gt;= 1 Then DisableOtherCheckBoxs(cblItems) Else EnableOtherCheckBoxs(cblItems) End If Exit Select Case &#34;QUARTERLY&#34; If CheckedCount(cblItems) &gt;= 3 Then DisableOtherCheckBoxs(cblItems) Else EnableOtherCheckBoxs(cblItems) End If Exit Select Case &#34;HALFYEARLY&#34; If CheckedCount(cblItems) &gt;= 6 Then DisableOtherCheckBoxs(cblItems) Else EnableOtherCheckBoxs(cblItems) End If Exit Select Case Else Exit Select End Select End If End Sub Protected Sub PayOptionChanged(sender As Object, e As EventArgs) For Each item As ListItem In cblItems.Items item.Enabled = True item.Selected = False Next End Sub</pre> <p><span style="text-decoration: underline;">Screenshot</span></p> <p><span style="text-decoration: underline;"><img src="http://i988.photobucket.com/albums/af3/mudassarkhan/880114.gif" alt="" width="327" height="219" /></span></p>http://www.aspforums.net/Threads/880114/Allow-user-to-select-no-of-CheckBoxes-based-on-ListBox-selected-value-using-C-and-VBNet-in-ASPNet/http://www.aspforums.net/Threads/880114/Allow-user-to-select-no-of-CheckBoxes-based-on-ListBox-selected-value-using-C-and-VBNet-in-ASPNet/Mon, 09 Jan 2017 09:38:24 GMT