页面刷新再次触发该事件

发布于 2024-11-07 14:38:47 字数 62 浏览 0 评论 0原文

在asp.net中,当我提交表单并刷新它时,数据再次重新提交? C# 有没有办法在页面加载时捕获页面刷新事件?

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??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

停滞 2024-11-14 14:38:47

ASP.NET 没有提供直接执行此操作的方法。

另一方面,有一些技术可以避免重复提交:

  1. 提交后重定向。这是最糟糕的。即使它避免了重复提交,从用户的角度来看,这在现代 Web 应用程序中也是不可接受的。

  2. 跟踪每个表单、每个会话的提交。当用户第一次提交表单时,请在会话中记住这一点。如果发生另一次提交,请尝试确定是否必须丢弃它(在某些情况下,一定不能;例如,如果我在 StackOverflow 上编辑一次答案,如果需要,我可以编辑两次)。

  3. 首次提交后禁用 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.
    }
}

ASP.NET doesn't provide a way to do it directly.

There are a few techniques, on the other hand, to avoid duplicate submission:

  1. 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.

  2. 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 StackOverflow once, I would be able to do it twice if I need to).

  3. 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.

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:

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.
    }
}
喜你已久 2024-11-14 14:38:47

如果您有任何想法,您可以在提交表单、重新加载页面、删除文本框中的所有文本等后使页面自动刷新。您可以通过添加以下代码来完成此操作 -> Page.Redirect(Request.RawUrl); 在提交方法的底部。

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.

小鸟爱天空丶 2024-11-14 14:38:47

如果你想阻止,只需将这段代码放在 .aspx 页面中,但必须包含 jquery 库

$(document).ready(function(){

window.history.replaceState('','',window.location.href)

})

if you want to prevent just put the piece of code in your .aspx page but must be include jquery library

$(document).ready(function(){

window.history.replaceState('','',window.location.href)

})
公布 2024-11-14 14:38:47

我遇到了同样的问题,我通过会话跟踪解决了它。
我认为这是解决这个问题的简单耗时更少的方法。

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.

请爱~陌生人 2024-11-14 14:38:47

例如:
如果您单击“按钮”系统将捕获事件“button_click”。
如果刷新页面,系统将再次执行相同的事件。
为了避免出现此问题,请在您的事件中插入:
你的活动

private void button_click(object sender, System.EventArgs e)
{
    button.Enabled =false;
    button.Enabled =true;
}

是什么意思?

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

private void button_click(object sender, System.EventArgs e)
{
    button.Enabled =false;
    button.Enabled =true;
}

is what you meant?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文