Aspdotnet-Suresh

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

Gridview checkbox validation using javascript

Sep 21, 2010
Introduction

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

Description

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="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Check all checkboxes if Header checkbox checks</title>
<style type="text/css">
.Gridview
{
font-family:Verdana;
font-size:10pt;
font-weight:normal;
color:black;
width:500px;
}
</style>
<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;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="gvcheckbox" CssClass="Gridview" runat="server" AutoGenerateColumns="False"
HeaderStyle-BackColor="#7779AF" HeaderStyle-ForeColor="white">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="chkheader" runat="server" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="chkchild" runat="server"/>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Left" />
<ItemStyle HorizontalAlign="Left" />
</asp:TemplateField>
<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" />
</Columns>
</asp:GridView>
<asp:button runat="server" Text="Submit" OnClientClick="javascript:validateCheckBoxes()"/>
</div>
</form>
</body>
</html>

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

Demo


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

Get the latest Asp.net, C#.net, 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

14 comments :

Anonymous said...

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

Thanks alot
KEEP IT UP

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) {
if(inouts[0].enabled)
{
}
}
But it didn't work for me..11

subrat said...

Hi..

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...

Varun Maggo said...

Thanks this was really a good help!

manohar r said...

super concept in jquery

nagarjuna said...

Excellent
helped me alot
thank you

Narasimhareddy Venna said...

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

Karuppa Samy 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

sarfu ahmed said...

SIRJI,
I HAVE BECOME A FAN OF URS...
U ROCK

sarfu ahmed said...

sir,
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.............

Give your Valuable Comments

Other Related Posts

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