在aspx中定义javascript事件,判断按键是否为上下键;
然后在gridview的rowDataBound中, 添加处理按键的事件处理函数和使用鼠标点击某行时的选中事件.
- <scripttype="text/javascript">
- varcurrentRowId=0;
- functionSelectRow()
- {
- if(event.keyCode==40)
- MarkRow(currentRowId+1);
- elseif(event.keyCode==38)
- MarkRow(currentRowId-1);
- }
- functionMarkRow(rowId)
- {
- if(document.getElementById(rowId)==null)
- return;
- if(document.getElementById(currentRowId)!=null)
- document.getElementById(currentRowId).style.backgroundColor='#ffffff';
- currentRowId=rowId;
- document.getElementById(rowId).style.backgroundColor='#ff0000';
- }
- </script>
然后在gridview的rowDataBound中, 添加处理按键的事件处理函数和使用鼠标点击某行时的选中事件.
- protectedvoidGridView1_RowDataBound(objectsender,GridViewRowEventArgse)
- {
- 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++;
- }
- }
本文介绍如何在ASPX页面中使用JavaScript实现键盘上下键事件处理,并通过改变表格行背景色来反馈用户交互。该方法适用于网格视图的导航操作。
229

被折叠的 条评论
为什么被折叠?



