日期:2014-05-17  浏览次数:20443 次

如何用上下键在gridview中切换行
rt

------解决方案--------------------
这好像有点难度 我写过最多是用JS实现,keydow事件时判断点击是哪个键 然后在做处理。
------解决方案--------------------
前台html:
HTML code
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>无标题页</title>
   <script>
   var FocusIndex=0;
   function InitFocus(obj)
   {
       if(obj.rows.length>0)
        {
           FocusIndex=0;
           SetBK(obj,FocusIndex);
        }
   }
   function Show(e,obj)
   {
        var len=obj.rows.length;
        if(len==0) return; 
        e = e || window.event;
        if(e.keyCode==38&&FocusIndex>0)
        {
            FocusIndex--;
            SetBK(obj,FocusIndex);
        }
        if(e.keyCode==40&&FocusIndex<len-1)
        {
            FocusIndex++;
           SetBK(obj,FocusIndex);
        }
   }
   function SetBK(obj,index)
   {
       for(var i=0;i<obj.rows.length;i++)
       {
            if(i==FocusIndex) obj.rows[i].style.backgroundColor="red";
            else obj.rows[i].style.backgroundColor="";
       } 
   }
   </script> 
</head>
<body>
    <form id="form1" runat="server">
    <div>
        点击gridview后按上下键看看效果
    </div>
    </form>
</body>
</html>

------解决方案--------------------
学习了

------解决方案--------------------
3楼写的很不错
------解决方案--------------------
学习下.................
------解决方案--------------------
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 = '#ffffff';
currentRowId = rowId;
document.getElementById(rowId).style.backgroundColor = '#ff0000';
}
</script>

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++;
}
}