Click here to Skip to main content
Licence CPOL
First Posted 18 Mar 2008
Views 76,678
Downloads 464
Bookmarked 30 times

Change the GridView row color on click without postback

By farazsk11 | 18 Mar 2008 | Unedited contribution
Article about changing the GridView row color on mouse click without postback
1 vote, 5.9%
1

2
1 vote, 5.9%
3
9 votes, 52.9%
4
6 votes, 35.3%
5
4.14/5 - 21 votes
1 removed
μ 3.33, σa 1.75 [?]

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

Web Developer

United Arab Emirates United Arab Emirates

Member
Faraz is working as a Senior Software Engineer for a company located in Sharjah, UAE. He likes developing new applications with the latest technologies. Mostly reponsible for web applications using Microsoft.Net. He has done MCPD so far. Other than work play guitars, sing and play PSP.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 5 Pinmembermhm362320:01 1 Oct '11  
GeneralMy vote of 4 Pinmemberlinda mathew5:16 26 Mar '11  
GeneralHandling Postbacks Pinmemberscottch6:18 15 Jun '10  
Generalthanks Pinmemberadolfhardik21:08 9 Jun '10  
GeneralMy vote of 1 PinmemberSaeed Alg0:38 24 Apr '10  
Generalupdate PreviousRowColor PinmemberMember 440165421:53 15 Apr '09  
GeneralHi faraz PinmemberVimalSaifudin20:40 26 Sep '08  
QuestionGreat Solution But How Do I Handle Postbacks? Pinmembereggnturtle12:45 20 Sep '08  
AnswerRe: Great Solution But How Do I Handle Postbacks? Pinmemberfarazsk1121:06 20 Sep '08  
GeneralRe: Great Solution But How Do I Handle Postbacks? Pinmembereggnturtle5:14 21 Sep '08  
GeneralGood work PinmemberMember 418340820:05 8 Sep '08  
GeneralRediculously simplistic PinmemberOakman13:40 5 Apr '08  
GeneralRe: Rediculously simplistic Pinmemberfarazsk1119:48 5 Apr '08  
GeneralRe: Rediculously simplistic PinmemberOakman2:24 6 Apr '08  
GeneralRe: Rediculously simplistic Pinmemberfarazsk113:17 6 Apr '08  
GeneralMultiple Rows PinmemberDigitalRacer12:56 4 Apr '08  
GeneralRe: Multiple Rows Pinmemberfarazsk1119:44 5 Apr '08  
GeneralGood Work PinmemberIrwan Hassan18:41 26 Mar '08  
GeneralRe: Good Work Pinmemberfarazsk1116:43 27 Mar '08  
Generalthanks Pinmemberjomet18:53 19 Mar '08  
cool feature!!!
but i cant download your source code. Cry | :((
thanks Laugh | :laugh:
GeneralRe: thanks Pinmemberfarazsk117:37 20 Mar '08  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120210.1 | Last Updated 19 Mar 2008
Article Copyright 2008 by farazsk11
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid