C# validate repeat last PostBack when hit Refresh (F5)

*爱你&永不变心* 提交于 2019-12-04 21:59:22

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.

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.

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
}
jmpena

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
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!