在asp.net中,当我提交表单并刷新它时,数据再次重新提交?C# 中有没有办法在页面加载时捕获页面刷新事件?
5 回答
ASP.NET 不提供直接执行此操作的方法。
另一方面,有一些技术可以避免重复提交:
提交后重定向。这是最糟糕的一个。即使它避免了重复提交,从用户的角度来看,它在现代 Web 应用程序中也是不可接受的。
跟踪每个表单、每个会话的提交。当用户第一次提交表单时,请在会话中记住这一点。如果发生另一次提交,请尝试确定是否必须丢弃它(在某些情况下,它不能;例如,如果我在 StackOverflow 上编辑我的答案一次,如果需要,我可以做两次)。
首次提交后禁用 JavaScript 提交。这避免了在某些情况下,用户双击提交按钮,或者第一次点击,等待并认为表单没有提交,从而第二次点击的情况。当然,不要依赖这个:JavaScript 可能被禁用,它可以在双击时工作,但不能在 F5 刷新时工作,而且在所有情况下,该技术都不是完全可靠的。
作为说明,让我们尝试实现第二个。
假设我们有一个评论框this.textBoxComment
,让用户可以在博客页面上添加新评论。提交是这样完成的:
private void Page_Load(object sender, System.EventArgs e)
{
if (this.IsPostBack)
{
string comment = this.ValidateCommentInput(this.textBoxComment.Text);
if (comment != null)
{
this.databaseContext.AddComment(comment);
}
}
}
如果用户点击两次,评论将被发布两次。
现在,让我们添加一些会话跟踪:
private void Page_Load(object sender, System.EventArgs e)
{
if (this.IsPostBack)
{
if (this.Session["commentAdded"] == null)
{
string comment = this.ValidateCommentInput(this.textBoxComment.Text);
if (comment != null)
{
this.databaseContext.AddComment(comment);
this.Session.Add("commentAdded", true);
}
}
else
{
// TODO: Inform the user that the comment cannot be submitted
// several times.
}
}
}
在这种情况下,用户只能提交一次评论。其他所有评论将被自动丢弃。
问题是用户可能想要向多个博客文章添加评论。我们有两种可能的方式来实现这一点。最简单的方法是在每个非回发请求上重置会话变量,但这将允许用户在一个页面上提交帖子,加载另一个页面,而不是在第一个页面上点击刷新,从而提交两次评论但不是能够再在第二页上添加评论。
private void Page_Load(object sender, System.EventArgs e)
{
if (this.IsPostBack)
{
if (this.Session["commentAdded"] == null)
{
string comment = this.ValidateCommentInput(this.textBoxComment.Text);
if (comment != null)
{
this.databaseContext.AddComment(comment);
this.Session.Add("commentAdded", true);
}
}
else
{
// TODO: Inform the user that the comment cannot be submitted
// several times.
}
}
else
{
this.Session.Remove("commentAdded");
}
}
更高级的一种是在会话中跟踪提交评论的页面列表。
private void Page_Load(object sender, System.EventArgs e)
{
List<string> commentsTrack = this.Session["commentAdded"] as List<string>;
string blogPostId = this.ValidatePostId(this.Request.QueryString["id"]);
if (blogPostId != null)
{
if (this.IsPostBack)
{
this.AddComment(commentsTrack);
}
else
{
if (commentsTrack != null && commentsTrack.Contains(blogPostId))
{
commentsTrack.Remove(blogPostId);
}
}
}
}
private void AddComment(List<string> commentsTrack)
{
if (commentsTrack == null || !commentsTrack.Contains(blogPostId))
{
string comment = this.ValidateCommentInput(this.textBoxComment.Text);
if (comment != null)
{
this.databaseContext.AddComment(comment);
if (commentsTrack == null)
{
commentsTrack = new List<string>();
}
commentsTrack.Add(blogPostId);
this.Session["commentAdded"] = commentsTrack;
}
}
else
{
// TODO: Inform the user that the comment cannot be submitted
// several times.
}
}
如果您有任何想法,您可以在提交表单后自动刷新页面,重新加载页面,从文本框中删除所有文本等。您可以通过Page.Redirect(Request.RawUrl);
在提交方法的底部添加以下代码 -> 来做到这一点.
如果你想防止只是把这段代码放在你的 .aspx 页面中,但必须包含 jquery 库
$(document).ready(function(){
window.history.replaceState('','',window.location.href)
})
我遇到了同样的问题,我通过会话跟踪解决了它。而且我认为解决这个问题是一种简单且耗时较少的方法。
例如:如果您单击“按钮”,系统将捕获事件“按钮单击”。如果刷新页面,系统将再次执行相同的事件。没有这个问题,在您的活动中插入:在您的活动中
private void button_click(object sender, System.EventArgs e)
{
button.Enabled =false;
button.Enabled =true;
}
你是什么意思?