0

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 :)

4 Answers 4

2

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.

2
  • 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
    Jul 2, 2009 at 18:58
  • +1, resolves the issue in a very clean manner rather than just "patching".
    – CAbbott
    Jul 2, 2009 at 19:11
0

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.

0

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
}
1
  • 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
    Jul 2, 2009 at 23:53
0

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
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.