惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Y
Y Combinator Blog
博客园 - 司徒正美
TaoSecurity Blog
TaoSecurity Blog
Martin Fowler
Martin Fowler
T
Threat Research - Cisco Blogs
Blog — PlanetScale
Blog — PlanetScale
S
Secure Thoughts
博客园 - 三生石上(FineUI控件)
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
K
Kaspersky official blog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Cisco Talos Blog
Cisco Talos Blog
H
Help Net Security
博客园 - 叶小钗
爱范儿
爱范儿
GbyAI
GbyAI
I
Intezer
M
MIT News - Artificial intelligence
Latest news
Latest news
Schneier on Security
Schneier on Security
T
Tor Project blog
Simon Willison's Weblog
Simon Willison's Weblog
I
InfoQ
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
C
CXSECURITY Database RSS Feed - CXSecurity.com
罗磊的独立博客
N
News and Events Feed by Topic
T
The Blog of Author Tim Ferriss
V2EX - 技术
V2EX - 技术
B
Blog
T
Tailwind CSS Blog
N
Netflix TechBlog - Medium
Security Latest
Security Latest
V
V2EX
F
Fortinet All Blogs
Forbes - Security
Forbes - Security
Application and Cybersecurity Blog
Application and Cybersecurity Blog
The Hacker News
The Hacker News
Scott Helme
Scott Helme
P
Privacy International News Feed
P
Palo Alto Networks Blog
H
Heimdal Security Blog
C
Cisco Blogs
T
The Exploit Database - CXSecurity.com
博客园 - Franky
酷 壳 – CoolShell
酷 壳 – CoolShell
G
Google Developers Blog
W
WeLiveSecurity
L
LINUX DO - 最新话题

博客园 - 汗水房

LINQ to sQL:业务层多个Class Library,or 只用一个? window.open() 第2次进入不执行 page_load 中的代码 转载:Ajax中“Sys未定义”错误的解决方法 - 汗水房 - 博客园 CodeProject: 自定义控件实现Radiobox选择Gridview一行 翻译:LINQ to SQL N-层智能客户端 - 第一部分 构建中间层 插件开发时代 Adobe的RIA平台 使用Flash的数据可视化开发(Flash Data Visualization) 转载自Microsoft:每个开发人员现在应该下载的十种必备工具 转载:Google API简介 转载:移动widget玩玩可以 LINQ与Entity Framework以及其它新特性 转载LINQ to SQL(LINQ2SQL) vs. ADO.NET Entity Framework(ADOEF)-ccBoy版 LINQ to SQL建立独立的BLL实体层 LINQ in Action笔记 Architecting Your Data Access Layer with the Entity Framework | Architecting with .NET 转摘好文:Barry Gervin's Software Architecture Perspectives : The Entity Framework vs. The Data Access Layer (Part 1: The EF as a DAL) LinQ to SQL系统的层次架构设计 Linq-To-Sql Business Layer - 汗水房
MicrosoftMiles: AJAX Updatepanel中保持Gridview的滚动位置
汗水房 · 2009-09-11 · via 博客园 - 汗水房

一开始由于我的GridView的名字有大写字母,一直不行。后来看了comment的一个人说把“postbackElement.id.toLowerCase().indexOf('grdorders') > -1”行去掉就可以,我去掉了果然就行了。然后回过头来,才发现这一行中是要小写字母比较的。其实这一行不用去掉。

Maintaining GridView Scroll Position in an ASP.NET AJAX UpdatePanel

Sometimes, it is inappropriate to use the paging feature of the ASP.NET GridView.  Instead, a scrolling grid is more applicable and enclosing the GridView in a <div> tag with the overflow style applied ensures that the over-sized element is clipped and that scroll bars are displayed.

<asp:UpdatePanel ID="updateGrid" runat="server" UpdateMode="Conditional">
<ContentTemplate>

<input type="hidden" id="hdnScrollTop" runat="server" value="0" />

<div id="divScroll" style="width:350px;height:200px; overflow-x:hidden; overflow-y:scroll;" onscroll="$get('hdnScrollTop').value = this.scrollTop;">
<asp:gridview id="grdOrders" runat="server" width="95%" datasourceid="objDataSource" cellpadding="3" GridLines="Horizontal">
<Columns>
<asp:CommandField ShowSelectButton="True" />
</Columns>
</asp:gridview>
</div>
</ContentTemplate>
</asp:UpdatePanel>

It is slightly more complex to persist the scroll position during an syschronous postback using ASP.NET AJAX.  It's necessary to store the scrollTop property of the div tag in a hidden field using the client side onscroll event.  Note the use of the Sys.UI.DomElement $get method, which is a shortcut to the getElementById method. It's also important that the hidden input element has the runat="server" attribute so the element can be accessed during the pageLoaded function.

<asp:ScriptManager ID="scriptManager" runat="server" EnablePartialRendering="True" />
<script type="text/javascript" language="javascript">
    var prm = Sys.WebForms.PageRequestManager.getInstance();
    prm.add_pageLoaded(pageLoaded);
    prm.add_beginRequest(beginRequest);
var postbackElement;

function beginRequest(sender, args) {
        postbackElement = args.get_postBackElement();
    }
function pageLoaded(sender, args) {
var updatedPanels = args.get_panelsUpdated();
if (typeof(postbackElement) == "undefined") {
return;
        }
if (postbackElement.id.toLowerCase().indexOf('grdorders') > -1) {
            $get("divScroll").scrollTop = $get("hdnScrollTop").value;
        }
     }
</script>

In order that the scroll position of the div can be reset after a postback, it is first necessary to add a reference to the PageRequestManager.  To use the PageRequestManager class in client script, you must first have a ScriptManager server control on the page. To access the PageRequestManager class, you must have the EnablePartialRendering set to true (the default) on the ScriptManager control. When EnablePartialRendering is set to true, the MicrosoftAjaxWebForms.js file that contains the PageRequestManager class is included as a script resource for the page.  Once you have the current instance of the PageRequestManager, you can access all of its methods, properties, and events such as beginRequest and pageLoaded.

The beginRequest event is raised before the processing of an asynchronous postback begins and the postback is sent to the server.  Here we get a reference to the element that has raised the postback. 

The pageLoaded event is raised after all content on the page is refreshed.  The function initially determines if the page has been posted back by checking the typeof the postbackElement.  If the postback was raised by a our GridView, the scrollTop property of our div tag is set to the value stored in the hidden field.

Client Page Life-cycle Events are also integral to Customizing Error Handling in Partial-Page Updates.

Posted by Miles Ashton at 9:47 PM

Labels: AJAX, ASP.NET

11 comments:
bebandit said...

Thanks! This helped me with adding Omniture (Site Tracking Tags) to my AJAX pages. Everytime the datagrid gets called it updates the omniture tags. Great post!

6:30 AM
Joe said...

I'm looking for siome code that will handle a scenario where an ajx page has a number of asp panels. The user begins in panel1 and begins looking at material, even paging while in panel 1.
At some point the user clicks on a link that exposes panel2. The user does some things in panel2 and when finised is returned to panel1.
I need to return the user to the original scroll position when leaving for panel2.

3:17 PM
Travis said...

I had to make some modifications to get this to work with a masterpage/contentplaceholder.
my page elements were being renamed to: ctl00_ContentPlaceHolder1_hdnScrollTop as an example.
I found this out by inspecting the page source.
Once I renamed all the elements referenced in the code, it now works :)

3:08 PM
Anonymous said...

Thanks, this was very helpful. I had to comment out the line:
if (postbackElement.id.toLowerCase().indexOf('grdorders') > -1) {
and put this line in a try / catch:
$get("divScroll").scrollTop = $get("hdnScrollTop").value;
Otherwise, it was great. Thanks!

1:23 AM
Anonymous said...

Wow - that is a beautiful thing!! Worked a treat for me, after I read the comment above about the masterpages.
cheers

12:07 PM
Anonymous said...

Travis, that will work sometimes. the ctl00_ContentPlaceHolder1_ is generated at runtime. therefore you cannoy guarantee that it will be the same everytime.

5:54 AM
Anonymous said...

This was a great post.
It worked out of the box for a non-masterpage webform.
With a masterpage I also had to change the ids to
ctl00_ContentPlaceHolder1_hdnScrollTop in the pageLoaded function and the onscroll call.
I also went with the try catch as recommended.
function pageLoaded(sender, args) {
try
{
$get("divScroll").scrollTop = $get("ctl00_ContentPlaceHolder1_hdnScrollTop").value;
}
catch(err)
{
}
}
THANK YOU!

5:56 AM
Anonymous said...

Thanks for the great post. Just wondering if there is a way to make the div visible/invisble from the code behind.

2:33 AM
Anonymous said...

Great post.
Just wondering how can I make my div visble/invisble from the code behind.

2:34 AM
l3dx said...

Thanks a lot, this helped me a lot!

5:38 PM
Hardysmith said...

Absolutely most helpful. I used your technique but rather than using a hidden form field I stored the scrolltop value in a javascript object. Brilliant.

12:19 AM

Post a Comment

MicrosoftMiles: Maintaining GridView Scroll Position in an ASP.NET AJAX UpdatePanel