views:

182

answers:

2

When the user select an item from a dropdownlist and presses a button, my application shows a list of data manually binded and filtered according the selected value. If the user presses the Refresh button of the browser, it asks for confirmation whether the user is sure they want to submmit the query again.

I don't want the browser asks this. How can I avoid this behaviour?

As far as I understand, this can be done implementing the post/redirect/get pattern, but I don't know how to do it in ASP.NET 3.5.

+1  A: 

All POST requests resubmitted by the browser will confirm the resubmission with the user. You cannot change this behavior in the browser.

What the PRG pattern means for asp.net is that you test for postback, perform your processing, and redirect the user to a different page (or the same page with a different querystring to change that page's behavior).

The problem with this pattern is that you lose all the postback features of asp.net, like viewstate and automatic forms handling.

BC
A: 

Yep, something like this will do the job for you, in your page load event for example:

// Check to see if the user submitted the form:
if (Page.IsPostBack){
  // Get the Params collection - query and forms
  NameValueCollection params = Request.Params;
  StringBuilder query = new StringBuilder();

  // Iterate over the params collection to build a complete
  // query string - you can skip this and just build it
  // manually if you don't have many elements to worry about
  for(int i = 0; i <= params.Count - 1; i++)
  {
    // If we're not on the first parameter, add an & seperator
    if (i > 0){
      query.Append("&");
    }

    // Start the query string 
    query.AppendFormat("{0}=", params.GetKey(i));

    // Create a string array that contains
    // the values associated with each key,
    // join them together with commas.
    query.Append(String.Join(",", pColl.GetValues(i));
  }

  Response.Redirect(String.Format("{0}?{1}", 
                      Request.Url.AbsolutePath, query.ToString()))
}

The other problem with this pattern is that you'll end up with this additional redirect in the history which might cause the users to have to click back twice to get back to your search form.

On the plus side, they can now bookmark the results page quite happily and return to their results without having to re-submit the form.

Zhaph - Ben Duguid