我有一个生成文件的网络表单,但是当我单击生成回发以生成文件的按钮时向用户显示消息或干脆什么都不做!
谢谢 :)
更简单的方法是使用 Post Rediret Get 模式。
http://en.wikipedia.org/wiki/Post/Redirect/Get
请务必查看该 Wikipedia 文章中的外部链接。
我为这个问题写了一个解决方案,如果有人需要它就在这里。
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
}
}
你只需要评估:
if (!isRefresh)
PostFile();
else
{
//Error msg you are refreshing
}
如果他们在已回发的页面上点击刷新,浏览器应该警告他们。我如何处理它虽然是在会话跟踪我做了什么,所以我不重复某些动作。一个简单的标志就足够了。
检查回发逻辑中是否存在有问题的文件,并且仅在文件不存在时才创建该文件:
if (false == System.IO.File.Exists(filename))
{
// create the file
}
else
{
// do whatever you do when the file already exists
}