0
votes

I want to use Pagination and cache the search result for when certain pages changes. I have a transaction page which loads with the latest 100 transactions by default. However the user can click on the 'Search' link which will display the search criteria and the page would retrieve that specific data set.

I added pagination to my GridView and it shows the correct number of page based on page size field .

My challenge is retrieving the data set for the GridView pages after the user clicks on the page number. The page loads by default or through a user search. The grid is displayed. Now,say, a user want to go to second page. How do i approach that?

Here is what i tried:

The Page load function calls the Populate() , below. I added a DataSet global variable,pagingQuery, to save my search criteria. In the populate() i set the pagingQuery to what i retrieve from the db. However this variable gets set to null after the pages load!! Why? In the PageIndexChanging() i simply do

     protected void GridView1_PageIndexChanging(object sender,GridViewPageEventArgs e)
    {

        GridView1.PageIndex = e.NewPageIndex;
        //Populate();
        GridView1.DataSource = pagingQuery;
        GridView1.DataBind();
    }

   public void Populate()
    {
        try
        {
            Transaction tran = new Transaction();

            //DataSet ds = tran.GetAllBankTransactions(); 
            pagingQuery = tran.GetAllBankTransactions();
            GridView1.DataSource = pagingQuery;
            GridView1.DataBind();
        }
        catch{
        }
    }

For the search criteria a similar approach is followed but to no avail. The value is null and any subsequent page is empty. I was able to get Pagination to work by calling populate() again with the local variable DataSet ds, however i cannot do the same for the search as saving a global value for the search criteria is failing miserably lool. I could possibly save the search text and call the db again but i cannot store a global variable it seems.
Is my approach accurate? Is there a way to save the DataSet/string/variable between pageIndexChanging() calls?

    protected void Search_button_click(object sender, EventArgs e)
    {
        try
        {
            ....
            else if (ddSearhField.SelectedValue == "Bank" && ddSearchFieldValue.SelectedValue != "All Banks")
            {
                pagingQuery  = trans.GetAllBankTransactionsByBank(Convert.ToInt64(ddSearchFieldValue.SelectedValue), strFromDate, strToDate, showAllFields);
                GridView1.DataSource = pagingQuery;
                GridView1.DataBind();

                lblHeading.Text = ddSearchFieldValue.SelectedItem.Text;
            }
            ...

        }
    }
2

2 Answers

1
votes

Try to save datasource in session then load in Page_Load section. I usually store in session the select command of the gridview. Example:

DS_GRIDVIEW1.SelectCommand = myquery;
GRIDVIEW1.DataBind();
Session["myquery"] = DS_MCV.SelectCommand.ToString();

Then in the Page Load

protected void Page_Load(object sender, EventArgs e)
{
    if (Session["myquery"]!= null)
    {
        try
        {
            DS_GRIDVIEW1.SelectCommand = Session["myquery"].ToString();
            GRIDVIEW1.DataBind();
        }
        catch (Exception)
        {
            Session["myquery"] = null;    
        }

    }

}
0
votes

@Robert Mattea , thanks that was the trick. I didn't think of that.

What i did in Page_Load() i see if the session still has that variable pagingQuery if not i add it to the session.

       protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (Session["IsValidUser"] == null || Session["IsValidUser"].ToString() != "true")
                Response.Redirect("Login.aspx", false);

            /// Setting the Body tag. 
            Site1 m = (Site1)Master;
            m.PageSection = "transactions";

            //AH setup the pagingQuery variable to cache where we are. 
            if (Session["pagingQuery"] == null) 
            {
                Session.Add("pagingQuery", null);
            }

            if (!IsPostBack)
            {
                Populate();
            }
            /////////////////////////////
            user = (User)Session["user"];

        }
        catch (Exception ex)
        {
            Response.Redirect("Login.aspx");
        }        
    }

In both the Populate() function that retrieves the default top 100 and the Search_button_click() I set the the variable to the query

    public void Populate()
    {
        try
        {
            Transaction tran = new Transaction();

           // DataSet ds = tran.GetAllBankTransactions();
            //AH: set the variable 
            Session["pagingQuery"] = tran.GetAllBankTransactions();
            GridView1.DataSource= (DataSet)Session["pagingQuery"];
            GridView1.DataBind();
            ..........
        }
    }

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
       {

        GridView1.PageIndex = e.NewPageIndex;
        //AH get the data set used to retrieve the query  default page load or search criteria
        GridView1.DataSource = (DataSet)Session["pagingQuery"];
        GridView1.DataBind();

    }

It is working like a charm.


One more thought. How bad am i adding to the memory heap by buffering the DataSet in the session? Data set could be 100 or 5K records but mostly in the range of couple of hundred records with each record about 1Kb.