<Script type = "text/javascript">
Var currentRowId = 0;
Function SelectRow ()
{
If (event. keyCode = 40)
MarkRow (currentRowId + 1 );
Else if (event. keyCode = 38)
MarkRow (currentRowId-1 );
}
Function MarkRow (rowId)
{
If (document. getElementById (rowId) = null)
Return;
If (document. getElementById (currentRowId )! = Null)
Document. getElementById (currentRowId). style. backgroundColor = '# ffff ';
CurrentRowId = rowId;
Document. getElementById (rowId). style. backgroundColor = '# ff0000 ';
}
</Script>
Then, in the rowDataBound of the gridview, add the event processing function for processing keys and select the event when you click a row with the mouse.
Protected void GridView1_RowDataBound (object sender, GridViewRowEventArgs e)
{
If (e. Row. RowType = DataControlRowType. DataRow)
{
E. Row. Attributes. Add ("id", _ I. ToString ());
E. Row. Attributes. Add ("onKeyDown", "SelectRow ();");
E. Row. Attributes. Add ("onClick", "MarkRow (" + _ I. ToString () + ");");
_ I ++;
}
}
When a row is clicked, It is selected directly, and then the direction keys are moved to switch to different selected rows. If you press the direction keys directly, it is marked from the first row.