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

匿名 (未验证) 提交于 2019-12-03 08:33:39

问题:

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

回答1:

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 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.



回答3:

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 } 


回答4:

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 } 


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