Click here to Skip to main content
11,412,833 members (72,439 online)
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.


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:

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">
        <asp:BoundColumn DataField="name"></asp:BoundColumn>
        <asp:BoundColumn DataField="Age"></asp:BoundColumn>
          <asp:CheckBox ID="chkSelect" runat="server"></asp:CheckBox>

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 && 
          return true;        
      alert('Select at least one checkbox!');
      return false;

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.


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


About the Author

Member 1072719
Web Developer Clover
India India
No Biography provided

Comments and Discussions

GeneralMy vote of 1 Pin
C.V.Vikram at 16-Sep-09 0:17
memberC.V.Vikram16-Sep-09 0:17 
GeneralThere could be a problem Pin
Irwan Hassan at 18-Jun-09 7:59
memberIrwan Hassan18-Jun-09 7:59 
GeneralRe: There could be a problem Pin
Member 1072719 at 20-Jun-09 5:58
memberMember 107271920-Jun-09 5:58 
Thanks for your input and review of the code , in the function call name of checkbox is also passed. Within the function it is checked whether the checkbox is same on which the function should work.

The approach will work on n number of checkbox too.

One enhancement that can be done is to find only the checkboxes of interest through javascript. So overhead of looking in entire list is saved.

Hope it Helps
Jatinder Singh

GeneralError message Pin
Member 2560472 at 9-Jun-09 0:02
memberMember 25604729-Jun-09 0:02 
GeneralSamir?! Pin
Wartickler at 5-Jun-09 3:25
memberWartickler5-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
Web03 | 2.8.150427.1 | Last Updated 4 Jun 2009
Article Copyright 2009 by Member 1072719
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid