5,542,300 members and growing! (17,897 online)
Email Password   helpLost your password?
Desktop Development » Grid & Data Controls » Grid controls     Intermediate License: The Code Project Open License (CPOL)

Change the GridView row color on click without postback

By farazsk11

Article about changing the GridView row color on mouse click without postback
Javascript, VB (VB 8.0, VB), .NET (.NET, .NET 2.0), ASP.NET, Dev

Posted: 18 Mar 2008
Updated: 18 Mar 2008
Views: 13,790
Bookmarked: 18 times
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
12 votes for this Article.
Popularity: 3.24 Rating: 3.00 out of 5
0 votes, 0.0%
1
0 votes, 0.0%
2
1 vote, 12.5%
3
5 votes, 62.5%
4
2 votes, 25.0%
5
Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article

Introduction

This article is about changing the color of a GridView row on mouse click without having a postback on the page. In order to achieve this we will be using some javascript and off course the GridView Control itself :).

Background

GridView is a server side control that contains Rows and Columns. In some cases we want to do something more than the straight forward functionality of the GridView control depending on the functional/UI requirements of the application.

Using the code

Following javascript will be used to change the color of the row.

        <script type="text/javascript">
        //variable that will store the id of the last clicked row
        var previousRow;
        
        function ChangeRowColor(row)
        {
            //If last clicked row and the current clicked row are same
            if (previousRow == row)
                return;//do nothing
            //If there is row clicked earlier
            else if (previousRow != null)
                //change the color of the previous row back to white
                document.getElementById(previousRow).style.backgroundColor = "#ffffff";
            
            //change the color of the current row to light yellow

            document.getElementById(row).style.backgroundColor = "#ffffda";            
            //assign the current row id to the previous row id 
            //for next row to be clicked
            previousRow = row;
        }
     </script>

Following code will be required on GridView1_RowDataBound event

    Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) _
        Handles GridView1.RowDataBound
        If (e.Row.RowType = DataControlRowType.DataRow) Then
            e.Row.Attributes.Add("onclick", "javascript:ChangeRowColor('" & e.Row.ClientID & "')")
        End If
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            'FillDataTable is a function that will return a DataTable
            'with some values and is available in the code for download.
            Me.GridView1.DataSource = Me.FillDataTable()
            Me.GridView1.DataBind()
        End If
    End Sub
        

Before clicking the row

After clicking the row

License

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

About the Author

farazsk11


I am a Software Engineer working for a company located in Sharjah, UAE. I like developing new applications with the latest technologies. Mostly reponsible for web applications using Microsoft.Net. I have done MCPD so far. Other than work play guitars, sing and play PSP.
Occupation: Web Developer
Location: United Arab Emirates United Arab Emirates

Other popular Grid & Data Controls articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 15 of 15 (Total in Forum: 15) (Refresh)FirstPrevNext
Subject  Author Date 
GeneralHi farazmemberVimalSaifudin20:40 26 Sep '08  
GeneralGreat Solution But How Do I Handle Postbacks?membereggnturtle12:45 20 Sep '08  
GeneralRe: Great Solution But How Do I Handle Postbacks?memberfarazsk1121:06 20 Sep '08  
GeneralRe: Great Solution But How Do I Handle Postbacks?membereggnturtle5:14 21 Sep '08  
GeneralGood workmemberMember 418340820:05 8 Sep '08  
GeneralRediculously simplisticmemberOakman13:40 5 Apr '08  
GeneralRe: Rediculously simplisticmemberfarazsk1119:48 5 Apr '08  
GeneralRe: Rediculously simplisticmemberOakman2:24 6 Apr '08  
GeneralRe: Rediculously simplisticmemberfarazsk113:17 6 Apr '08  
GeneralMultiple RowsmemberDigitalRacer12:56 4 Apr '08  
GeneralRe: Multiple Rowsmemberfarazsk1119:44 5 Apr '08  
GeneralGood WorkmemberIrwan Hassan18:41 26 Mar '08  
GeneralRe: Good Workmemberfarazsk1116:43 27 Mar '08  
Generalthanksmemberjomet18:53 19 Mar '08  
GeneralRe: thanksmemberfarazsk117:37 20 Mar '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 18 Mar 2008
Editor: Sean Ewington
Copyright 2008 by farazsk11
Everything else Copyright © CodeProject, 1999-2008
Web07 | Advertise on the Code Project