如何滚动到GridView中选定行 [英] How to scroll to selected row in GridView

查看:570
本文介绍了如何滚动到GridView中选定行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在,我与ASP.net和C#的工作。

Now, I am working with ASP.net and C#.

我有每页= 20(20行)的GridView。

I have a GridView with PageSize = 20 (20 rows).

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

But , it can show only 10 rows and vertical scroll bar appeared.

我的问题是....结果
当回发发生时,跳转到网格顶行虽然我选择任何其他行。我想它滚动所选行。我怎么能做到这一点。请帮帮我。

My problem is ....
When postback occurs, it jump to top row of the grid Though I selected any other rows . I would like to scroll it to the selected row. How could I do this. Please help me.

与问候,

推荐答案

在你的页面指令。

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

另一种方法,使用该包装你的G​​ridView的DIV的scrollTop的方法:

Another way, use a scrollTop method of the DIV that wraps your GridView:

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);
}

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

This won't work for several reasons:

1),如果在GridView是NamingContainer控制里面,就像一个小组,因为在客户端的编号不会是客户端Id 。您需要使用唯一ID 德的控制,而不是

1) if the gridView is inside a NamingContainer control, like a Panel, because the Id on the client side won't be the ClientId. You need to use the UniqueId of teh control instead.

2)你不能相信行的高度来计算滚动位置。如果任何列中的文本换行到多行或任何行包含的东西高于风格,行的大小会有所不同。

2) you can't trust the row height to calculate the scroll position. If the text in any column wraps to more than one line, or any row contains something higher than the style, the size of the row will be different

3)不同的浏览器可以有不同的行为。你使用jQuery scrollTop的()滚动()功能更好。要使用它们,你必须使用 scrollTop的在客户端和设定值的 HiddenControl 可在服务器读取侧复位位置。你不能得到在浏览器中的行的高度,直到它们被在客户端呈现

3) different browsers can have different behaviors. You're better of using jQuery scrollTop() and scroll() functions. To use them, you must use scrollTop on client side and set the value of a HiddenControl that can be read on server side to reset the position. You can't get the height of the rows in the browser until they are rendered on client side.

这篇关于如何滚动到GridView中选定行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆