ASP.NET如何在DataGrid回发后维持内滚动位置 [英] ASP.NET How to Maintain Scroll Position within a Datagrid After Postback

查看:138
本文介绍了ASP.NET如何在DataGrid回发后维持内滚动位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含一个DataGrid的aspx页面。该数据网格在$ C $通过SQL查询填充C-后面,从1到1000行有任何地方。

I have an aspx page containing a datagrid. This datagrid is populated in the code-behind via a SQL query and can have anywhere from 1 to 1000 rows.

用户可以点击每行和编辑数据为该行。一旦他们节省,然而,数据网格向后滚动到顶部,无论他们在哪里。我想保持在DataGrid中滚动位置,使用户可以方便地编辑下一行。我有我的aspx页面上以下内容:

Users can click on each row and edit the data for that row. Once they save, however, the datagrid scrolls back to the top, no matter where they were. I would like to maintain the scroll position within the datagrid so the user can easily edit the next row. I have the following on my aspx page:

<script src="common.js" type="text/javascript"></script>

    <script type = "text/javascript">

        function setScroll(val) {
            document.getElementById("hidscrollPos").value = val.scrollTop;
        }

        function scrollTo(what) {
            document.getElementById(what).scrollTop =
        document.getElementById("hidscrollPos").value;
        }
</script>

<body MS_POSITIONING="GridLayout" onload="javascript:scrollTo('divDataGrid')">
<form id="form1" method="post" runat="server">
<asp:Panel ID="Panel4" style="left: 0px; position: absolute; top: 354px" runat="server">
<div id="divDataGrid" style="overflow:scroll;" onscroll="javascript:setScroll(this);">
    <asp:DataGrid ID="grdItem" runat="server" AllowSorting="True"
        AutoGenerateColumns="False" style="position: absolute; top: 50px" 
        CssClass="grdGrid" Font-Size="X-Small" GridLines="None" Width="97%" 
        Height="4px" CellPadding="1">


</asp:DataGrid>
</div>
</asp:Panel>
<input type="hidden" id="hidscrollPos" name="scrollPos" value="0" runat="server" />
</form>
</body>

在DataGrid中我已经定义了11 ASP:TemplateColumns,我认为(希望?)不相关的这一问题。我可以在code肯定会增加,如果需要的话。

Within the datagrid I have defined 11 asp:TemplateColumns that I think (hope?) are not relevant to this issue. I can certainly add in that code if need be.

我的.css文件有这样的grdGrid:

My .css file has this for grdGrid:

.grdGrid
{
FONT-SIZE: 12px;
FONT-FAMILY: Verdana;
BACKGROUND-COLOR: white
}

我使用Visual Studio 2008和IE9。用户将使用IE8或者IE9或

I am using Visual Studio 2008 and IE9. Users will be using either IE8 or IE9.

任何帮助将是非常美联社preciated!

Any help would be much appreciated!

推荐答案

打我的头撞在墙上了几个小时后,我终于想通了。

After beating my head against the wall for hours, I finally figured it out.

上面贴的code是正确的和完美的作品。问题是,divDataGrid载另一个div内,并且该div溢出有:自动设置。因此,滚动条,我看到的是从DIV,不divDataGrid或grdItem。

The code posted above is correct and works perfectly. The problem was that divDataGrid was contained within another div, and THAT div had overflow:auto set. So the scroll bar I was seeing was from that div, not divDataGrid or grdItem.

在我删除了溢出:从previous格都自动设置开始工作。

Once I removed the overflow:auto setting from the previous div everything started working.

这篇关于ASP.NET如何在DataGrid回发后维持内滚动位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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