Quantcast
Channel: C# – Random Technical Thoughts
Viewing all articles
Browse latest Browse all 17

How to add a row click event to a gridview

$
0
0

So I ran across the problem of I need a web page to do something if a user clicked a row in a grid.
It wasn’t going to work for the user to click a button, the whole row had to be clickable.

Adding a row data bound event solved the problem.
I also change the background color on mouse over (and mouse out).

When the user clicks the row they get redirected to a page (with a query string parm so I know what row they clicked on).

 

   1: protected void grid_RowDataBound(object sender, GridViewRowEventArgs e)

   2: {

   3:     if (e.Row.RowType == DataControlRowType.DataRow)

   4:     {

   5:         e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#ceedfc'");

   6:         e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=''");

   7:         e.Row.Attributes.Add("style", "cursor:pointer;");

   8:         e.Row.Attributes.Add("onclick", "location='detail.aspx?id=" + e.Row.Cells[0].Text + "'");

   9:     }

  10: }



Viewing all articles
Browse latest Browse all 17

Trending Articles