Click here to Skip to main content
Click here to Skip to main content

Maintain GridView Scroll Position and Header Inside UpdatePanel

, 18 Oct 2008
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.

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 State Description

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:

<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: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.

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

Share

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 : http://abhijitjana.net
Web Site : http://dailydotnettips.com
Twitter : @AbhijitJana
My Kinect Book : Kinect for Windows SDK Programming Guide
Follow on   Twitter

Comments and Discussions

 
GeneralGridView With Fixed Headers in Asp.Net Using C# and jQuery PinmemberBinu198520-Jun-14 23:56 
QuestionGridView Control Pinmemberibrahim_540326-Feb-13 19:41 
QuestionAbt Code PinmemberAmitDhakre13-Jul-12 1:36 
QuestionI am not using select button? Pinmembershuvra24-Apr-12 5:19 
AnswerRe: I am not using select button? Pinmembershuvra24-Apr-12 5:30 
QuestionFix header doesnt work for me BUT YOUR POST IS MY MISSING LINK. Thanks so much! PinmemberdonPEPOT3-Aug-11 23:35 
General5 is my Vote PinmemberHernan K. Cabrera29-Jul-11 22:53 
Work Perfectly...
 
Thanks a lot.
GeneralMy vote of 5 PinmemberRashmi_Karnam17-May-11 0:42 
GeneralMy vote of 5 PinmemberSChristmas15-Feb-11 0:59 
GeneralHeaderFreeze is NOT working in IE8, Mozilla and Chrome PinmemberAnurag Gandhi3-Dec-09 3:46 
GeneralRe: HeaderFreeze is NOT working in IE8, Mozilla and Chrome PinmvpAbhijit Jana3-Dec-09 21:40 
GeneralRe: HeaderFreeze is NOT working in IE8, Mozilla and Chrome Pinmemberjoshshrf4-Dec-09 6:29 
GeneralRe: HeaderFreeze is NOT working in IE8, Mozilla and Chrome PinmemberAbhi-now10-Mar-10 2:36 
GeneralRe: HeaderFreeze is NOT working in IE8, Mozilla and Chrome PinmvpAbhijit Jana13-Jun-10 21:10 
GeneralRe: HeaderFreeze is NOT working in IE8, Mozilla and Chrome Pinmemberkumargtl28-Apr-11 2:41 
GeneralRe: HeaderFreeze is NOT working in IE8, Mozilla and Chrome PinmemberdonPEPOT3-Aug-11 23:18 
GeneralRe: HeaderFreeze is NOT working in IE8, Mozilla and Chrome PinmemberVince3rd24-Oct-11 22:52 
GeneralLost the Header Pinmemberlqluo19-Nov-09 22:44 
GeneralRe: Lost the Header PinmvpAbhijit Jana19-Nov-09 22:48 
GeneralRe: Lost the Header Pinmemberlqluo19-Nov-09 22:53 
GeneralRe: Lost the Header PinmvpAbhijit Jana19-Nov-09 23:01 
GeneralRe: Lost the Header [modified] Pinmemberlqluo22-Nov-09 20:49 
GeneralVery useful code PinmemberSivaPrakasamDotNet28-Oct-09 22:57 
GeneralRe: Very useful code PinmvpAbhijit Jana7-Nov-09 1:37 
GeneralThanks Pinmembermilindchavan1216-Oct-09 1:44 

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 | Mobile
Web02 | 2.8.140827.1 | Last Updated 18 Oct 2008
Article Copyright 2008 by Abhijit Jana
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid