<%@ 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>