aspdotnet-suresh offers articles and tutorials,csharp dot net, articles and tutorials,VB.NET Articles,Gridview articles,code examples of 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

Gridview checkbox validation using javascript

Sep 21, 2010

Hi here I will explain how to check the checked status of checkboxes in gridview using JavaScript 


I have a one gridview with checkboxes and one button if user clicks on button I need to raise validation if user doesn’t checked at least one checkbox in gridview for that I have written one JavaScript function to check whether checkboxes selected in gridview or not.

For that write the following code in aspx page

<html xmlns="" >
<head runat="server">
<title>Check all checkboxes if Header checkbox checks</title>
<style type="text/css">
<script type="text/javascript" language="javascript">
function validateCheckBoxes() {
var isValid = false;
var gridView = document.getElementById('<%= gvcheckbox.ClientID %>');
for (var i = 1; i < gridView.rows.length; i++) {
var inputs = gridView.rows[i].getElementsByTagName('input');
if (inputs != null) {
if (inputs[0].type == "checkbox") {
if (inputs[0].checked) {
isValid = true;
return true;
alert("Please select atleast one checkbox");
return false;
<form id="form1" runat="server">
<asp:GridView ID="gvcheckbox" CssClass="Gridview" runat="server" AutoGenerateColumns="False"
HeaderStyle-BackColor="#7779AF" HeaderStyle-ForeColor="white">
<asp:CheckBox ID="chkheader" runat="server" />
<asp:CheckBox ID="chkchild" runat="server"/>
<HeaderStyle HorizontalAlign="Left" />
<ItemStyle HorizontalAlign="Left" />
<asp:BoundField HeaderText = "UserName" DataField="UserName" HeaderStyle-HorizontalAlign="Left" />
<asp:BoundField HeaderText="FirstName" DataField="FirstName" HeaderStyle-HorizontalAlign="Left" />
<asp:BoundField HeaderText="LastName" DataField="LastName" HeaderStyle-HorizontalAlign="Left" />
<asp:button runat="server" Text="Submit" OnClientClick="javascript:validateCheckBoxes()"/>

In code behind bind gridview with some data and check it will work for you


If you enjoyed this post, please support the blog below. It's FREE!

Get the latest,, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email


Anonymous said...

I have been reading Ur blogs since a long time and always I find somthing new and valuable

Thanks alot

msbyuva said...

Hello, how can I exclude the disabled Check boxes, I have some CheckBoxes which are not enabled I want to exclude them How can I do that ??? I added
if (inputs[0].checked) {
But it didn't work for me..11

subrat said...


This article is really helpfull keep on updating.

Thaks a lot

dhaneshwarjeevansingh said...

hi suresh...
im facing a problem with gridview validations...
wat i need is... i hav to perform all validations in gridview editable mode by using javascript...
can u pls help me...

Anonymous said...

Thanks this was really a good help!

Manohara Ravikumar said...

super concept in jquery

nagarjuna said...

helped me alot
thank you

Unknown said...

hi suresh i am validating check box when i am click the send the validation working fine IE 5 But it's not working in IE7 and IE8 can u help me regarding this

Unknown said...

Hi suresh,
please give master page pages using javascript,

priyanka said...

is it possible without using javascript.. then plzz help me..

Anonymous said...

hi I am getting error here 'inputs[0].type == "checkbox" ',Error is input[0] has no property .please help me

Unknown said...


Unknown said...

i have a senairo:
when a checkbox is checked in gridview . a table has a salary column then what are the rows that user have checked i want to sum it in footer. can u help me plz..

anand sutar said...

its tooooo good.............

Mohammed Zaid Meraj said...

I am getting error
Unable to get property 'type' of undefined or null reference

Give your Valuable Comments

Note: Only a member of this blog may post a comment.

© 2015 All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.