Click here to Skip to main content
15,860,943 members
Articles / Web Development / CSS

Maintain GridView Scroll Position and Header Inside UpdatePanel

Rate me:
Please Sign up or sign in to vote.
4.63/5 (53 votes)
18 Oct 2008CPOL3 min read 215K   5.2K   80   50
This article describes how to maintain the scroll position and freeze header at the time of postback inside an UpdatePanel.

LastTry.gif

Introduction

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. Ohhhh...no, 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 StateDescription

ajax.h1.jpg

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.

ajax.h2.jpg

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.

ajax.h3.jpg

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:

JavaScript
<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 the GridView. The Gridview should be placed inside an UpdatePanel and a Div control. The Div allow us to scroll the GridView.

ASP.NET
<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 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.

CSS
.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)


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

Blog : http://abhijitjana.net
Web Site : http://dailydotnettips.com
Twitter : @AbhijitJana
My Kinect Book : Kinect for Windows SDK Programming Guide

Comments and Discussions

 
Generalmaintain position scroll Gridview Pin
Le Minh3-Mar-19 16:41
Le Minh3-Mar-19 16:41 
GeneralMy vote of 5 Pin
Raj.rcr14-Mar-18 23:28
Raj.rcr14-Mar-18 23:28 
Questionthanks Pin
lida shoaee3-Jul-17 18:41
lida shoaee3-Jul-17 18:41 
QuestionHeaderFreez Pin
Member 37988227-Oct-15 6:33
Member 37988227-Oct-15 6:33 
QuestionTHis is not working for horizontal scroll bar Pin
Vikram Singh Rathaur20-Aug-15 20:02
Vikram Singh Rathaur20-Aug-15 20:02 
QuestionScrollbar Position Pin
Member 1155517825-Mar-15 6:46
Member 1155517825-Mar-15 6:46 
QuestionGridView Control Pin
ibrahim_540326-Feb-13 19:41
ibrahim_540326-Feb-13 19:41 
QuestionAbt Code Pin
AmitDhakre13-Jul-12 1:36
AmitDhakre13-Jul-12 1:36 
QuestionI am not using select button? Pin
shuvra24-Apr-12 5:19
shuvra24-Apr-12 5:19 
AnswerRe: I am not using select button? Pin
shuvra24-Apr-12 5:30
shuvra24-Apr-12 5:30 
QuestionFix header doesnt work for me BUT YOUR POST IS MY MISSING LINK. Thanks so much! Pin
donPEPOT3-Aug-11 23:35
donPEPOT3-Aug-11 23:35 
General5 is my Vote Pin
Hernan K. Cabrera29-Jul-11 22:53
Hernan K. Cabrera29-Jul-11 22:53 
GeneralMy vote of 5 Pin
Rashmi_Karnam17-May-11 0:42
Rashmi_Karnam17-May-11 0:42 
GeneralMy vote of 5 Pin
Sandesh M Patil15-Feb-11 0:59
Sandesh M Patil15-Feb-11 0:59 
Cool stuff
GeneralHeaderFreeze is NOT working in IE8, Mozilla and Chrome Pin
Anurag Gandhi3-Dec-09 3:46
professionalAnurag Gandhi3-Dec-09 3:46 
GeneralRe: HeaderFreeze is NOT working in IE8, Mozilla and Chrome Pin
Abhijit Jana3-Dec-09 21:40
professionalAbhijit Jana3-Dec-09 21:40 
GeneralRe: HeaderFreeze is NOT working in IE8, Mozilla and Chrome Pin
joshshrf4-Dec-09 6:29
joshshrf4-Dec-09 6:29 
GeneralRe: HeaderFreeze is NOT working in IE8, Mozilla and Chrome Pin
Abhi-now10-Mar-10 2:36
Abhi-now10-Mar-10 2:36 
GeneralRe: HeaderFreeze is NOT working in IE8, Mozilla and Chrome Pin
Abhijit Jana13-Jun-10 21:10
professionalAbhijit Jana13-Jun-10 21:10 
GeneralRe: HeaderFreeze is NOT working in IE8, Mozilla and Chrome Pin
KLKJ28-Apr-11 2:41
KLKJ28-Apr-11 2:41 
GeneralRe: HeaderFreeze is NOT working in IE8, Mozilla and Chrome Pin
donPEPOT3-Aug-11 23:18
donPEPOT3-Aug-11 23:18 
GeneralRe: HeaderFreeze is NOT working in IE8, Mozilla and Chrome Pin
Vince3rd24-Oct-11 22:52
Vince3rd24-Oct-11 22:52 
GeneralLost the Header Pin
lqluo19-Nov-09 22:44
lqluo19-Nov-09 22:44 
GeneralRe: Lost the Header Pin
Abhijit Jana19-Nov-09 22:48
professionalAbhijit Jana19-Nov-09 22:48 
GeneralRe: Lost the Header Pin
lqluo19-Nov-09 22:53
lqluo19-Nov-09 22:53 

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

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