0

I have a checkboxList having some items.I want to specify a maximum checkbox to allow checking How can we do this using c# code.

here is my code

<asp:CheckBoxList ID="chkLstFields" runat="server" AutoPostBack="true">

                                            </asp:CheckBoxList>


int br_id = Convert.ToInt32(ddlBrand.SelectedValue);
        int status = Convert.ToInt32(ddlStatus.SelectedIndex.ToString());
        DataTable dt_Spec = bl.sp_select_model_spec_field(br_id, status);
        chkLstFields.Items.Clear();

        if (dt_Spec.Rows.Count > 0)
        {
            for (int i = 0; dt_Spec.Rows.Count > i; i++)
            {
                chkLstFields.Items.Add(dt_Spec.Rows[i].ItemArray[0].ToString());

            }
        }
2
  • The question is unclear. Where in your code are you trying to specify a "maximum checkbox"(whatever that is)? Commented Jan 9, 2015 at 9:29
  • Do you mean you have N checkboxes and you want to only allow the user to check N-M checkboxes, where you want to specify M in code? I'd check this serverside and clientside, for the latter you'll need JavaScript. Commented Jan 9, 2015 at 9:30

2 Answers 2

2

You could use a CustomValidator for this purpose. I've realized it in an old VB.NET project.

Here is the relevant aspx:

<asp:ListBox ID="LbSymptomCodesInsert" runat="server" 
    CausesValidation="true" 
    ValidationGroup="VG_RMA_SAVE" SelectionMode="Multiple"> 
</asp:ListBox>
<asp:CustomValidator ID="CV_SymptomCodeSelectionCount" runat="server" 
    ValidateEmptyText="true" 
    ClientValidationFunction="validateSymptomCodeSelectionCount" 
    OnServerValidate="validateSymptomCodeSelectionCount" 
    ControlToValidate="LbSymptomCodesInsert" 
    Display="None" 
    EnableClientScript="true" 
    ErrorMessage="Select at least one and at most 5 SymptomCodes" 
    Style="visibility: hidden" 
    ValidationGroup="VG_RMA_SAVE">*</asp:CustomValidator>

Here are the javascript functions:

function validateSymptomCodeSelectionCount(sender, args){
    var listbox = document.getElementById('LbSymptomCodesInsert');
    args.IsValid = validateListBoxSelectionCount(listbox, 1, 5);
}

function validateListBoxSelectionCount(listbox, minSelected, maxSelected){
    var selected=0;
    if(listbox != null){
        for (var i=0; i<listbox.length; i++){
            if(listbox.options[i].selected){
               selected++; 
               if(selected>maxSelected)break;
            }
        }
    }
   return (selected >= minSelected && selected <= maxSelected);
} 

Here's the ServerValidate (VB.NET but i'm sure you get the point):

Protected Sub validateSymptomCodeSelectionCount(ByVal source As Object, ByVal args As ServerValidateEventArgs)
    Dim count = 0
    For Each item As ListItem In LbSymptomCodesInsert.Items
        If item.Selected Then count += 1
        If count > 5 Then Exit For
    Next
    args.IsValid = (count >= 1 AndAlso count <= 5)
End Sub
Sign up to request clarification or add additional context in comments.

Comments

1

If you want to limit the number of items checked,you can limit the checked items amount this way:

if(chkLstFields.Items.OfType<ListItem>().Count(x => x.Selected) > 2)
{

  // show message you cannot select more than 2 items
}
else
{
 // continue execution
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.