In previous article I explained select/deselect checkboxes in gridview using JQuery, select header checkbox when all child checkboxes selected in JQuery and many articles relating to JQuery. Here I am using select header checkbox when all child checkboxes selected in JQuery concept to highlight gridview row when checkbox is checked in JQuery.
$("[id$=chkChild]").click(function() {
if(this.checked) {
$(this).parent().parent().addClass('highlightRow');
}
else {
$(this).parent().parent().removeClass('highlightRow');
}
});
|
Column
Name
|
Data
Type
|
Allow
Nulls
|
UserName
|
varchar(50)
|
Yes
|
LastName
|
varchar(50)
|
Yes
|
Location
|
Varchar(50)
|
Yes
|
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1"
runat="server">
<title>Highlight row when checkbox is checked in gridview asp.net</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js">
</script>
<script type="text/javascript">
$(document).ready(function()
{
// Header Checkbox click function
$('[id$=chkHeader]').click(function() {
if ($('[id$=chkHeader]:checked').length>0)
{
$('[id$=chkChild]').parent().parent().addClass('highlightRow');
}
else {
$('[id$=chkChild]').parent().parent().removeClass('highlightRow');
}
$("[id$=chkChild]").attr('checked', this.checked);
});
// Child Checkbox click function
$("[id$=chkChild]").click(function() {
if(this.checked) {
$(this).parent().parent().addClass('highlightRow');
}
else {
$(this).parent().parent().removeClass('highlightRow');
}
if ($('[id$=chkChild]').length
== $('[id$=chkChild]:checked').length) {
$('[id$=chkHeader]').attr("checked", "checked");
}
else {
$('[id$=chkHeader]').removeAttr("checked");
}
});
});
</script>
<style type="text/css">
.highlightRow
{
background-color:Green;
Color:White;
}
</style>
</head>
<body>
<form id="form1"
runat="server">
<div>
<asp:GridView ID="gvUserInfo"
runat="server">
<HeaderStyle BackColor="#df5015" Font-Bold="true" ForeColor="White" />
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="chkHeader"
runat="server"
/>
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="chkChild" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
|
using
System;
using
System.Data;
using
System.Data.SqlClient;
|
protected
void Page_Load(object
sender, EventArgs e)
{
if
(!IsPostBack)
{
BindGridview();
}
}
// This method is used to bind gridview from database
protected
void BindGridview()
{
SqlConnection con = new SqlConnection("Data
Source=SureshDasari;Integrated Security=true;Initial Catalog=MySampleDB");
con.Open();
SqlCommand cmd = new SqlCommand("select
TOP 10 UserName,LastName,Location from UserInformation", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet
ds = new DataSet();
da.Fill(ds);
gvUserInfo.DataSource
= ds;
gvUserInfo.DataBind();
}
|
Imports
System.Data
Imports
System.Data.SqlClient
Partial
Class VBCode
Inherits
System.Web.UI.Page
Protected
Sub Page_Load(ByVal
sender As Object,
ByVal e As
EventArgs) Handles Me.Load
If
Not IsPostBack Then
BindGridview()
End
If
End
Sub
' This method is used to bind gridview from database
Protected
Sub BindGridview()
Dim
con As New
SqlConnection("Data Source=SureshDasari;Integrated
Security=true;Initial Catalog=MySampleDB")
con.Open()
Dim
cmd As New
SqlCommand("select TOP 10
UserName,LastName,Location from UserInformation", con)
Dim
da As New
SqlDataAdapter(cmd)
Dim
ds As New
DataSet()
da.Fill(ds)
gvUserInfo.DataSource
= ds
gvUserInfo.DataBind()
End
Sub
End
Class
|
|
|
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
4 comments :
Not working properly
It's working properly i tested it in all broswers. Please check your code.
hi suresh
how to write insert(query) gridview checkbox values in database
(multiplerows insert)
css is not working so background is not changed after checked on checkbox...