Allow user to select no of CheckBoxes based on ListBox selected value using C# and VB.Net in ASP.Net

Last Reply one month ago By dharmendr

Posted one month ago

Hi,

i had a tableLayout With 12 month CheckBoxes 

What i want to do is based on some condition 1 only want to allow the user to check the 6 checkboxes only.

and on button click i want that checked checkbox  text into textbox. how could i acheve this 

 

Posted one month ago
Hi @droiddeve,
Please try the following

Allow only one CheckBox to be checked form CheckBoxList in ASP.Net

It might help you.

Cheers Andrea.

Posted one month ago

hi Andrea,

the senerio is like if a one select a text from ListBox is Quaterly 

then it should only allow to select 3 checkbox

 


Posted one month ago

Hi droiddeve,

Refer the below sample.

HTML

<div>
    <asp:ListBox runat="server" ID="lbPayOption" OnSelectedIndexChanged="PayOptionChanged"
        AutoPostBack="true">
        <asp:ListItem Text="Monthly" Value="0" />
        <asp:ListItem Text="Quarterly" Value="1" />
        <asp:ListItem Text="Halfyearly" Value="2" />
        <asp:ListItem Text="Yearly" Value="3" />
    </asp:ListBox>
    <br />
    <br />
    <asp:CheckBoxList runat="server" ID="cblItems" OnSelectedIndexChanged="ItemsSelected"
        RepeatDirection="Horizontal" RepeatColumns="4" AutoPostBack="true">
        <asp:ListItem Text="Test 1" Value="1" />
        <asp:ListItem Text="Test 2" Value="2" />
        <asp:ListItem Text="Test 3" Value="3" />
        <asp:ListItem Text="Test 4" Value="4" />
        <asp:ListItem Text="Test 5" Value="5" />
        <asp:ListItem Text="Test 6" Value="6" />
        <asp:ListItem Text="Test 7" Value="7" />
        <asp:ListItem Text="Test 8" Value="8" />
        <asp:ListItem Text="Test 9" Value="9" />
        <asp:ListItem Text="Test 10" Value="10" />
        <asp:ListItem Text="Test 11" Value="11" />
        <asp:ListItem Text="Test 12" Value="12" />
    </asp:CheckBoxList>
</div>

Code

C#

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 "MONTHLY":
                if (CheckedCount(cblItems) >= 1)
                {
                    DisableOtherCheckBoxs(cblItems);
                }
                else
                {
                    EnableOtherCheckBoxs(cblItems);
                }
                break;
            case "QUARTERLY":
                if (CheckedCount(cblItems) >= 3)
                {
                    DisableOtherCheckBoxs(cblItems);
                }
                else
                {
                    EnableOtherCheckBoxs(cblItems);
                }
                break;
            case "HALFYEARLY":
                if (CheckedCount(cblItems) >= 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;
    }
}

VB.Net

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 "MONTHLY"
				If CheckedCount(cblItems) >= 1 Then
					DisableOtherCheckBoxs(cblItems)
				Else
					EnableOtherCheckBoxs(cblItems)
				End If
				Exit Select
			Case "QUARTERLY"
				If CheckedCount(cblItems) >= 3 Then
					DisableOtherCheckBoxs(cblItems)
				Else
					EnableOtherCheckBoxs(cblItems)
				End If
				Exit Select
			Case "HALFYEARLY"
				If CheckedCount(cblItems) >= 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

Screenshot

I agree, here is the link: http://e-iceblue.com/free-api