Click here to Skip to main content
15,891,204 members
Articles / Web Development / ASP.NET

How to validate atleast one checkbox is selected in a grid

Rate me:
Please Sign up or sign in to vote.
1.00/5 (1 vote)
4 Jun 2009CPOL 39.4K   10   6
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.NET
<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:

JavaScript
<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.NET
<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)


Written By
Web Developer Clover
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionHow to validate checkbox list inside gridview Pin
Member 102176784-Sep-17 21:23
Member 102176784-Sep-17 21:23 
GeneralMy vote of 1 Pin
C.V.Vikram15-Sep-09 23:17
C.V.Vikram15-Sep-09 23:17 
Lot of problems and no source code attached.
GeneralThere could be a problem Pin
Irwan Hassan18-Jun-09 6:59
Irwan Hassan18-Jun-09 6:59 
GeneralRe: There could be a problem Pin
Member 107271920-Jun-09 4:58
Member 107271920-Jun-09 4:58 
GeneralError message Pin
Member 25604728-Jun-09 23:02
Member 25604728-Jun-09 23:02 
GeneralSamir?! Pin
Wartickler5-Jun-09 2:25
Wartickler5-Jun-09 2:25 

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

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