Welcome to AspAdvice Sign in | Join | Help

AzamSharp

Some day I will know everything I hope that day never comes

Syndication

Tags

Navigation

Highlight GridView Rows Using CheckBox

Highlighting GridView rows using onmouseover, onclick and using checkbox are all similar features. All are pretty simple to implement. The main problem rises when using the same technique on a GridView with alternate rows with different color. Let's check out how this can be done.

First, here is the HTML code for the GridView control.

<asp:GridView ID="gvCategories" runat="server" AutoGenerateColumns="False" BackColor="LightGoldenrodYellow" BorderColor="Tan" BorderWidth="1px" CellPadding="2" ForeColor="Black" GridLines="None">

<Columns>

<asp:TemplateField>

<ItemTemplate>

<asp:CheckBox ID="chkSelect" runat="server" onclick="changeColor(this)" />

</ItemTemplate>

</asp:TemplateField>

<asp:TemplateField HeaderText="Name">

<ItemTemplate>

<asp:Label ID="lblCategoryName" runat="server" Text='<%# Eval("CategoryName") %>' />

</ItemTemplate>

</asp:TemplateField>

<asp:TemplateField HeaderText="Description">

<ItemTemplate>

<asp:Label ID="lblDescription" runat="server" Text='<%# Eval("Description") %>' />

</ItemTemplate>

</asp:TemplateField>

</Columns>

<FooterStyle BackColor="Tan" />

<PagerStyle BackColor="PaleGoldenrod" ForeColor="DarkSlateBlue" HorizontalAlign="Center" />

<SelectedRowStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" />

<HeaderStyle BackColor="Tan" Font-Bold="True" />

<AlternatingRowStyle BackColor="PaleGoldenrod" />

</asp:GridView>

The ChangeColor function is fired whenever a checkbox is clicked.

One important point to note about Alternate rows is that there is no color assigned to them. They get the color from the style property of the table. In other words we only assign the style to the even numbered rows.

<script language="javascript" type="text/javascript">

var color = '';

function changeColor(obj)

{

var rowObject = getParentRow(obj);

var parentTable = document.getElementById("gvCategories");

if(color == '')

{

color = getRowColor();

}

if(obj.checked)

{

rowObject.style.backgroundColor = 'Yellow';

}

else

{

rowObject.style.backgroundColor = color;

color = '';

}

// private method

function getRowColor()

{

if(rowObject.style.backgroundColor == '') return parentTable.style.backgroundColor;

else return rowObject.style.backgroundColor;

}

}

// This method returns the parent row of the object

function getParentRow(obj)

{

do

{

obj = obj.parentElement;

}

while(obj.tagName != "TR")

return obj;

}

 

</script>

Published Tuesday, June 26, 2007 1:09 PM by azamsharp

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# re: Highlight GridView Rows Using CheckBox @ Saturday, June 30, 2007 3:23 AM

I would likt to know if we can use background image in place of background color. Can u provide a sample on that

Asw

# re: Highlight GridView Rows Using CheckBox @ Wednesday, August 22, 2007 1:41 PM

Hi. Thanks for the post. It worked well for me. The problem I'm having however, is that my gridview is inside a AJAX asp:UpdatePanel and when other events get triggered and the screen is updated.. say with some new visible, the background color changes back to the normal color. I no longer have the "selected" color. Any thoughts on how I can get around this?

Nick W.

# re: Highlight GridView Rows Using CheckBox @ Thursday, August 23, 2007 3:07 PM

Hi Nick W,

It seems like when the Update Panel is fired it refreshes the GridView control and hence loads the old style. The only way is to fire the function which remembers the row color and then paints it after the GridView is again created.

azamsharp

# re: Highlight GridView Rows Using CheckBox @ Tuesday, August 28, 2007 11:17 PM

Abbbo Super

Vasu

Leave a Comment

(required) 
required 
(required) 
Enter the code you see below