C#点击刷新时验证重复上次回发(F5)
我有一个生成文件的webform,但是当我单击生成回发的按钮以生成文件时,如果我按刷新(F5),页面将重新提交回发并重新生成文件,那么有任何方法可以验证它并向用户显示消息,或者干脆什么都不做C#点击刷新时验证重复上次回发(F5),c#,asp.net,validation,refresh,C#,Asp.net,Validation,Refresh,我有一个生成文件的webform,但是当我单击生成回发的按钮以生成文件时,如果我按刷新(F5),页面将重新提交回发并重新生成文件,那么有任何方法可以验证它并向用户显示消息,或者干脆什么都不做 谢谢:)如果他们在已回发的页面上点击“刷新”,浏览器应向他们发出警告。不过,我如何处理它是在会话中跟踪我所做的,这样我就不会重复某些操作。一个简单的标志就足够了。更简单的方法是使用Post-Rediret-Get模式 确保查看维基百科文章的外部链接 在回发逻辑中检查有问题的文件是否存在,并且仅在文件不存在
谢谢:)如果他们在已回发的页面上点击“刷新”,浏览器应向他们发出警告。不过,我如何处理它是在会话中跟踪我所做的,这样我就不会重复某些操作。一个简单的标志就足够了。更简单的方法是使用Post-Rediret-Get模式
确保查看维基百科文章的外部链接 在回发逻辑中检查有问题的文件是否存在,并且仅在文件不存在时创建该文件:
if (false == System.IO.File.Exists(filename))
{
// create the file
}
else
{
// do whatever you do when the file already exists
}
我为这个问题写了一个解决方案,如果有人需要,就在这里
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
}
}
我在生产中使用的应用程序,我有一个bug,需要用尽可能少的代码来修复,你给我的解决方案将非常适合发布。+1,以非常干净的方式解决问题,而不仅仅是“修补”。问题更大。。。如果你有另一个场景,如事务表或其他什么,我会发布我制定的解决方案。
if (!isRefresh)
PostFile();
else
{
//Error msg you are refreshing
}