Click here to Skip to main content
12,447,778 members (64,397 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: ASP.NET MVC3
Hi,

i am using checkbox in grid, i am getting the combox from seperate table and join

in the main table.

i need validation operation for that so please anyone

can sent sample.


Thank you,
Posted 2-Jul-12 0:04am
Comments
Sandeep Mewara 2-Jul-12 6:23am
   
1. Your question is not quite clear.
2. Did you try anything by yourself?
chetan virkar 2-Jul-12 6:26am
   
can u briefly explain your question or paste your code what u write for that?

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Gridview checkbox validation using javascript

Labels: Gridview, Javascript
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">
<asp:gridview id="gvcheckbox" cssclass="Gridview" runat="server" autogeneratecolumns="False" xmlns:asp="#unknown">
HeaderStyle-BackColor="#7779AF" HeaderStyle-ForeColor="white">

<asp:templatefield>
<HeaderTemplate>
<asp:checkbox id="chkheader" runat="server" />
</HeaderTemplate>

<asp:checkbox id="chkchild" runat="server" />

<HeaderStyle 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()" xmlns:asp="#unknown" />
</form>
</body>
</html>

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

DEMO!


  Permalink  
v2
Comments
ely_bob 21-Jun-13 0:20am
   
decent answer, you can do the trick a little more elegantly using
var inputs = $(':input', GRID);
then looping over inputs
and you have an unused variable being set that isn't even needed...

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160811.3 | Last Updated 2 Jul 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100