Highlighting Rows in a GridView
Scritto da
Massimiliano Bellisario il
giovedì 3 giugno 2010
•
Linguaggio:
• Livello: 100
Per implementare un effetto di colorazione delle righe di una
GridView è sufficiente implementare i gestori degli eventi
javascript onmouseover e onmouseout. Per fare ciò possiamo
utilizzare l'evento RowDataBound della classe GridView:
C#
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.RowState == DataControlRowState.Alternate || e.Row.RowState == DataControlRowState.Normal)
{
e.Row.Attributes.Add("onmouseover",
"this.savedColor=this.style.backgroundColor;this.style.backgroundColor='Yellow'");
e.Row.Attributes.Add("onmouseout",
"this.style.backgroundColor=this.savedColor;");
}
}
}
VB.NET
Private 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
If e.Row.RowState = DataControlRowState.Alternate Or e.Row.RowState = DataControlRowState.Normal Then
e.Row.Attributes.Add("onmouseover", _
"this.savedColor=this.style.backgroundColor;this.style.backgroundColor='Yellow'")
e.Row.Attributes.Add("onmouseout", _
"this.style.backgroundColor=this.savedColor;")
End If
End If
End Sub
Tags: GridView