IN asp.net when I submit form and refresh it, the data resubmitted again? Is there a way in C# to trap the page refresh event on page load??
在asp.net中,当我提交表单并刷新它时,数据又重新提交了吗?在C#中有一种方法可以在页面加载时捕获页面刷新事件吗?
4 个解决方案
#1
7
ASP.NET doesn't provide a way to do it directly.
ASP.NET没有提供直接执行此操作的方法。
There are a few techniques, on the other hand, to avoid duplicate submission:
另一方面,有一些技术可以避免重复提交:
-
Redirect after submission. This is the worst one. Even if it avoids duplicate submission, it is not acceptable in a modern web application from the users point of view.
提交后重定向。这是最糟糕的一个。即使它避免重复提交,从用户的角度来看,在现代Web应用程序中也是不可接受的。
-
Track submissions per form, per session. When the user submits a form for the first time, remember this in the session. If another submission happens, try to determine if it must be discarded or not (in some cases, it must not; for example, if I edit my answer on * once, I would be able to do it twice if I need to).
每个会话跟踪每个表单的提交。当用户第一次提交表单时,请在会话中记住这一点。如果发生了另一次提交,请尝试确定是否必须丢弃它(在某些情况下,它必须不被丢弃;例如,如果我在*上编辑我的答案一次,如果需要,我将能够执行两次)。
-
Disable submission with JavaScript after the first submission. This avoids in some cases the situation when either the user double-clicks the submission button, or clicks it for the first time, waits and thinks that the form was not submitted, thus clicking for the second time. Of course, don't rely on this one: JavaScript may be disabled, it will work on double-click but not on F5 refresh, and in all cases the technique is not completely reliable.
首次提交后禁用JavaScript提交。这避免了在某些情况下用户双击提交按钮或第一次单击它的情况,等待并认为表单未提交,因此第二次单击。当然,不要依赖于这个:JavaScript可能被禁用,它将在双击但不在F5刷新上工作,并且在所有情况下该技术都不是完全可靠的。
As an illustration, let's try to implement the second one.
举个例子,让我们尝试实现第二个。
Let's say we have a comment box this.textBoxComment
which let the users add a new comment on a page of a blog. The submission is done like this:
假设我们有一个评论框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);
}
}
}
If the user clicks twice, the comment will be posted twice.
如果用户点击两次,评论将被发布两次。
Now, let's add some session tracking:
现在,让我们添加一些会话跟踪:
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.
}
}
}
In this case, the user will be able to submit a comment only once. Every other comment will be automatically discarded.
在这种情况下,用户只能提交一次评论。每个其他评论都将被自动丢弃。
The problem is that the user may want to add comments to several blog posts. We have two possible ways to allow that. The easy one is to reset the session variable on every non-postback request, but this will allow the user to submit a post on one page, load another page, than hit refresh on the first one, thus submitting the comment twice but not being able to add a comment on the second page any longer.
问题是用户可能想要向几个博客帖子添加评论。我们有两种可能的方法来实现这一点。最简单的方法是在每个非回发请求上重置会话变量,但这将允许用户在一个页面上提交帖子,加载另一个页面,而不是在第一个页面上点击刷新,从而提交注释两次但不是能够再在第二页上添加评论。
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");
}
}
The more advanced one is to track in session the list of pages where the comment was submitted.
更高级的是在会话中跟踪提交评论的页面列表。
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.
}
}
#2
1
You could, if it has any point in your mind, make the page refresh itself automatically after submitting the form, reloading the page removing all text from textboxes etc. You do this by adding the following code -> Page.Redirect(Request.RawUrl);
in the bottom of your submit method.
如果您有任何意义,可以在提交表单后自动刷新页面,重新加载页面,从文本框中删除所有文本等。您可以通过添加以下代码 - > Page.Redirect(Request.RawUrl)来完成此操作。 );在提交方法的底部。
#3
0
I was getting same problem and I resolved it by session tracing. And I think it is easy and less time consuming way to solve this problem.
我遇到了同样的问题,我通过会话跟踪解决了这个问题。我认为解决这个问题很简单,耗时少。
#4
-1
for example: if you click on 'button' system will catch the event 'button_click'. if you refresh the page, system will re execute again the same event. to don t have this problem, in your event insert : on your event
例如:如果你点击'按钮'系统将捕捉事件'button_click'。如果刷新页面,系统将再次重新执行同一事件。没有这个问题,在你的活动中插入:你的活动
private void button_click(object sender, System.EventArgs e)
{
button.Enabled =false;
button.Enabled =true;
}
is what you meant?
你是什么意思?
#1
7
ASP.NET doesn't provide a way to do it directly.
ASP.NET没有提供直接执行此操作的方法。
There are a few techniques, on the other hand, to avoid duplicate submission:
另一方面,有一些技术可以避免重复提交:
-
Redirect after submission. This is the worst one. Even if it avoids duplicate submission, it is not acceptable in a modern web application from the users point of view.
提交后重定向。这是最糟糕的一个。即使它避免重复提交,从用户的角度来看,在现代Web应用程序中也是不可接受的。
-
Track submissions per form, per session. When the user submits a form for the first time, remember this in the session. If another submission happens, try to determine if it must be discarded or not (in some cases, it must not; for example, if I edit my answer on * once, I would be able to do it twice if I need to).
每个会话跟踪每个表单的提交。当用户第一次提交表单时,请在会话中记住这一点。如果发生了另一次提交,请尝试确定是否必须丢弃它(在某些情况下,它必须不被丢弃;例如,如果我在*上编辑我的答案一次,如果需要,我将能够执行两次)。
-
Disable submission with JavaScript after the first submission. This avoids in some cases the situation when either the user double-clicks the submission button, or clicks it for the first time, waits and thinks that the form was not submitted, thus clicking for the second time. Of course, don't rely on this one: JavaScript may be disabled, it will work on double-click but not on F5 refresh, and in all cases the technique is not completely reliable.
首次提交后禁用JavaScript提交。这避免了在某些情况下用户双击提交按钮或第一次单击它的情况,等待并认为表单未提交,因此第二次单击。当然,不要依赖于这个:JavaScript可能被禁用,它将在双击但不在F5刷新上工作,并且在所有情况下该技术都不是完全可靠的。
As an illustration, let's try to implement the second one.
举个例子,让我们尝试实现第二个。
Let's say we have a comment box this.textBoxComment
which let the users add a new comment on a page of a blog. The submission is done like this:
假设我们有一个评论框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);
}
}
}
If the user clicks twice, the comment will be posted twice.
如果用户点击两次,评论将被发布两次。
Now, let's add some session tracking:
现在,让我们添加一些会话跟踪:
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.
}
}
}
In this case, the user will be able to submit a comment only once. Every other comment will be automatically discarded.
在这种情况下,用户只能提交一次评论。每个其他评论都将被自动丢弃。
The problem is that the user may want to add comments to several blog posts. We have two possible ways to allow that. The easy one is to reset the session variable on every non-postback request, but this will allow the user to submit a post on one page, load another page, than hit refresh on the first one, thus submitting the comment twice but not being able to add a comment on the second page any longer.
问题是用户可能想要向几个博客帖子添加评论。我们有两种可能的方法来实现这一点。最简单的方法是在每个非回发请求上重置会话变量,但这将允许用户在一个页面上提交帖子,加载另一个页面,而不是在第一个页面上点击刷新,从而提交注释两次但不是能够再在第二页上添加评论。
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");
}
}
The more advanced one is to track in session the list of pages where the comment was submitted.
更高级的是在会话中跟踪提交评论的页面列表。
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.
}
}
#2
1
You could, if it has any point in your mind, make the page refresh itself automatically after submitting the form, reloading the page removing all text from textboxes etc. You do this by adding the following code -> Page.Redirect(Request.RawUrl);
in the bottom of your submit method.
如果您有任何意义,可以在提交表单后自动刷新页面,重新加载页面,从文本框中删除所有文本等。您可以通过添加以下代码 - > Page.Redirect(Request.RawUrl)来完成此操作。 );在提交方法的底部。
#3
0
I was getting same problem and I resolved it by session tracing. And I think it is easy and less time consuming way to solve this problem.
我遇到了同样的问题,我通过会话跟踪解决了这个问题。我认为解决这个问题很简单,耗时少。
#4
-1
for example: if you click on 'button' system will catch the event 'button_click'. if you refresh the page, system will re execute again the same event. to don t have this problem, in your event insert : on your event
例如:如果你点击'按钮'系统将捕捉事件'button_click'。如果刷新页面,系统将再次重新执行同一事件。没有这个问题,在你的活动中插入:你的活动
private void button_click(object sender, System.EventArgs e)
{
button.Enabled =false;
button.Enabled =true;
}
is what you meant?
你是什么意思?