Click here to Skip to main content
15,885,244 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Comments
Sandeep Mewara 2-Jul-12 6:23am    
1. Your question is not quite clear.
2. Did you try anything by yourself?
[no name] 2-Jul-12 6:26am    
can u briefly explain your question or paste your code what u write for that?

1 solution

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">
<columns><asp:templatefield>
<HeaderTemplate>
<asp:checkbox id="chkheader" runat="server" />
</HeaderTemplate>
<itemtemplate>
<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()" xmlns:asp="#unknown" />

</form>
</body>
</html>

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

DEMO!

 
Share this answer
 
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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900