Blog
Search Results – Webpage has expired on back button
December 8, 2009 Author: Joel
I’ve been asked this a few times now so I thought I would blog my solution to point others to this next time I am asked.
Often times people will create a simple search on their site. The most obvious way to do this is with a simple textbox and button. You then handle the search when the button is clicked in the code-behind.
It may look something like this:
protected void btnSearch_Click(object sender, EventArgs e)
{
if (!String.IsNullOrEmpty(txtSearch.Text.Trim()))
{
BindSearch(txtSearch.Text.Trim());
}
}
Pretty straight forward. The problem here is you’re doing a postback to the same page. You won’t have a history of the previous searches in your browser. After doing a search and you want to see what you had for results from the previous search and simply hit back in your browser you’ll see this
So what I have always done to avoid this is to simply do a Response.Redirect to the page instead of just calling the BindSearch() method and put the search term in the query string.
protected void btnSearch_Click(object sender, EventArgs e)
{
if (!String.IsNullOrEmpty(txtSearch.Text.Trim()))
{
Response.Redirect("/Search.aspx?search=" + txtSearch.Text.Trim());
}
}
This is a pretty simple solution, does anyone else have a different way to handle this?
-
http://blog.stevienova.com Steve
-
http://www.thedeets.com Ed Kohler
-
Joel
-
Categories
- asp.net (12)
- Blog (3)
- Business (5)
- Conferences (1)
- Errors (5)
- Office (1)
- Portfolio (3)
- Reviews (3)
- SEO (4)
- SQL Server (4)
- Tips (2)
- Uncategorized (2)
- Web Development (14)
-
Archives