call today Call Today 320.281.0605

Search Results – Webpage has expired on back button

December 8, 2009

search

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

expired

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?

  • Share/Bookmark
  • http://blog.stevienova.com Steve

    Google Site Search :)

  • http://www.thedeets.com Ed Kohler

    Would it be better to send people to a new page for results? People seem to understand the back button very well.

  • Joel

    Well with this approach, the back button does work.

blog comments powered by Disqus
Agent Cody Banks 2 Destination London full lenght movie download Afghan Knights download movie Inside the Smiths download movie Beverly Hills Chihuahua download movie Permanent Vacation download movie Indiana Jones and the Kingdom of the Crystal Skull download movie Afghan Knights download movie Inside the Smiths download movie Beverly Hills Chihuahua download movie Permanent Vacation download movie Indiana Jones and the Kingdom of the Crystal Skull download movie