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;
}
...
}
}