ASP.NET网站,通过上下键控制Gridview行焦点的移动

直接贴代码了,代码来自CSDN上一位朋友的回复,效果不错,主要还是js的东西了。在大数据量时最好别搞这么复杂

system.xml:(作为数据源)

<?xml version="1.0" encoding="utf-8"?>
<Assembly name="System">
 <Type kind="class" namespace="System" name="System.ComponentModel.LicenseException">
 </Type>
 <Type kind="class" namespace="System" name="System.ComponentModel.InvalidEnumArgumentException">
 </Type>
 <Type kind="class" namespace="System" name="System.Net.CookieException">
 </Type>
 <Type kind="class" namespace="System" name="System.Net.Sockets.SocketException"
 </Type>
</Assembly>

Default2.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>无标题页</title>
</head>
<body bgColor="#c0c0c0" onload="window_onload()">
    <form id="form1" runat="server">
    <div>
    
    </div>
        <asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridView1_RowDataBound" AutoGenerateColumns="False" DataSourceID="XmlDataSource1" >
            <Columns>
                <asp:BoundField DataField="kind" HeaderText="kind" SortExpression="kind" />
                <asp:BoundField DataField="namespace" HeaderText="namespace" SortExpression="namespace" />
                <asp:BoundField DataField="name" HeaderText="name" SortExpression="name" />
            </Columns>
        </asp:GridView>
        <asp:XmlDataSource ID="XmlDataSource1" runat="server" DataFile="~/system.xml"></asp:XmlDataSource>
        <CENTER><INPUT id=lineNum type=hidden value=<%=i %> name=lineNum><BUTTON 
      language=javascript id=btnCancel 
      onclick="return btnCancel_onclick()">关闭窗口</BUTTON> </CENTER>
    </form>
</body>
</html>
<SCRIPT language="javascript" id="clientEventHandlersJS">
<!--
//------------------全局变量-------------------------
var preColor="#e8e9fc";//保存行原来的颜色
var selectedItem;//选定的行

//---------------------------------------------------
//--------------funtion selectItem-------------------
//选定一行
//---------------------------------------------------
function selectItem(item,selected) {
var aitem = item;
if (aitem !=null && aitem.item == "1"){
while(aitem.tagName != "TR"){
aitem = aitem.parentElement;
}
if(selected){
aitem.bgColor = "#0101aa";
aitem.style.color = "#ffffff";
selectedItem = aitem;//当前选中的行
}
else{
aitem.bgColor="#e8e9fc";
aitem.style.color = "#000000";
selectedItem = null;
}
preColor =aitem.bgColor;
}
}

function document_onmouseover(o) {

        o.bgColor = "#e8f9fc";
        o.style.color = "#000000";
}

function document_onmouseout(o) {

    if(o!=selectedItem)
    {
        o.bgColor = "#e8e9fc";
        o.style.color = "#000000";
    }
        else
    {
        o.bgColor = "#0101aa";
        o.style.color = "#ffffff";
    }
}

function document_onclick(o) {
selectItem(selectedItem,false);//清空上一次选中的状态
selectedItem=o;
selectItem(selectedItem,true);

}
function TR_ondblclick(){
   //alert(selectedItem.item);
if(selectedItem.item == "1"){
var ysbm=selectedItem.cells("ysbm").ysbm;
var xm=selectedItem.cells("xm").innerText;
var ksbm=selectedItem.cells("ysbm").ksbm;
window.returnValue = new Array(ysbm,xm,ksbm);
        window.close();


}
}
function btnCancel_onclick() {
window.close();
}
function document_onkeydown() {
if (selectedItem!=null){
//alert(selectedItem.line);
var line=parseInt(selectedItem.line);//从0计
var upline=line-1;
var downline=line+1;
var lineNum=parseInt(document.all("lineNum").value)-1;//总行数减一
if (line>0)
   upline=line-1
else
   upline=line
if (line<lineNum)
   downline=line+1;
else
   downline=line

if (event.keyCode==13){//回车返回值
   selectedItem.ondblclick();
   //alert("dd");
}
//alert(event.keyCode);
if (event.keyCode==38){//向上方向箭
selectItem(selectedItem,false);
selectItem(document.all("row"+upline.toString()),true);

}
if (event.keyCode==40){//向下方向箭
selectItem(selectedItem,false);
selectItem(document.all("row"+downline.toString()),true);
}
}
}
function window_onload(){
//alert(document.all("row0"))
if (document.all("row0")=="[object]"){
   selectItem(document.all("row0"),true);
}else{
btnCancel.focus();
}

}
//-->
</SCRIPT>

<SCRIPT language="javascript" event="onclick" for="oTable">
<!--
 document_onclick()
//-->
</SCRIPT>

<SCRIPT language="javascript" event="onkeydown" for="document">
<!--
 document_onkeydown()
//-->
</SCRIPT>

Default.aspx.cs:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class Default2 : System.Web.UI.Page
{
    public int i = 0;

    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
       

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes.Add("line", i.ToString());
            e.Row.Attributes.Add("id", string.Format("row{0}", i));
            e.Row.Attributes.Add("style", "CURSOR: hand");
            e.Row.Attributes.Add("bgColor", "#e8e9fc");
            e.Row.Attributes.Add("item", "1");
            e.Row.Attributes.Add("onmousemove", "document_onmouseover(this)");
            e.Row.Attributes.Add("onmouseout", "document_onmouseout(this)");
            e.Row.Attributes.Add("onclick", "document_onclick(this)");
            i++;
        }
    }
}



运行就好了。

你可能感兴趣的:(ASP.NET网站,通过上下键控制Gridview行焦点的移动)