/*! *tableId:表格ID *rowIndex:行号 *left:左边控件ID *columnName:当前控件ID *right:右边控件ID */ function inputKeyDown(tableId,rowIndex,left, columnName,right) { key=window.event.keyCode; if(key>=37 && key<=40) { var index=rowIndex; var name=columnName; if (key==38 && rowIndex>1){//↑ index=rowIndex-1; }else if (key==40 ){//↓ index=rowIndex+1; }else if (key==37 && left){//← name=left; }else if (key==39 && right){//→ name=right; } if(index>0 && name) { if(index<=9) index="0"+index; var obj=document.getElementById(tableId+"_ctl"+(index)+"_"+name); if(obj && obj.disabled) obj.focus(); } } }
该方法主要是针对Asp.net数据源控件自动生成行。如GridView的模板列生成。因为生成的控件的ID格式是固定的,如:数据源控件ID+“_clt”+行索引+“_”+该控件ID
<INPUT style="WIDTH: 90%" onkeydown="inputKeyDown('gvBuyAppDetailItem',2,'number',this.id,'hfUnit')" id=’gvBuyAppDetailItem_ctl02_MaterialID’ class=’TextBox’>在行生成事件中添加代码:
if (e.Row.RowType == DataControlRowType.DataRow) { for (int i = 0; i < e.Row.Cells.Count; i++) { if (e.Row.Cells[i].Controls.Count > 0 && e.Row.Cells[i].Controls[0] is WebControl) { WebControl control=e.Row.Cells[i].Controls[0] as WebControl; string left = null; string right=null; if (i - 1 >= 0 && e.Row.Cells[i - 1].Controls.Count > 0) left = e.Row.Cells[i - 1].Controls[0].ID; if (i + 1 <e.Row.Cells.Count && e.Row.Cells[i + 1].Controls.Count > 0) right = e.Row.Cells[i + 1].Controls[0].ID; control.Attriubter.Add("onkeydown",string.Format( "inputKeyDown('{0}',{1},'{2}',{3} ,'{4}')",“GridView的id”,e.Row.RowIndex+2,left,control.ID,right)); } } }