• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 409
  • Last Modified:

How do I maintain my gridview controls State when I page back to it from another page ? Revisited

I thought that I had this solved but it is still an issue for me.
I have a gridview on page 1 and when a user filters the data there and goes to a particular page in the data if they then go to another page, page 2 and then return to page 1 through a link to page 1 they are returned to the beginning  of the dataset and not to the last page they were on.

I tried saving the page number, filter expression, and dropdown value for the expression in sessino state vars and then restoring then when the page IS posted back. But this doesn't work.

Is there anything else that I am overlooking ?

Thanks for any insights
In my page load event
  Restoring from my session 
       If DropDownList9.SelectedIndex = 0 Then
                If Session("holdDDL") > 0 Then
                    DropDownList9.SelectedIndex = Session("holdDDL")
                    ObjectDataSource4.FilterExpression = "assignee = {0}"
                    'Session("holdDDL") = 0
                    setDat()
                Else
                    setDat()
                End If
            Else
                ObjectDataSource4.FilterExpression = "assignee = {0}"
                'setDat()
                'GridView1.DataSourceID = "ObjectDataSource4"
                'GridView1.DataBind()
            End If
 LOADING my session vars here
 
     Protected Sub Page_Unload(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Unload
        Session("holdPgNo") = GridView1.PageIndex
        Session("holdFltr") = "assignee={0}"
        Session("HoldDDL") = DropDownList9.SelectedIndex
        Session("OBJDS") = ObjectDataSource4
    End Sub

Open in new window

0
gggoddard
Asked:
gggoddard
  • 3
  • 2
1 Solution
 
wht1986Commented:
We use this method a lot in our web apps, You have basically the same thing that we do, on page load check for session store to exist. if it does bind the grid and set the saved data
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                if (Session["somekey1"] != null)
                {
                    BindMyGrid((string)Session["somekey1"], (int)Session["somekey2"], (int)Session["somekey3"]);
                }
            }
            else
            {
                BindMyGrid("", -1, -1);
            }
        }
 
        private void BindMyGrid(string filter, int pageIndex, int selectedIndex)
        {
            MyDataTableAdpter adpt = new MyDataTableAdpter();
            this.GridView1.DataSource = adpt.Select(filter);
            this.GridView1.PageIndex = pageIndex;
            this.GridView1.SelectedIndex = selectedIndex;
            this.GridView1.DataBind();
        }

Open in new window

0
 
gggoddardAuthor Commented:
You are performing your code when it's not a post back.
I need to restore from the session vars when I am linking back to the page from another page .
So I need to place my restore statements outside of a not a post back  test , right ?
 
0
 
wht1986Commented:
no, Page_Load happens everytime the page loads. This includes postbacks plus the first time you visit the page.  The statement !this.IsPostaback determines if its the first time visiting the page or not.  

So the logic works like this.
Page1 Load Event 
   IsPostback : Yes -> normal postback processing do nothing special, ignore whats in session
   IsPostback: No
           Session store is null -> this means 1st time ever visited, show the default grid i want display
           Session store is NOT null -> this means we are coming back to this page from another page so we should set the filters
 
Unload Event: Set the grid data to session

Open in new window

0
 
gggoddardAuthor Commented:
Ok,  thanks again.
0
 
wht1986Commented:
I had already started coding an example for you so i am posting it here.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="Web_Form.WebForm1" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" DataSourceID="SqlDataSource2" DataTextField="CodeTypeName" DataValueField="CodeTypeId" ondatabound="DropDownList1_DataBound">
        </asp:DropDownList>
        <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="~/WebForm2.aspx">Go to page 2</asp:HyperLink>
        <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False" DataKeyNames="CodeID" DataSourceID="SqlDataSource1" PageSize="5">
            <Columns>
                <asp:BoundField DataField="CodeID" HeaderText="CodeID" InsertVisible="False" ReadOnly="True" SortExpression="CodeID" />
                <asp:BoundField DataField="CodeType" HeaderText="CodeType" SortExpression="CodeType" />
                <asp:BoundField DataField="Active" HeaderText="Active" SortExpression="Active" />
                <asp:BoundField DataField="CurrentValue" HeaderText="CurrentValue" SortExpression="CurrentValue" />
                <asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" />
                <asp:BoundField DataField="OrigTable" HeaderText="OrigTable" SortExpression="OrigTable" />
                <asp:BoundField DataField="NewTable" HeaderText="NewTable" SortExpression="NewTable" />
                <asp:BoundField DataField="OptionId" HeaderText="OptionId" SortExpression="OptionId" />
                <asp:BoundField DataField="Source" HeaderText="Source" SortExpression="Source" />
            </Columns>
        </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:PM_ReWriteConnectionString %>" SelectCommand="SELECT * FROM [CodeLookup]"></asp:SqlDataSource>
        <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:PM_ReWriteConnectionString %>" SelectCommand="SELECT * FROM [CodeType]"></asp:SqlDataSource>
    
    </div>
    </form>
</body>
</html>
===========================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
 
namespace Web_Form
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if ((!this.IsPostBack) && (Session["PageIndex"] != null))
            {
                this.GridView1.PageIndex = (int)Session["PageIndex"];
                this.GridView1.SelectedIndex = (int)Session["SelectedIndex"];
            }
        }
 
        protected void Page_Unload(object sender, EventArgs e)
        {
            Session["PageIndex"] = this.GridView1.PageIndex;
            Session["SelectedIndex"] = this.GridView1.SelectedIndex;
            Session["FilterIndex"] = this.DropDownList1.SelectedIndex;
        }
 
        protected void DropDownList1_DataBound(object sender, EventArgs e)
        {
            if ((!this.IsPostBack) && (Session["FilterIndex"] != null))
            {
                this.DropDownList1.ClearSelection();
                this.DropDownList1.SelectedIndex = (int)Session["FilterIndex"];
            }
        }
 
    }
}
==================
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="Web_Form.WebForm2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="~/WebForm1.aspx">Go to page 1</asp:HyperLink>
    </div>
    </form>
</body>
</html>

Open in new window

0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now