页面刷新再次触发事件

时间:2011-05-16 07:21:42

标签: asp.net page-refresh

在asp.net中提交表单并刷新时,数据又重新提交了吗? 在C#中是否有一种方法可以在页面加载时捕获页面刷新事件?

5 个答案:

答案 0 :(得分:7)

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

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

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

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

  3. 首次提交后,禁止使用JavaScript提交。这避免了在某些情况下用户双击提交按钮或第一次单击它的情况,等待并认为表单未提交,因此第二次单击。当然,不要依赖于这个:JavaScript可能被禁用,它可以双击而不是F5刷新,并且在所有情况下该技术都不完全可靠。

  4. 作为一个例子,让我们尝试实现第二个。

    假设我们有一个评论框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.
        }
    }
    

答案 1 :(得分:1)

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

答案 2 :(得分:1)

如果您想防止将代码片段放在.aspx页面中,但必须包含jquery库

$(document).ready(function(){

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

})

答案 3 :(得分:0)

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

答案 4 :(得分:-1)

例如: 如果你点击'按钮'系统将捕捉事件'button_click'。 如果刷新页面,系统将再次重新执行同一事件。 没有这个问题,在你的事件中插入: 关于你的活动

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

是你的意思吗?

相关问题