如何滚动到GridView中的选定行

现在,我正在使用ASP.net和C#。

我有一个PageSize = 20(20行)的GridView。

但是,它只能显示10行,并且会出现垂直滚动条。

我的问题是……
当发生回发时,它会跳转到网格的顶行但是我选择了任何其他行。 我想将其滚动到选定的行。 我怎么能这样做 请帮我。

带着敬意,

在页面指令中添加MaintainScrollPositionOnPostback

 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" MaintainScrollPositionOnPostback ="true"%> 

另一种方法,使用包装GridView的DIV的scrollTop方法:

 private void ScrollGrid() { int intScrollTo = this.gridView.SelectedIndex * (int)this.gridView.RowStyle.Height.Value; string strScript = string.Empty; strScript += "var gridView = document.getElementById('" + this.gridView.ClientID + "');\n"; strScript += "if (gridView != null && gridView.parentElement != null && gridView.parentElement.parentElement != null)\n"; strScript += " gridView.parentElement.parentElement.scrollTop = " + intScrollTo + ";\n"; ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "ScrollGrid", strScript, true); } 

编辑:这不会有几个原因:

1)如果gridView在一个NamingContainer控件内,就像Panel一样,因为客户端的ClientId不是ClientId 。 您需要使用Teh控件的UniqueId

2)你不能相信行高来计算滚动位置。 如果任何列中的文本换行到多行,或者任何行包含的内容高于样式,则行的大小将不同

3)不同的浏览器可以有不同的行为。 你最好使用jQuery scrollTop()scroll()函数。 要使用它们,必须在客户端使用scrollTop并设置可在服务器端读取的HiddenControl的值以重置位置。 在客户端呈现行之前,您无法获得浏览器中行的高度。