检测浏览器的刷新事件
我正在尝试单击浏览器的刷新按钮时防止重复输入,我正在使用此代码.
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 thatSession("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[^].
如果您回答我的评论,我不会收到通知.所以我要添加一个答案.
您的代码告诉我,它将第一次插入记录,之后将完全不插入.是这样吗?
我说这是因为.当页面第一次加载时,ViewState
和Session
的"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 bothViewState
andSession
''s "update" key gets the same value.
Now when you click the button, Page_Load code is not executed as it is insidePage.IsPostBack
. NowButton1_Click
is fired which will executeAddEmployee
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.