Click here to Skip to main content
11,412,125 members (65,011 online)
Click here to Skip to main content

Maintain GridView Scroll Position and Header Inside UpdatePanel

, 18 Oct 2008 CPOL
Rate this:
Please Sign up or sign in to vote.
This article describes how to maintain the scroll position and freeze header at the time of postback inside an UpdatePanel.



For most kind of web development, we mostly need to show data inside a GridView. Suppose we need to show 500 records inside a GridView and when the user selects a record, the details are displayed in the bottom or any where else in the page. Now for preventing postback for such operations, generally we use the AJAX UpdatePanel and put the GridView inside the UpdatePanel. This will resolve the problem of postback, but then what happens? When you want to check record 100, you just scroll down the records and select the record for checking the details, and now you get the records inside your details area, but look at the GridView scroll position., it should not be happening: it is back at the top of the records. We have to now solve this problem at the time of partial postback of the page.

Problem Statement

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

Problem State Description


I have a GridView with a scroll bar where I have 8 records. I can see 5 records at a time. For getting the other records, I have to scroll down. I just select the first records and get the details in the details area.


No I want to see some other records. Let's say the 8th record. I just scroll down the scroll bar but I don't select a record. Now check the scroll bar position and the student detail.


Now I select the 8th record. You will see in the details section that the detail of that records is showing, but check the scroll bar position.

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

The Solution

When we are using an UpdatePanel, we need more control on the UpdatePanel to solve this problem. For that, we need to use the Sys.WebForms.PageRequestManager class. This is used to manage the partial-page update by using client-side script. We do not need to create an object of the PageRequestManager class directly. For this solution, I have used two events. beginRequest is raised before processing of the asynchronous postback starts. Here I have just retrieved the Div position of the scroll and stored it in a variable. endRequest is raised after an asynchronous postback is finished and control has been returned to the browser. Here I have just assigned the retrieved 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 the postback of the GridView inside the UpdatePanel:

<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 
//Get The Div Scroll Position
Function BeginRequestHandler(sender, args) 
var m = document.getElementById('divGrid');
//Set The Div Scroll Position
function EndRequestHandler(sender, args)
var m = document.getElementById('divGrid');
m.scrollTop = scrollTop;

Following is the code for the GridView. The Gridview should be placed inside an UpdatePanel and a Div control. The Div allow us to scroll the GridView.

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<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"
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<HeaderStyle CssClass="HeaderFreez" />
<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" />
<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:SqlDataSource ID="SqlDataSource1" runat="server" 
ConnectionString="<%$ ConnectionStrings:DotNetRnDDBConnectionString %>"
SelectCommand="SELECT * FROM [StudDetails]"></asp:SqlDataSource>

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

   position:relative ;
   z-index: 10;



  • Written on 17-Oct-2008.


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


About the Author

Abhijit Jana
Technical Lead
India India
.NET Consultant | Former Microsoft MVP - ASP.NET | CodeProject MVP, Mentor, Insiders| Technology Evangelist | Author | Speaker | Geek | Blogger | Husband

Blog :
Web Site :
Twitter : @AbhijitJana
My Kinect Book : Kinect for Windows SDK Programming Guide
Follow on   Twitter

Comments and Discussions

QuestionScrollbar Position PinmemberMember 1155517825-Mar-15 7:46 
GeneralGridView With Fixed Headers in Asp.Net Using C# and jQuery PinmemberBinu198521-Jun-14 0:56 
QuestionGridView Control Pinmemberibrahim_540326-Feb-13 20:41 
QuestionAbt Code PinmemberAmitDhakre13-Jul-12 2:36 
QuestionI am not using select button? Pinmembershuvra24-Apr-12 6:19 
AnswerRe: I am not using select button? Pinmembershuvra24-Apr-12 6:30 
QuestionFix header doesnt work for me BUT YOUR POST IS MY MISSING LINK. Thanks so much! PinmemberdonPEPOT4-Aug-11 0:35 
General5 is my Vote PinmemberHernan K. Cabrera29-Jul-11 23:53 
GeneralMy vote of 5 PinmemberRashmi_Karnam17-May-11 1:42 
GeneralMy vote of 5 PinmemberSChristmas15-Feb-11 1:59 
GeneralHeaderFreeze is NOT working in IE8, Mozilla and Chrome PinmemberAnurag Gandhi3-Dec-09 4:46 
GeneralRe: HeaderFreeze is NOT working in IE8, Mozilla and Chrome PinmvpAbhijit Jana3-Dec-09 22:40 
GeneralRe: HeaderFreeze is NOT working in IE8, Mozilla and Chrome Pinmemberjoshshrf4-Dec-09 7:29 
GeneralRe: HeaderFreeze is NOT working in IE8, Mozilla and Chrome PinmemberAbhi-now10-Mar-10 3:36 
GeneralRe: HeaderFreeze is NOT working in IE8, Mozilla and Chrome PinmvpAbhijit Jana13-Jun-10 22:10 
GeneralRe: HeaderFreeze is NOT working in IE8, Mozilla and Chrome Pinmemberkumargtl28-Apr-11 3:41 
GeneralRe: HeaderFreeze is NOT working in IE8, Mozilla and Chrome PinmemberdonPEPOT4-Aug-11 0:18 
GeneralRe: HeaderFreeze is NOT working in IE8, Mozilla and Chrome PinmemberVince3rd24-Oct-11 23:52 
GeneralLost the Header Pinmemberlqluo19-Nov-09 23:44 
GeneralRe: Lost the Header PinmvpAbhijit Jana19-Nov-09 23:48 
GeneralRe: Lost the Header Pinmemberlqluo19-Nov-09 23:53 
GeneralRe: Lost the Header PinmvpAbhijit Jana20-Nov-09 0:01 
GeneralRe: Lost the Header [modified] Pinmemberlqluo22-Nov-09 21:49 
GeneralVery useful code PinmemberSivaPrakasamDotNet28-Oct-09 23:57 
GeneralRe: Very useful code PinmvpAbhijit Jana7-Nov-09 2:37 
GeneralThanks Pinmembermilindchavan1216-Oct-09 2:44 
GeneralRe: Thanks PinmvpAbhijit Jana16-Oct-09 3:09 
GeneralGreat info PinmemberAbhishek Sur13-Sep-09 10:04 
GeneralRe: Great info PinmvpAbhijit Jana16-Oct-09 3:12 
GeneralThanks for share this solution. Pinmemberkorbe9-Sep-09 4:56 
GeneralRe: Thanks for share this solution. PinmvpAbhijit Jana9-Sep-09 4:58 
GeneralGreat, but... Pinmemberperle12-Aug-09 6:51 
GeneralNot work in Firefox Pinmemberchiragvm27-Jul-09 20:46 
GeneralThanks Alottttt............ Pinmemberravirajk2112-Jun-09 6:46 
GeneralRe: Thanks Alottttt............ PinmvpAbhijit Jana14-Jun-09 9:14 
GeneralBrilliant ! Pinmembermarc charmois17-Apr-09 13:23 
GeneralRe: Brilliant ! PinmvpAbhijit Jana19-Apr-09 2:53 
GeneralRe: Brilliant ! [modified] Pinmembermarc charmois19-Apr-09 4:43 
GeneralThanks for such a nice article PinmemberSomali3-Mar-09 23:47 
GeneralRe: Thanks for such a nice article PinmvpAbhijit Jana4-Mar-09 0:19 
GeneralBrilliant PinmemberJSMui3-Mar-09 13:43 
GeneralRe: Brilliant PinmvpAbhijit Jana3-Mar-09 19:30 
GeneralGreat Job PinmemberBrij19-Oct-08 3:15 
GeneralRe: Great Job PinmemberAbhijit Jana19-Oct-08 3:42 
GeneralRe: Great Job Pinmemberjames_carter20-Oct-08 23:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150414.5 | Last Updated 18 Oct 2008
Article Copyright 2008 by Abhijit Jana
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid