本文介绍了如何在网格视图中触发选择行命令在输入键按下asp.net c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
protected void gridproduct_RowCreated(对象发件人,GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow& &(e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate))
{
e.Row.TabIndex = -1;
e.Row.Attributes [ window.onload;] = string .Format( javascript:SelectRow(this,{0}) ;, 0 );
e.Row.Attributes [ onclick] = string .Format( javascript:SelectRow(this,{0});,e.Row.RowIndex);
e.Row.Attributes [ onkeydown] = javascript:return SelectSibling(event);;
e.Row.Attributes [ onselectstart;] = javascript:return false;;
e.Row.ToolTip = 点击选择行;
e.Row.Cells [ 0 ]。宽度= Unit.Pixel( 35 );
e.Row.Attributes.Add( onkeypress, javascript:if(event.keyCode == 13){__ doPostBack(' + gridproduct.UniqueID + ','选择$ + e.Row.RowIndex.ToString()+ '); return false;});
// e.Row.Attributes [onclick] = this .Page.ClientScript.GetPostBackClientHyperlink(this.gridproduct,Select $+ e.Row.RowIndex);
}
}
我的java脚本
< script type = text / javascript>
var SelectedRow = null ;
var SelectedRowIndex = null ;
var UpperBound = null ;
var LowerBound = null ;
window .onload = function (){
UpperBound = parseInt (' <%= this.gridproduct.Rows .Count%>') - 1 ;
LowerBound = 0 ;
SelectedRowIndex = -1;
}
function SelectRow(CurrentRow,RowIndex){
if (SelectedRow == CurrentRow || RowIndex> UpperBound || RowIndex< LowerBound) return ;
if (SelectedRow!= null ){
SelectedRow。 style.backgroundColor = SelectedRow.originalBackgroundColor;
SelectedRow.style.color = SelectedRow.originalForeColor;
}
if (CurrentRow!= null ){
CurrentRow.originalBackgroundColor = CurrentRow.style.backgroundColor;
CurrentRow.originalForeColor = CurrentRow.style.color;
CurrentRow.style.backgroundColor = ' #DCFC5C';
CurrentRow.style.color = ' Black';
}
SelectedRow = CurrentRow;
SelectedRowIndex = RowIndex;
setTimeout( SelectedRow.focus();, 0 跨度>);
}
function SelectSibling(e){
var e = e? e: window .event;
var KeyCode = e.which? e.which:e.keyCode;
if (KeyCode == 40 )
SelectRow(SelectedRow) .nextSibling,SelectedRowIndex + 1 );
else if (KeyCode == 38 )
SelectRow(SelectedRow.previousSibling,SelectedRowIndex - 1 );
return false ;
}
< / script>
我尝试了什么:
我已经尝试过这些代码,它在上/下箭头的选择行上运行正常,但它没有在Enter键上按下行命令而没有默认选择gridview的第一行
解决方案
protected void gridproduct_RowCreated(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow && (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)) { e.Row.TabIndex = -1; e.Row.Attributes["window.onload";] = string.Format("javascript:SelectRow(this, {0})";, 0); e.Row.Attributes["onclick"] = string.Format("javascript:SelectRow(this, {0});", e.Row.RowIndex); e.Row.Attributes["onkeydown"] = "javascript:return SelectSibling(event);"; e.Row.Attributes["onselectstart";] = "javascript:return false;"; e.Row.ToolTip = "Click to select row"; e.Row.Cells[0].Width = Unit.Pixel(35); e.Row.Attributes.Add("onkeypress", "javascript:if (event.keyCode == 13) { __doPostBack('" + gridproduct.UniqueID + "', 'Select$" + e.Row.RowIndex.ToString() + "'); return false; }"); // e.Row.Attributes["onclick"] = this.Page.ClientScript.GetPostBackClientHyperlink(this.gridproduct, "Select$" + e.Row.RowIndex); } }
My java Script
<script type="text/javascript"> var SelectedRow = null; var SelectedRowIndex = null; var UpperBound = null; var LowerBound = null; window.onload = function () { UpperBound = parseInt('<%= this.gridproduct.Rows.Count %>') - 1; LowerBound = 0; SelectedRowIndex = -1; } function SelectRow(CurrentRow, RowIndex) { if (SelectedRow == CurrentRow || RowIndex > UpperBound || RowIndex < LowerBound) return; if (SelectedRow != null) { SelectedRow.style.backgroundColor = SelectedRow.originalBackgroundColor; SelectedRow.style.color = SelectedRow.originalForeColor; } if (CurrentRow != null) { CurrentRow.originalBackgroundColor = CurrentRow.style.backgroundColor; CurrentRow.originalForeColor = CurrentRow.style.color; CurrentRow.style.backgroundColor = '#DCFC5C'; CurrentRow.style.color = 'Black'; } SelectedRow = CurrentRow; SelectedRowIndex = RowIndex; setTimeout("SelectedRow.focus();", 0); } function SelectSibling(e) { var e = e ? e : window.event; var KeyCode = e.which ? e.which : e.keyCode; if (KeyCode == 40) SelectRow(SelectedRow.nextSibling, SelectedRowIndex + 1); else if (KeyCode == 38) SelectRow(SelectedRow.previousSibling, SelectedRowIndex - 1); return false; } </script>
What I have tried:
I have tried these code its working fine for select row on up/down arrow but it's not firing row command on Enter key press and not select by default first row of gridview
解决方案
这篇关于如何在网格视图中触发选择行命令在输入键按下asp.net c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!