刷新页面再次触发事件 [英] page refresh is firing the event again

查看:235
本文介绍了刷新页面再次触发事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

推荐答案

ASP.NET并没有提供一种方式来直接做到这一点。

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. 提交后重定向。这是最差的一个。即使它避免了重复提交,它不是从用户的角度看现代Web应用程序可以接受的。

  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.

每个表单的轨道意见书,每个会话。当用户提交表单的第一次,在会话中记住这一点。如果另一个提交,可尝试以确定它是否必须被丢弃或不(在某些情况下,切不可;例如,如果修改我的答案在计算器上一次,我就可以做两次,如果我需要)。

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

禁用提交支持JavaScript的第一次提交后。这避免了在某些情况下,当任一用户双击提交按钮或点击它为第一次,等待,认为形式没有提交,从而点击第二次的情况。当然,不靠这一个不支持JavaScript可能会被禁用,它将在双击而不是在F5刷新工作,在所有情况下的技术不完全可靠

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.

让我们说我们有一个评论框 this.textBoxComment 这让用户的博客页面上添加一个新评论。提交像这样做:

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

这篇关于刷新页面再次触发事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆