Validate ASP.NET CheckBoxList which is inside a DataList Control

Recently someone was facing a problem on validating a CheckBoxList using CustomValidator control. The CheckBoxList was inside of a DataList control.

As for e.g. something like below:

<asp:DataList ID="DataList1" runat="server">
    <ItemTemplate>
        <asp:CheckBoxList ID="CheckBoxList1" runat="server">
            <asp:ListItem Text="One" Value="1"></asp:ListItem>
            <asp:ListItem Text="Two" Value="2"></asp:ListItem>
        </asp:CheckBoxList>
        <asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="Please check at least one item."
 ClientValidationFunction="ValidateCBL"></asp:CustomValidator>
    </ItemTemplate>
</asp:DataList>

Here’s the code that does the job.

<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
 <script type = "text/javascript">
        function ValidateCBL(source, args) {
            var cblist= $(source).parent().find("table[id$='_CheckBoxList1']");
            if (cblist.length==0 || cblist.find('input:checkbox:checked').length > 0) {
                args.IsValid = true;
            }
            else {
                args.IsValid = false;
            }
      }
</script> 

It is using jQuery to do get the control and finding if anything is checked.

$(source).parent() will get parent DOM element of the CustomValidator control that fired the ValidateCBL function. And then it finds any table that has an id ending with _CheckBoxList1. It’s because by default CBL is rendered as and table. When inside of a databound control, the asp.net will, by default, change the id to something like Ctl00_something_CheckBoxList1.

Then it checks if there is at lease one checkbox checked, if so then set args.IsValid=true for that CustomValidator else it will set it to false.

The above code isn’t specific to CBL inside of a DataList only. It would work for most of the DataBoundControls like GridView, Repeater, ListView, etc.

Hope this helps.

4 Comments

  • Jeff,

    CheckBoxList will render into a table by default.

    What the code line: var cblist= $(source).parent().find("table[id$='_CheckBoxList1']"); is doing:

    - Get the source = the custom validator control $(source)

    - Then get its .parent() i.e. the td in which the validator is.

    - then find the CB that has id ending in _CheckBoxList1

    Check the rendered markup of your Checkbox list and find what is the id of the CBL.

    If you are using ASP.NET 4.0+, make sure have set ClientIDMode for the CBL:



    Hope this helps.

  • Thanks for the response. I included the ClientIDmode but it still didnt work

    Please find below the markup for the checkboxlist:
    Cif EasyLift Actifizz
    Cif Cream
    Domestos Toilet Wipes


    the Javascript code below:

    function ValidateCBL(source, args) {
    var cblist= $(source).parent().find("table[id$='_CheckBoxList1']");
    if (cblist.length==0 || cblist.find('input:checkbox:checked').length > 0) {
    args.IsValid = true;
    }
    else {
    args.IsValid = false;
    }
    }

  • Hi Guru,

    I sent you an email. Pls could you reply

    Jeff

  • Jeff,
    I see it is because you have set RepeatLayout="Flow" for your CBL. By default for my example it is RepeatLayout="Table"

    So in your code, look for span instead of table like below.
    var cblist = $(source).parent().find("span[id$='_CheckBoxList1']");

    That should fix your issue.

Comments have been disabled for this content.