可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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 }