Change GridView RowColor OnMouseClick
和上一片文章类似,只是触发的事件变成了onclick
主要代码
protected void MyGridView_RowCreated(object sender, GridViewRowEventArgs e)
{
string rowID = String.Empty;
if (e.Row.RowType == DataControlRowType.DataRow)
{
rowID = "row"+e.Row.RowIndex;
e.Row.Attributes.Add("id","row"+e.Row.RowIndex);
e.Row.Attributes.Add("onclick","ChangeRowColor(" +"'" + rowID + "'" + ")");
}
}
JavaScript Function:
<script
language
="javascript"
type="text/javascript">
document.body.style.cursor = 'pointer'; var oldColor = '';
function ChangeRowColor(rowID) { var color = document.getElementById(rowID).style.backgroundColor; if(color != 'yellow') oldColor = color; if(color == 'yellow') document.getElementById(rowID).style.backgroundColor = oldColor; else document.getElementById(rowID).style.backgroundColor = 'yellow'; } </script> |