Maintain page in Gridview on postback inside an Update Panel.

Dukster131 used Ask the Experts™
I have a gridview that has several pages in it.  The problem is that if I am on page 4 and then do an edit and cancel, it goes back to page 1.  I have tried doing some javascript to maintain the position and the maintainscrollpositiononPostback, but nothing seems to work.

I used the following javascript
<script type="text/javascript">
        $(document).ready(function () {
            var xPos, yPos;
            var prm = Sys.WebForms.PageRequestManager.getInstance();

            function BeginRequestHandler(sender, args) {
                //Get X and Y positions of scrollbar before the partical postback
                xPos = $get('scrollWork').scrollLeft;
                yPos = $get('scrollWork').scrollTop;


            function EndRequestHandler(sender, args) {
                //Set X and Y positions back to the scrollbar after the partial postack
                $get('scrollWork').scrollLeft = xPos;
                $get('scrollWork').scrollTop = yPos;


My page looks like this:

<asp:UpdatePanel runat="server" ID="UpdatePanel2"
    <div id="scrollWork" > 
<asp:GridView ID="WorkOrderGrid" runat="server" AutoGenerateColumns="False"
        AllowPaging="True" AllowSorting="True" DataKeyNames="Task_id"
        OnSelectedIndexChanged="WorkOrderGrid_SelectedIndexChanged" Width="1042px"
            onrowdatabound="WorkOrderGrid_RowDataBound" >
 <asp:BoundField DataField="Task_id" HeaderText = "No." SortExpression="Task_id" />
 <asp:BoundField DataField="CatName" HeaderText="Problem Area" SortExpression="CatName" />
     <asp:TemplateField HeaderText="Problem">
             <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("TaskDescription") %>'></asp:TextBox>
             <asp:Label ID="lblDescription" runat="server" Text='<%# Bind("TaskDescription") %>'></asp:Label>
 <asp:BoundField DataField= "TaskRequestDate" HeaderText = "Request Date" SortExpression="TaskRequestDate" />
 <asp:BoundField DataField = "TaskRequestedBy" HeaderText = "Requestor" SortExpression="TaskRequestedBy" />
 <asp:BoundField DataField="RoomNo" HeaderText="Room" SortExpression="RoomNo" />
 <asp:BoundField DataField="Phone" HeaderText="Phone"/>
 <asp:BoundField DataField = "TaskAssignedTo" HeaderText = "Assigned To" SortExpression="TaskAssignedTo" />
 <asp:BoundField DataField = "Status_Description" HeaderText = "Status" SortExpression="Status_Description" />
     <asp:ButtonField CommandName="Select" Text="Select" />
    <FooterStyle CssClass="GridFooter" />
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Technical Architect
Don't you have to store the position while leaving the page and restore them when you come back from page 4 ?


I am never leaving the page - just making gridview visible and invisible and Panels visible and invisible.


I tried saving the newPageIndex in a session variable and using that when I cancelled the details view.  Then when I got the data I used

if (Session["Page"] != null)
                        WorkOrderGrid.PageIndex = Convert.ToInt32(Session["Page"]);
                        WorkOrderGrid.PageIndex = newPageIndex;

That worked.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial