在asp.net中,当我提交表单并刷新它时,数据又重新提交了吗?C#中有什么方法可以在页面加载时捕获页面刷新事件?
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)
})
例如:如果单击“按钮”,系统将捕获事件“ button_click”。如果刷新页面,系统将再次执行同一事件。为了没有这个问题,在您的事件中插入:在您的活动中
private void button_click(object sender, System.EventArgs e)
{
button.Enabled =false;
button.Enabled =true;
}
你是什么意思?