检测浏览器的刷新事件 [英] To detect the Browser's Refresh event

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

问题描述

我正在尝试单击浏览器的刷新按钮时防止重复输入,我正在使用此代码.

Hi I am trying prevent duplicate entry when clicking browser''s refresh button for this I am using this code.

Sub Page_Load (sender As Object, e As EventArgs)
    If Not Page.IsPostBack
        Session("update") = Server.URLEncode(System.DateTime.Now.ToString())
    End If
End Sub

Sub Page_PreRender (sender As Object, e As EventArgs)
    ViewState("update") = Session("update")
End Sub

Sub Button1_Click(sender As Object, e As EventArgs)
    If Session("update").ToString() = ViewState("update").ToString() Then
        If AddEmployee(firstName.Text, lastName.Text) = 0
            Message.Text = "Success"
            Session("update")= Server.URLEncode(System.DateTime.Now.ToString())
        Else
            Message.Text = "Failure"
        End If
    Else
        Message.Text = "Failure - Session" 
   End If
    firstName.Text =""
    lastName.Text = ""
End Sub


这样做很好,但是当我尝试插入新条目而不通过浏览器的刷新或F5刷新页面时,没有插入新数据.

因此,请告诉我如何解决此问题.


It is working fine for that,but when I am trying to insert a new entry without refreshing page by browser''s refresh or F5 ,a new data is not inserting.

So plz tell how to solve this problem.

推荐答案

您尝试调试它了吗? Session("update").ToString() = ViewState("update").ToString()条件是否为假并且被视为刷新?

调试代码应该可以告诉您问题所在.检查Session和Viewstate中的值以及它们的更改位置.
这样,代码看起来可以捕获刷新事件.
Did you try debugging it? Is it that Session("update").ToString() = ViewState("update").ToString() condition is false and it is considered a refresh?

Debugging the code should tell you the issue. Check the values in Session and Viewstate and where they change.
As such, code looks fine to trap refresh event.


保存后,请使用Response.Redirect(...),而不是尝试检测刷新.然后F5将不会发回邮件.
有关更多详细信息,请参见此Wikipedia文章. http://en.wikipedia.org/wiki/Post/Redirect/Get [ ^ ].
Instead of trying to detect a refresh, use a Response.Redirect(...) after saving. Then F5 will not post back.
See this Wikipedia article for more details http://en.wikipedia.org/wiki/Post/Redirect/Get[^].


如果您回答我的评论,我不会收到通知.所以我要添加一个答案.

您的代码告诉我,它将第一次插入记录,之后将完全不插入.是这样吗?

我说这是因为.当页面第一次加载时,ViewStateSession的"update"键的值将获得相同的值.
现在,当您单击按钮时,将不会执行Page_Load代码,因为它位于Page.IsPostBack内部.现在触发Button1_Click,它将执行AddEmployee
I won''t get notified if you answer my comment. So I am adding an answer.

Your code tells me that it would insert a record for the first time and after that it wouldn''t insert at all. Is this the case?

I am saying this because. When the page loads for the first time, value of both ViewState and Session''s "update" key gets the same value.
Now when you click the button, Page_Load code is not executed as it is inside Page.IsPostBack. Now Button1_Click is fired which will execute AddEmployee and
Session("update")= Server.URLEncode(System.DateTime.Now.ToString())

将更新session的值.

现在,让我们看看再次单击该按钮输入新记录时会发生什么.
首先,执行顺序,
Page_Load然后Button1_Click然后Page_PreRender.
现在,不执行Page_Load中的代码(Page.IsPostback为true)
Button1_Click被解雇.它不会进入循环,因为Session("update")不等于ViewState("update").

这是你不想要的.

请让我知道这是怎么回事.

will update the value of session.

Now, lets see what happens when you click the button again for entering a new record.
Firstly, the sequence of execution,
Page_Load then Button1_Click and then Page_PreRender.
Now, code within Page_Load is not executed (Page.IsPostback is true)
Button1_Click is fired. It would not go inside the loop as Session("update") is not equal to ViewState("update").

Which is what you don''t want.

Please let me know if this is what is happening.


这篇关于检测浏览器的刷新事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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