views:

851

answers:

4

Hello, i have a webform that generates a file, but when i click the button that produces the postback to generate the file Once it finish if i press Refresh (F5) the page resubmit the postback and regenerates the file, there's any way to validate it and show a message to the user or simply DO NOTHING!

thanks :)

A: 

the browser should warn them if they hit refresh on a page that has been postbacked. how i handle it though is in the session track what i have done so i don't repeat certain actions. a simple flag should suffice.

Christopher Kelly
+2  A: 

The simpler way will be to use Post Rediret Get pattern.

http://en.wikipedia.org/wiki/Post/Redirect/Get

Make sure to check out External Links on that Wikipedia article.

SolutionYogi
the application that im working its in production and i have a bug that have to fix with less code as possible, the solution that ur giving me will works great for a release.
jmpena
+1, resolves the issue in a very clean manner rather than just "patching".
CAbbott
A: 

Check for the existence of the file in question in your postback logic and only create the file if the file doesn't already exist:

if (false == System.IO.File.Exists(filename))
{
   // create the file
}
else
{
   // do whatever you do when the file already exists
}
Parvenu74
the problem its bigger than that... what if you have another scenarie a transaction table or something, ill post a solution that i made.
jmpena
A: 

hello... i wrote a solution for this problem and here it is if anyone needs it.

  protected void Page_Load(object sender, System.EventArgs e)
  {
    /*******/
    //Validate if the user Refresh the webform.
    //U will need::
    //A global private variable called ""private bool isRefresh = false;""
    //a global publica variable called ""public int refreshValue = 0;""
    //a html control before </form> tag: ""<input type="hidden" name="ValidateRefresh" value="<%= refreshValue %>">""

    int postRefreshValue = 0;
    refreshValue = SII.Utils.convert.ToInt(Request.Form["ValidateRefresh"]); //u can use a int.parse()

    if (refreshValue == 0)
      Session["ValidateRefresh"] = 0;

    postRefreshValue = SII.Utils.convert.ToInt(Session["ValidateRefresh"]); //can use a int.parse()

    if (refreshValue < postRefreshValue)
      isRefresh = true;

    Session["ValidateRefresh"] = postRefreshValue + 1;
    refreshValue = SII.Utils.convert.ToInt(Session["ValidateRefresh"]); //can use a int.parse()
    /********/

    if (!IsPostBack)
    {
       //your code
    }
  }

you just have to evaluate:

if (!isRefresh)
  PostFile();
else
{
  //Error msg you are refreshing
}
jmpena