Click here to Skip to main content
Click here to Skip to main content

How to validate atleast one checkbox is selected in a grid

, 4 Jun 2009 CPOL
Rate this:
Please Sign up or sign in to vote.
How to validate atleast one checkbox is selected in a grid, from the client side.

Introduction

Here is the JavaScript code that ensures at least one checkbox is checked among the checkboxes inside a GridView control before submitting the form. A generalized function is provided which can be used for any grid. The base idea came from a fellow poster, Samir Nigam: http://www.codeproject.com/KB/webforms/GridViewcheckBoxes.aspx.

Using the code

In the DataGrid, I use a checkbox inside a template column, and attach an OnClientClick event to the button by passing the grid name and the checkbox name.

<asp:DataGrid ID="dgTry" runat="server">
    <Columns>
        <asp:BoundColumn DataField="name"></asp:BoundColumn>
        <asp:BoundColumn DataField="Age"></asp:BoundColumn>
        <asp:TemplateColumn>
         <ItemTemplate>
          <asp:CheckBox ID="chkSelect" runat="server"></asp:CheckBox>
         </ItemTemplate>
        </asp:TemplateColumn>
    </Columns>
</asp:DataGrid>

In the JavaScript section, I then add this piece of code:

<script language="javascript">
   function TestCheckBox(gridId,checkboxName)
   {    
      var TargetChildControl = checkboxName;
      var Inputs ;
            Inputs = gridId.getElementsByTagName("input");
      for(var n = 0; n < Inputs.length; ++n)
         if(Inputs[n].type == 'checkbox' && 
            Inputs[n].id.indexOf(TargetChildControl,0) >= 0 && 
            Inputs[n].checked)
          return true;        
            
      alert('Select at least one checkbox!');
      return false;
   }
</script>

Now, add a button and add the OnClientClick event:

<asp:Button ID="btnSubmit" runat="server" 
  OnClick="Submit" OnClientClick="return TestCheckBox(dgTry,'chkSelect');" />

In the above code, we are passing the grid name and the checkbox name to the DataGrid. The function will find out all the input elements within the grid, and look if a checkbox with the name passed is checked. On the first instance it finds such a case, it returns true so a postback occurs, otherwise no.

Points of interest

While coding this, it was fun to find how the client ID could be passed to a function. Later, I figured out the ID of the control was sufficient.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Member 1072719
Web Developer Clover
India India
No Biography provided

Comments and Discussions

 
GeneralMy vote of 1 PinmemberC.V.Vikram16-Sep-09 0:17 
GeneralThere could be a problem PinmemberIrwan Hassan18-Jun-09 7:59 
Hi, your approch is OK, but what happen if there are more then one check box in a row and the other check box is selected then the actual one.
 
For example let say you have a list of Members. You can delete one or many members by selecting them by checking each checkbox in the first column. At the same time, there is another check box at the last column with (let say) MemberIsActive name.
 
If the user haven't selected any checkbox in the first column, but have selected one of the checkbox for MemberIsActive, the alert won't appear. Correct?
 
But anyway, nice approch.
GeneralRe: There could be a problem PinmemberMember 107271920-Jun-09 5:58 
GeneralError message PinmemberMember 25604729-Jun-09 0:02 
GeneralSamir?! PinmemberWartickler5-Jun-09 3:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.1411023.1 | Last Updated 4 Jun 2009
Article Copyright 2009 by Member 1072719
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid