【问题标题】:page refresh is firing the event again页面刷新再次触发事件
【发布时间】:2011-05-16 07:21:42
【问题描述】:

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

【问题讨论】:

  • 你是在浏览器中按F5刷新页面吗??
  • 是的,当我这样做时,之前触发的事件会再次触发

标签: asp.net page-refresh


【解决方案1】:

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

在这种情况下,用户只能提交一次评论。其他所有评论都将被自动丢弃。

问题是用户可能希望将 cmets 添加到多个博客文章中。我们有两种可能的方式来实现这一点。最简单的方法是在每个非回发请求上重置会话变量,但这将允许用户在一个页面上提交帖子,加载另一个页面,而不是在第一个页面上点击刷新,从而提交两次评论但不是可以在第二页上添加评论了。

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

【讨论】:

  • 我认为这不是问题,因为他正在浏览器中刷新页面。
  • @Muhammad Akhtar:是的,谢谢,我完全误解了这个问题。现在已编辑答案。
  • 只是一个想法。如果我们将其重定向到同一页面怎么办。接受与否?
  • @Malik:见第 1 点:“提交后重定向。[...] 从用户的角度来看,这在现代 Web 应用程序中是不可接受的。”无论您重定向到哪个页面。
【解决方案2】:

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

【讨论】:

    【解决方案3】:

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

    $(document).ready(function(){
    
    window.history.replaceState('','',window.location.href)
    
    })
    

    【讨论】:

    • 欢迎 #Rohan #Rao
    【解决方案4】:

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

    【讨论】:

      【解决方案5】:

      例如: 如果您单击“按钮”,系统将捕获事件“按钮点击”。 如果刷新页面,系统将再次执行相同的事件。 没有这个问题,在你的事件插入: 在您的活动中

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

      你的意思是?

      【讨论】:

      • 与您的此答案的其他副本一样,这不是问题所在。
      猜你喜欢
      • 2011-06-17
      • 1970-01-01
      • 1970-01-01
      • 2021-11-15
      • 2011-06-25
      • 2010-10-17
      • 1970-01-01
      • 2018-12-14
      • 1970-01-01
      相关资源
      最近更新 更多