I'm close to going live with this web app but still have one glaring problem, which is wrong data in gridviews after the user uses the back button. This is another mastersearch/detail type page. Users search for items, which show in a gridview on page search.aspx. Users can click on items to go to the detail page, itemdetails.aspx. All works fine until users use the back button.
Example, search for dog, get a list of dogs in gridview on search.aspx. Click dog records and see their details on itemdetails.aspx. Search for cats, get a list of cats on gridview in search.aspx. Click cats to see details, use back to go back to the search.aspx with list of cats, click another cat to see details. All is fine so far. Problems start when users go back a few times, from cat detail to cat search resutlts, back to dog detail, then back to dog search results. Now if user clicks the dog in search results row 3, the detail page that is displayed is the cat from row 3 in the most recent search.
I've tried clearing the cache, this forces the grid to reload but the user always sees the page expired page and has to resubmit the data. I've tried Server.Transfer and Response.Redirect to the search page to try to reload the grid, but this makes the user have to click the record twice(once to reload the expired grid, then again to go to the details.) Also tried rebinding grid, and so many other things over 4-5 or so days, I don't remember them all. Nothing has fixed the problem without creating and unexpected side effect. At this point I just want a solution, even if I have to rewrite the whole thing. Thanks for any suggestions!
public partial class Search : System.Web.UI.Page { public string searchString;//for info typed in search box public string searchISBN;//to hold clicked item in gridview ISBN
private void GetProducts()
{
try
{
DataSet ds = DataAccess.GetProductsPerCategory(searchString);
GridView1.DataSource = ds;
GridView1.DataBind();
}
catch (SqlException ex)
{
lblSearch.Text = "Cannot get product data." + ex.Message;
}
}
protected void Button2_Click(object sender, EventArgs e)
//button in gridview, sends to item detail page
{
LinkButton btn = (LinkButton)(sender);
searchISBN = btn.CommandArgument;
//call product detail page, pass ISBN
Global.SetISBNData = searchISBN;
Server.Transfer("ItemDetail.aspx");
}
//when this search page is called from site.master page,
//search criteria passed to GetProducts
protected void Page_Load(object sender, EventArgs e)
{
if (IsExpired())
{
Response.Redirect("Expired.aspx");
}
else
{
this.SaveTimeStamps();
searchString = Global.GetSearchData;
GetProducts();
}
}
private bool IsExpired()
{
if (Session["Search_SearchStamp"] == null)
return false;
else if (ViewState["SearchStamp"] == null)
return false;
else if (ViewState["SearchStamp"].ToString() ==
Session["Search_SearchStamp"].ToString())
return false;
else
return true;
}
private void SaveTimeStamps()
{
DateTime dtm = DateTime.Now;
ViewState.Add("SearchStamp", dtm);
Session.Add("Search_SearchStamp", dtm);
}
}