Click here to Skip to main content
6,595,444 members and growing! (18,695 online)
Email Password   helpLost your password?
Web Development » ASP.NET » General     Intermediate License: The Code Project Open License (CPOL)

Maintain GridView Scroll Position and Header Inside Update Panel

By Abhijit Jana

This article describes how to maintain the scroll position and Freezing header at the time of postback inside Update panel.
C#, Javascript, CSS.NET 2.0, ASP.NET, Ajax, Architect, Dev, Design
Posted:18 Oct 2008
Views:25,163
Bookmarked:40 times
Unedited contribution
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
38 votes for this article.
Popularity: 6.85 Rating: 4.34 out of 5
2 votes, 5.3%
1
1 vote, 2.6%
2
2 votes, 5.3%
3
1 vote, 2.6%
4
32 votes, 84.2%
5

LastTry.gif

Introduction

For any kind of Web development, the maximum time we need to show data inside the grid view. suppose we are going to show 500 records inside a GridView and when user select any one the record details will going to display in the bottom or any where of that page. Now for preventing postback for such kind of operation, generally we are used AJAX Update Panel and put the GridView to inside of Update panel, This will resolve the problem of Postback, but now see what happen , when you want to check records no 100 , you just scroll the records and select that records for checking the details, now you are getting the records inside your details area but look at the Grid view scroll position . Ohhhh...no, It should not be happened its back to the top of records. So we have to solve the problem at the time of partial postback of page.

Problem Statement

Here I am going to describe the actual problem in detail:

Problem State Description

ajax.h1.jpg

I have a grid view with a scroll bar, where I am having 8 of records. I can see 5 records at a time, for getting others records I have to scroll. I have just select the first records and getting the details in details area.

ajax.h2.jpg

No I want to see some other Records Let 8th Records, I have just scroll the scroll bar but I didn't select records. Now check the scroll bar position and Student detail.

ajax.h3.jpg

Now, I have just select the 8th Records, and See , in details section details of that records is coming , but Check the scroll bar position .

This is quite frustrating scenario for the user . Now we have a good solution for that using handling the partial post back of the page.

The Solution

When we are using UpdatePanel, we need some more control on Update Panel to solve this problem. For that we need to use Sys.WebForms.PageRequestManager class. This is used to manage the partial-page update by using client script. We do not need to create the object of PageRequestManager class Directly. For this solution , I have used two events beginRequest which raised before processing of an asynchronous postback starts, Here I have just retrieve the Div position of scroll and store it in a variable and endRequest raised after an asynchronous postback is finished and control has been returned to the browser, here I have just assigned the retrieve scroll position to the div again. For More Info Click Here.

Using The Code

I have written the following JavaScript code for handling the partial postback. which handles the Div scroll position during postback of GridView inside the update panel:

<script language="javascript" type="text/javascript">
// This Script is used to maintain Grid Scroll on Partial Postback
var scrollTop;
//Register Begin Request and End Request 
Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(BeginRequestHandler);
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
//Get The Div Scroll Position
Function BeginRequestHandler(sender, args) 
{
var m = document.getElementById('divGrid');
scrollTop=m.scrollTop;
}
//Set The Div Scroll Position
function EndRequestHandler(sender, args)
{
var m = document.getElementById('divGrid');
m.scrollTop = scrollTop;
} 
</script>

Following is the code for GridView. Gridview should be placed inside a Update panel and Div control. Div allow us to scroll the gridview.

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<div id="divGrid" style="overflow: auto; height: 130px"> 
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4"
DataSourceID="SqlDataSource1" ForeColor="#333333" GridLines="None" Width="235px"
OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<HeaderStyle CssClass="HeaderFreez" />
<Columns>
<asp:BoundField DataField="Roll" HeaderText="Roll" SortExpression="Roll" />
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
<asp:BoundField DataField="Langauge" HeaderText="Language" SortExpression="Langauge" />
<asp:CommandField ShowSelectButton="True" />
</Columns>
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#999999" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
</asp:GridView>
</div>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
ConnectionString="<%$ ConnectionStrings:DotNetRnDDBConnectionString %>"
SelectCommand="SELECT * FROM [StudDetails]"></asp:SqlDataSource>
</ContentTemplate>
</asp:UpdatePanel>

Here is the CSS code which is used to freeze the Header of GridView. Just add this class as a HeaderStyle class of Gridview.This will freeze the Grids Header. We can use it in all other cases also where we need to freeze the header.

.HeaderFreez
{
   position:relative ;
   top:expression(this.offsetParent.scrollTop);
   z-index: 10;
}

Reference

History

  • Written on 17-Oct-2008

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Abhijit Jana


Member
Abhijit has done Master Degree in Computer Application from Heritage Institute of Technology (HIT-K) ,Kolkata, West Bengal, India . He is an interested, committed, creative Software professional having more than 2.8 years of solid experience in web-based and windows based solutions in Microsoft Technologies using .NET 2.0, .NET 3.0 , .NET 3.5, ASP.NET 2.0, ASP.NET 3.5 C# 2.0, AJAX, Silverlight, Web Services, MS SQL Server 2005, Exchange Server, Active Directory, and Dot Net Nuke (DNN),Win Forms, WinServices, WSS (Windows Sharepoint Server 3.0 ), WPF, WWF. He is also an MCP (Microsoft Certified Professional) and MCTS (Microsoft Certified Technology Specialist) on Web Development. He has good knowledge of Object Oriented Programming, 3-Tier Architecture and Design Patterns as well as good command over IIS (IIS 5.1,IIS 6.0, IIS 7.0) and deployment of Application on Live Production Environment . His hobbies, listing to music and Developing Own small Tools Utilities and Knowledge sharing.


Awards
CodeProject MVP 2009
Prize winner "Best ASP.NET article of Sep 2009
Prize winner "Best ASP.NET article of July 2009
Prize winner "Best ASP.NET article of June 2009"
Prize winner "Best ASP.NET article of January 2009"
Prize winner "Best ASP.NET article of November 2008"

Prize winner "Best ASP.NET article of October 2008"

Abhijit's CodeProject Guru :

Occupation: Software Developer
Location: India India

Other popular ASP.NET articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 22 of 22 (Total in Forum: 22) (Refresh)FirstPrevNext
GeneralVery useful code PinmemberSivaPrakasamDotNet23:57 28 Oct '09  
GeneralRe: Very useful code PinmvpAbhijit Jana2:37 7 Nov '09  
GeneralThanks Pinmembermilindchavan122:44 16 Oct '09  
GeneralRe: Thanks PinmvpAbhijit Jana3:09 16 Oct '09  
GeneralGreat info PinassociateAbhishek Sur10:04 13 Sep '09  
GeneralRe: Great info PinmvpAbhijit Jana3:12 16 Oct '09  
GeneralThanks for share this solution. Pinmemberkorbe4:56 9 Sep '09  
GeneralRe: Thanks for share this solution. PinmvpAbhijit Jana4:58 9 Sep '09  
GeneralGreat, but... Pinmemberperle16:51 2 Aug '09  
GeneralNot work in Firefox Pinmemberchiragvm20:46 27 Jul '09  
GeneralThanks Alottttt............ Pinmemberravirajk216:46 12 Jun '09  
GeneralRe: Thanks Alottttt............ PinmvpAbhijit Jana9:14 14 Jun '09  
GeneralBrilliant ! Pinmembermarc charmois13:23 17 Apr '09  
GeneralRe: Brilliant ! PinmvpAbhijit Jana2:53 19 Apr '09  
GeneralRe: Brilliant ! [modified] Pinmembermarc charmois4:43 19 Apr '09  
GeneralThanks for such a nice article PinmemberSomali23:47 3 Mar '09  
GeneralRe: Thanks for such a nice article PinmvpAbhijit Jana0:19 4 Mar '09  
GeneralBrilliant PinmemberJSMui13:43 3 Mar '09  
GeneralRe: Brilliant PinmvpAbhijit Jana19:30 3 Mar '09  
GeneralGreat Job PinmemberBrij3:15 19 Oct '08  
GeneralRe: Great Job PinmemberAbhijit Jana3:42 19 Oct '08  
GeneralRe: Great Job Pinmemberjames_carter23:29 20 Oct '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 18 Oct 2008
Editor: Deeksha Shenoy
Copyright 2008 by Abhijit Jana
Everything else Copyright © CodeProject, 1999-2009
Web21 | Advertise on the Code Project