.DataSource = dst
DataGrid1.DataBind() Dim txt As TextBox
Dim i As Integer
For i = 0 To DataGrid1.Items.Count - 1
txt= DataGrid1.Items(i).FindControl("textbox1")
txt.Attributes.Add("onkeydown", "if(event.keyCode==13) event.keyCode=9") NextTextBox的焦点即在模板列中加入的TextBox的 TabIndex,每次点击Enter(回车),TextBox的焦点按 Tab 顺序改变如果点击向上建,焦点回到上一个TextBox
DataGrid1.DataBind() Dim txt As TextBox
Dim i As Integer
For i = 0 To DataGrid1.Items.Count - 1
txt= DataGrid1.Items(i).FindControl("textbox1")
txt.Attributes.Add("onkeydown", "if(event.keyCode==13) event.keyCode=9") NextTextBox的焦点即在模板列中加入的TextBox的 TabIndex,每次点击Enter(回车),TextBox的焦点按 Tab 顺序改变如果点击向上建,焦点回到上一个TextBox
<script language=javascript>
//实现键盘上下左右四键遍历网页内相临的文本框
document.onkeydown=keyDown;
function keyDown()
{
if(event.srcElement.tagName.toLowerCase()=='input')
{
var element=event.srcElement;
if(event.keyCode==37)//左
{
var pre=element.parentNode.previousSibling;
var prerow=element.parentNode.parentNode.previousSibling;
if(pre!=null)
{
pre.firstChild.focus();
pre.firstChild.select();
}
else if(prerow!=null)
{
prerow.lastChild.firstChild.focus();
prerow.lastChild.firstChild.select();
}}
else if(event.keyCode==39)//右
{
var next=element.parentNode.nextSibling;
var nextrow=element.parentNode.parentNode.nextSibling;
if(next!=null)
{
next.firstChild.focus();
next.firstChild.select()
}
else if(nextrow!=null)
{
nextrow.firstChild.firstChild.focus();
nextrow.firstChild.firstChild.select();
}
}
else if(event.keyCode==38)//上
{
var cur=element.parentNode;
var curcellIndex=cur.cellIndex;
var curpre=cur.parentNode.previousSibling;
if(curpre!=null)
{
curpre.childNodes(curcellIndex).firstChild.focus();
curpre.childNodes(curcellIndex).firstChild.select();
}
}
else if(event.keyCode==40)//下
{
var cur=element.parentNode;
var curcellIndex=cur.cellIndex;
var curnext=cur.parentNode.nextSibling;
if(curnext!=null)
{
curnext.childNodes(curcellIndex).firstChild.focus();
curnext.childNodes(curcellIndex).firstChild.select();
}
}
}
}
</script>
首先按TAB键跳到下一个Textboxdim myTabIndex as Int32=0;
For i = 0 To DataGrid1.Items.Count - 1
txt= DataGrid1.Items(i).FindControl("textbox1")
txt.Attributes.Add("onkeydown", "if(event.keyCode==13) event.keyCode=9")
txt.Attributes.Add("tabIndex", myTabIndex.ToString())
myTabIndex=myTabIndex+1
Next下面是按上键时返回上一个textbox的JS
<script language =javascript>
document.onkeydown=keyDown;
function keyDown()
{
if(event.srcElement.tagName.toLowerCase()=='input')
{
var element=event.srcElement;
if(event.keyCode==38)//上
{
var cur=element.parentNode;
var curcellIndex=cur.cellIndex;
var curpre=cur.parentNode.previousSibling;
if(curpre!=null)
{
curpre.childNodes(curcellIndex).firstChild.focus();
curpre.childNodes(curcellIndex).firstChild.select();
}
}
}
}
</script>