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.
<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
No comments:
Post a Comment