<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication4.WebForm1" %>
<!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 runat="server">
<title></title>
<script type="text/javascript">
function keyPressed(TB) {
var tblGrid = document.getElementById("DataGrid1");
var TBID = document.getElementById(TB); //当前ID
var CurCellID = TBID.parentElement; //当前TD
var CurCellIndex = CurCellID.cellIndex; //当前是第几列
var CurRowIndex = CurCellID.parentElement.rowIndex; //当前是第行;
var rowcount = tblGrid.rows.length; //总行
var CellCount = tblGrid.rows[CurRowIndex].cells.length; //当前行的总列数
var TargateRow = -1; //目标行(默认值为-1)
var TargateCell = -1; //目标列 (默认值为-1)
var R, C;
if (event.keyCode == 37 || event.keyCode == 38 ||
event.keyCode == 39 || event.keyCode == 40 || event.keyCode == 13) {
if (tblGrid.rows[CurRowIndex].cells[CurCellIndex].children[0] != null) {
if (tblGrid.rows[CurRowIndex].cells[CurCellIndex].children[0].id == TBID.id) {
//向下光标****************************************************************************
if (event.keyCode == 40)//Buttom
{
if (CurRowIndex < rowcount - 1) {
if (tblGrid.rows[CurRowIndex + 1].cells[CurCellIndex].children[0] != null) {
if (tblGrid.rows[CurRowIndex + 1].cells[CurCellIndex].children[0].type == 'text') {
//downvalue
tblGrid.rows[CurRowIndex + 1].cells[CurCellIndex].children[0].focus();
return false;
}
}
}
else {
//当在最后一行按向下光标键时,新增一行空白行
}
}
//END****************************************************************************
//向上光标(当前行必须大于第一行)******************************************************
if (event.keyCode == 38 && CurRowIndex > 1)//up
{
if (tblGrid.rows[CurRowIndex - 1].cells[
CurCellIndex].children[0] != null) {
if (tblGrid.rows[CurRowIndex - 1].cells[
CurCellIndex].children[0].type == 'text') {
//upvalue
tblGrid.rows[CurRowIndex - 1].cells[
CurCellIndex].children[0].focus();
return false;
}
}
}
//END****************************************************************************
//向左光标(当前列为第0列时则转向上一行的最后一列)*******************************
if (event.keyCode == 37)//left
{
Outer:
for (R = CurRowIndex; R > 0; R--) {
for (C = CurCellIndex - 1; C >= 0; C--) {
if (tblGrid.rows[R].cells[C].children[0] != null
&& tblGrid.rows[R].cells[C].children[0].type == 'text') {
TargateRow = R;
TargateCell = C;
break Outer;
}
}
CurCellIndex = CellCount; //当本行中当前列之前的列中没有找到所需的单元格,则下上行从最后一列处开始
}
if (TargateRow == -1 || TargateCell == -1) {
return false;
}
if (tblGrid.rows[TargateRow].cells[TargateCell].children[0].value != '') {
var cPos = getCaretPos(tblGrid.rows[TargateRow].cells[TargateCell].children[0], 'left');
if (cPos) {
tblGrid.rows[TargateRow].cells[TargateCell].children[0].focus();
return false;
}
else {
return false;
}
}
tblGrid.rows[TargateRow].cells[TargateCell].children[0].focus();
return false;
}
//END****************************************************************************
//向右光标(当前列为最后一列时,则转向下一行的第一列)******************************
if ((event.keyCode == 39 || event.keyCode == 13)) //right(按回车也向右移,回车等同于向右光标键)
{
Outer:
for (R = CurRowIndex; R < rowcount; R++) {
for (C = CurCellIndex + 1; C < CellCount; C++) {
if (tblGrid.rows[R].cells[C].children[0] != null
&& tblGrid.rows[R].cells[C].children[0].type == 'text') {
TargateRow = R;
TargateCell = C;
break Outer;
}
}
CurCellIndex = -1; //当本行中当前列之后的列中没有找到所需的单元格,则下一行从第0列处开始
}
if (TargateRow == -1 || TargateCell == -1) {
return false;
}
if (tblGrid.rows[TargateRow].cells[TargateCell].children[0].value != '') {
var cPosR = getCaretPos(tblGrid.rows[TargateRow].cells[TargateCell].children[0], 'right');
if (cPosR) {
tblGrid.rows[TargateRow].cells[TargateCell].children[0].focus();
return false;
}
else {
return false;
}
}
tblGrid.rows[TargateRow].cells[TargateCell].children[0].focus();
return false;
}
//END****************************************************************************
}
}
}
}
function getCaretPos(control, way) {
var movement;
if (way == 'left') {
movement = -1;
}
else {
movement = 1;
}
if (control.createTextRange) {
control.caretPos = document.selection.createRange().duplicate();
if (control.caretPos.move("character", movement) != '') {
return false;
}
else {
return true;
}
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NewsWebsiteConnectionString %>"
SelectCommand="SELECT * FROM [news]"></asp:SqlDataSource>
<br />
<asp:DataGrid ID="DataGrid1" Style="left: 17px; position: absolute; top: 93px; z-currowindex: 101"
runat="server" Width="632px" AutoGenerateColumns="False" DataSourceID="SqlDataSource1">
<Columns>
<asp:TemplateColumn HeaderText="ID">
<ItemTemplate>
<asp:TextBox ID="TextBox4" runat="server" οnkeyup="keyPressed(this.id)" Text='<%# Eval("id") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn>
<ItemTemplate>
<asp:TextBox οnkeyup="keyPressed(this.id)" Text='<%# Eval("title") %>' runat="server"
ID="test0">
</asp:TextBox>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn>
<ItemTemplate>
<asp:TextBox οnkeyup="keyPressed(this.id)" Text='<%# Eval("content") %>' runat="server"
ID="Textbox5"></asp:TextBox>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn>
<ItemTemplate>
<asp:TextBox οnkeyup="keyPressed(this.id)" Text='<%# Eval("content") %>' runat="server"
ID="Textbox6"></asp:TextBox>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
</form>
</body>
</html>