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 Email
|
|||
Subscribe by RSS
Subscribe by Email
15 comments :
I have been reading Ur blogs since a long time and always I find somthing new and valuable
Thanks alot
KEEP IT UP
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
Hi..
This article is really helpfull keep on updating.
Thaks a lot
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...
Thanks this was really a good help!
super concept in jquery
Excellent
helped me alot
thank you
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
Hi suresh,
please give master page pages using javascript,
is it possible without using javascript.. then plzz help me..
hi I am getting error here 'inputs[0].type == "checkbox" ',Error is input[0] has no property .please help me
SIRJI,
I HAVE BECOME A FAN OF URS...
U ROCK
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..
its tooooo good.............
I am getting error
Unable to get property 'type' of undefined or null reference
Note: Only a member of this blog may post a comment.