By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,636 Members | 1,915 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,636 IT Pros & Developers. It's quick & easy.

Handling refreshes from browser

P: n/a
I was looking for a way to handle refreshes (user pressed refresh button)
and found a piece of code to check if a Web page was refreshed but I can't
get it to work.

The code is:
************************************************** **********
Namespace StevenBey.Web.UI

Public Class Page
Inherits System.Web.UI.Page
Private _refreshState As Boolean
Private _isRefresh As Boolean
Public ReadOnly Property IsRefresh() As Boolean
Get
Return _isRefresh
End Get
End Property
Protected Overrides Sub LoadViewState(savedState As Object)
Dim allStates As Object() = CType(savedState, Object())
MyBase.LoadViewState(allStates(0))
_refreshState = CBool(allStates(1))
_isRefresh = _refreshState = CBool(Session("__ISREFRESH"))
End Sub 'LoadViewState
Protected Overrides Function SaveViewState() As Object
Session("__ISREFRESH") = _refreshState
Dim allStates(2) As Object
allStates(0) = MyBase.SaveViewState()
allStates(1) = Not _refreshState
Return allStates
End Function 'SaveViewState
End Class 'Page

End NameSpace
************************************************** **********

If I do a:

trace.warn("is Refresh = " & Page.IsRefresh)

or

trace.warn("is Refresh = " & IsRefresh)

I get the error:

BC30456 'IsRefresh' is not a member of 'System.Web.UI.Page'

I took the compiled version (StevenBey.Web.UI.dll) and put it in my Bin
directory.

If you look at trace page you won't see __ISREFRESH?

Is there something else I need to do to get this to work?

Thanks,

Tom

Aug 21 '06 #1
Share this Question
Share on Google+
15 Replies


P: n/a
Hi Tom,

see the remarks here made by a User:

http://jarednevans.typepad.com/techn..._techno_b.html

this method seems to fail when back button is pressed. You can download
a demo.

Tommaso

tshad ha scritto:
I was looking for a way to handle refreshes (user pressed refresh button)
and found a piece of code to check if a Web page was refreshed but I can't
get it to work.

The code is:
************************************************** **********
Namespace StevenBey.Web.UI

Public Class Page
Inherits System.Web.UI.Page
Private _refreshState As Boolean
Private _isRefresh As Boolean
Public ReadOnly Property IsRefresh() As Boolean
Get
Return _isRefresh
End Get
End Property
Protected Overrides Sub LoadViewState(savedState As Object)
Dim allStates As Object() = CType(savedState, Object())
MyBase.LoadViewState(allStates(0))
_refreshState = CBool(allStates(1))
_isRefresh = _refreshState = CBool(Session("__ISREFRESH"))
End Sub 'LoadViewState
Protected Overrides Function SaveViewState() As Object
Session("__ISREFRESH") = _refreshState
Dim allStates(2) As Object
allStates(0) = MyBase.SaveViewState()
allStates(1) = Not _refreshState
Return allStates
End Function 'SaveViewState
End Class 'Page

End NameSpace
************************************************** **********

If I do a:

trace.warn("is Refresh = " & Page.IsRefresh)

or

trace.warn("is Refresh = " & IsRefresh)

I get the error:

BC30456 'IsRefresh' is not a member of 'System.Web.UI.Page'

I took the compiled version (StevenBey.Web.UI.dll) and put it in my Bin
directory.

If you look at trace page you won't see __ISREFRESH?

Is there something else I need to do to get this to work?

Thanks,

Tom
Aug 21 '06 #2

P: n/a
I was able to get it to work after closer reading of the article.

************************************************** *****************
Namespace MyFunctions

Public Class Page
Inherits System.Web.UI.Page
Private _refreshState As Boolean
Private _isRefresh As Boolean
Public ReadOnly Property IsRefresh() As Boolean
Get
Return _isRefresh
End Get
End Property
Protected Overrides Sub LoadViewState(savedState As Object)
Dim allStates As Object() = CType(savedState, Object())
MyBase.LoadViewState(allStates(0))
_refreshState = CBool(allStates(1))
_isRefresh = _refreshState = CBool(Session("__ISREFRESH"))
End Sub 'LoadViewState
Protected Overrides Function SaveViewState() As Object
Session("__ISREFRESH") = _refreshState
Dim allStates(2) As Object
allStates(0) = MyBase.SaveViewState()
allStates(1) = Not _refreshState
Return allStates
End Function 'SaveViewState

End Class 'Page

End NameSpace
***********************************************

In my page, I have to add an inherits:

<%@ Page Language="VB" trace="true" ContentType="text/html"
ResponseEncoding="iso-8859-1" Inherits="MyFunctions.Page"%>
....
trace.warn("is Refresh = " & IsRefresh())

Then it works.

I am confused as to the code with the "allStates" and how it works.

In LoadViewState:
Dim allStates As Object() = CType(savedState, Object())
MyBase.LoadViewState(allStates(0))
_refreshState = CBool(allStates(1))

It seems to be getting the savedState from the parameters list, then I think
it does the normal LoadViewState passing allStates(0) (which is really just
savedState - so why not just pass savedState??).

Also, what is allStates(1)?

In SaveViewState:
Dim allStates(2) As Object
allStates(0) = MyBase.SaveViewState()
allStates(1) = Not _refreshState
Return allStates

I assume the MyBase.SaveViewState() is just doing the normal SaveViewState
and passes back the changed ViewState into allStates(0), what is
allStates(1) and why are we passing back allStates - does this overwrite the
ViewState that was written out by MyBase.SaveViewState()?

Thanks,

Tom

<to**************@uniroma1.itwrote in message
news:11*********************@m73g2000cwd.googlegro ups.com...
Hi Tom,

see the remarks here made by a User:

http://jarednevans.typepad.com/techn..._techno_b.html

this method seems to fail when back button is pressed. You can download
a demo.

Tommaso

tshad ha scritto:
>I was looking for a way to handle refreshes (user pressed refresh button)
and found a piece of code to check if a Web page was refreshed but I
can't
get it to work.

The code is:
************************************************* ***********
Namespace StevenBey.Web.UI

Public Class Page
Inherits System.Web.UI.Page
Private _refreshState As Boolean
Private _isRefresh As Boolean
Public ReadOnly Property IsRefresh() As Boolean
Get
Return _isRefresh
End Get
End Property
Protected Overrides Sub LoadViewState(savedState As Object)
Dim allStates As Object() = CType(savedState, Object())
MyBase.LoadViewState(allStates(0))
_refreshState = CBool(allStates(1))
_isRefresh = _refreshState = CBool(Session("__ISREFRESH"))
End Sub 'LoadViewState
Protected Overrides Function SaveViewState() As Object
Session("__ISREFRESH") = _refreshState
Dim allStates(2) As Object
allStates(0) = MyBase.SaveViewState()
allStates(1) = Not _refreshState
Return allStates
End Function 'SaveViewState
End Class 'Page

End NameSpace
************************************************* ***********

If I do a:

trace.warn("is Refresh = " & Page.IsRefresh)

or

trace.warn("is Refresh = " & IsRefresh)

I get the error:

BC30456 'IsRefresh' is not a member of 'System.Web.UI.Page'

I took the compiled version (StevenBey.Web.UI.dll) and put it in my Bin
directory.

If you look at trace page you won't see __ISREFRESH?

Is there something else I need to do to get this to work?

Thanks,

Tom

Aug 22 '06 #3

P: n/a
But what about the back button problem.
I think that user was right. It doesn't work in that case.

The (almost) funny thing is that a guy has copied this article and
published it on Code Project site.
He also copied the bugs clearly ... :)

Tommaso

tshad ha scritto:
I was able to get it to work after closer reading of the article.

************************************************** *****************
Namespace MyFunctions

Public Class Page
Inherits System.Web.UI.Page
Private _refreshState As Boolean
Private _isRefresh As Boolean
Public ReadOnly Property IsRefresh() As Boolean
Get
Return _isRefresh
End Get
End Property
Protected Overrides Sub LoadViewState(savedState As Object)
Dim allStates As Object() = CType(savedState, Object())
MyBase.LoadViewState(allStates(0))
_refreshState = CBool(allStates(1))
_isRefresh = _refreshState = CBool(Session("__ISREFRESH"))
End Sub 'LoadViewState
Protected Overrides Function SaveViewState() As Object
Session("__ISREFRESH") = _refreshState
Dim allStates(2) As Object
allStates(0) = MyBase.SaveViewState()
allStates(1) = Not _refreshState
Return allStates
End Function 'SaveViewState

End Class 'Page

End NameSpace
***********************************************

In my page, I have to add an inherits:

<%@ Page Language="VB" trace="true" ContentType="text/html"
ResponseEncoding="iso-8859-1" Inherits="MyFunctions.Page"%>
...
trace.warn("is Refresh = " & IsRefresh())

Then it works.

I am confused as to the code with the "allStates" and how it works.

In LoadViewState:
Dim allStates As Object() = CType(savedState, Object())
MyBase.LoadViewState(allStates(0))
_refreshState = CBool(allStates(1))

It seems to be getting the savedState from the parameters list, then I think
it does the normal LoadViewState passing allStates(0) (which is really just
savedState - so why not just pass savedState??).

Also, what is allStates(1)?

In SaveViewState:
Dim allStates(2) As Object
allStates(0) = MyBase.SaveViewState()
allStates(1) = Not _refreshState
Return allStates

I assume the MyBase.SaveViewState() is just doing the normal SaveViewState
and passes back the changed ViewState into allStates(0), what is
allStates(1) and why are we passing back allStates - does this overwrite the
ViewState that was written out by MyBase.SaveViewState()?

Thanks,

Tom

<to**************@uniroma1.itwrote in message
news:11*********************@m73g2000cwd.googlegro ups.com...
Hi Tom,

see the remarks here made by a User:

http://jarednevans.typepad.com/techn..._techno_b.html

this method seems to fail when back button is pressed. You can download
a demo.

Tommaso

tshad ha scritto:
I was looking for a way to handle refreshes (user pressed refresh button)
and found a piece of code to check if a Web page was refreshed but I
can't
get it to work.

The code is:
************************************************** **********
Namespace StevenBey.Web.UI

Public Class Page
Inherits System.Web.UI.Page
Private _refreshState As Boolean
Private _isRefresh As Boolean
Public ReadOnly Property IsRefresh() As Boolean
Get
Return _isRefresh
End Get
End Property
Protected Overrides Sub LoadViewState(savedState As Object)
Dim allStates As Object() = CType(savedState, Object())
MyBase.LoadViewState(allStates(0))
_refreshState = CBool(allStates(1))
_isRefresh = _refreshState = CBool(Session("__ISREFRESH"))
End Sub 'LoadViewState
Protected Overrides Function SaveViewState() As Object
Session("__ISREFRESH") = _refreshState
Dim allStates(2) As Object
allStates(0) = MyBase.SaveViewState()
allStates(1) = Not _refreshState
Return allStates
End Function 'SaveViewState
End Class 'Page

End NameSpace
************************************************** **********

If I do a:

trace.warn("is Refresh = " & Page.IsRefresh)

or

trace.warn("is Refresh = " & IsRefresh)

I get the error:

BC30456 'IsRefresh' is not a member of 'System.Web.UI.Page'

I took the compiled version (StevenBey.Web.UI.dll) and put it in my Bin
directory.

If you look at trace page you won't see __ISREFRESH?

Is there something else I need to do to get this to work?

Thanks,

Tom
Aug 22 '06 #4

P: n/a
<to**************@uniroma1.itwrote in message
news:11**********************@b28g2000cwb.googlegr oups.com...
But what about the back button problem.
I think that user was right. It doesn't work in that case.
I know that that is also a problem and I am going to address that problem in
a bit. But I needed to get the refresh problem taken care of right away.
>
The (almost) funny thing is that a guy has copied this article and
published it on Code Project site.
He also copied the bugs clearly ... :)
I agree.

But that allows someone else to at least get a starting point to work from.

Tom
>
Tommaso

tshad ha scritto:
>I was able to get it to work after closer reading of the article.

************************************************* ******************
Namespace MyFunctions

Public Class Page
Inherits System.Web.UI.Page
Private _refreshState As Boolean
Private _isRefresh As Boolean
Public ReadOnly Property IsRefresh() As Boolean
Get
Return _isRefresh
End Get
End Property
Protected Overrides Sub LoadViewState(savedState As Object)
Dim allStates As Object() = CType(savedState, Object())
MyBase.LoadViewState(allStates(0))
_refreshState = CBool(allStates(1))
_isRefresh = _refreshState = CBool(Session("__ISREFRESH"))
End Sub 'LoadViewState
Protected Overrides Function SaveViewState() As Object
Session("__ISREFRESH") = _refreshState
Dim allStates(2) As Object
allStates(0) = MyBase.SaveViewState()
allStates(1) = Not _refreshState
Return allStates
End Function 'SaveViewState

End Class 'Page

End NameSpace
***********************************************

In my page, I have to add an inherits:

<%@ Page Language="VB" trace="true" ContentType="text/html"
ResponseEncoding="iso-8859-1" Inherits="MyFunctions.Page"%>
...
trace.warn("is Refresh = " & IsRefresh())

Then it works.

I am confused as to the code with the "allStates" and how it works.

In LoadViewState:
Dim allStates As Object() = CType(savedState, Object())
MyBase.LoadViewState(allStates(0))
_refreshState = CBool(allStates(1))

It seems to be getting the savedState from the parameters list, then I
think
it does the normal LoadViewState passing allStates(0) (which is really
just
savedState - so why not just pass savedState??).

Also, what is allStates(1)?

In SaveViewState:
Dim allStates(2) As Object
allStates(0) = MyBase.SaveViewState()
allStates(1) = Not _refreshState
Return allStates

I assume the MyBase.SaveViewState() is just doing the normal
SaveViewState
and passes back the changed ViewState into allStates(0), what is
allStates(1) and why are we passing back allStates - does this overwrite
the
ViewState that was written out by MyBase.SaveViewState()?

Thanks,

Tom

<to**************@uniroma1.itwrote in message
news:11*********************@m73g2000cwd.googlegr oups.com...
Hi Tom,

see the remarks here made by a User:

http://jarednevans.typepad.com/techn..._techno_b.html

this method seems to fail when back button is pressed. You can download
a demo.

Tommaso

tshad ha scritto:

I was looking for a way to handle refreshes (user pressed refresh
button)
and found a piece of code to check if a Web page was refreshed but I
can't
get it to work.

The code is:
************************************************* ***********
Namespace StevenBey.Web.UI

Public Class Page
Inherits System.Web.UI.Page
Private _refreshState As Boolean
Private _isRefresh As Boolean
Public ReadOnly Property IsRefresh() As Boolean
Get
Return _isRefresh
End Get
End Property
Protected Overrides Sub LoadViewState(savedState As Object)
Dim allStates As Object() = CType(savedState, Object())
MyBase.LoadViewState(allStates(0))
_refreshState = CBool(allStates(1))
_isRefresh = _refreshState = CBool(Session("__ISREFRESH"))
End Sub 'LoadViewState
Protected Overrides Function SaveViewState() As Object
Session("__ISREFRESH") = _refreshState
Dim allStates(2) As Object
allStates(0) = MyBase.SaveViewState()
allStates(1) = Not _refreshState
Return allStates
End Function 'SaveViewState
End Class 'Page

End NameSpace
************************************************* ***********

If I do a:

trace.warn("is Refresh = " & Page.IsRefresh)

or

trace.warn("is Refresh = " & IsRefresh)

I get the error:

BC30456 'IsRefresh' is not a member of 'System.Web.UI.Page'

I took the compiled version (StevenBey.Web.UI.dll) and put it in my
Bin
directory.

If you look at trace page you won't see __ISREFRESH?

Is there something else I need to do to get this to work?

Thanks,

Tom

Aug 23 '06 #5

P: n/a

"tshad" <ts**********@ftsolutions.comwrote in message
news:%2******************@TK2MSFTNGP05.phx.gbl...
<to**************@uniroma1.itwrote in message
news:11**********************@b28g2000cwb.googlegr oups.com...
>But what about the back button problem.
I think that user was right. It doesn't work in that case.
Actually, I did some work with the program and found that it also doesn't
work on the inital page. It only works for PostBacks.

Looking into it I found that since LoadViewState is not called on an initial
page load (makes sense as there would be no ViewState saved here), there is
nothing to test.

I need to find some other way to make it work or find a way to find out if
the initial page.

Tom
I know that that is also a problem and I am going to address that problem
in a bit. But I needed to get the refresh problem taken care of right
away.
>>
The (almost) funny thing is that a guy has copied this article and
published it on Code Project site.
He also copied the bugs clearly ... :)

I agree.

But that allows someone else to at least get a starting point to work
from.

Tom
>>
Tommaso

tshad ha scritto:
>>I was able to get it to work after closer reading of the article.

************************************************ *******************
Namespace MyFunctions

Public Class Page
Inherits System.Web.UI.Page
Private _refreshState As Boolean
Private _isRefresh As Boolean
Public ReadOnly Property IsRefresh() As Boolean
Get
Return _isRefresh
End Get
End Property
Protected Overrides Sub LoadViewState(savedState As Object)
Dim allStates As Object() = CType(savedState, Object())
MyBase.LoadViewState(allStates(0))
_refreshState = CBool(allStates(1))
_isRefresh = _refreshState = CBool(Session("__ISREFRESH"))
End Sub 'LoadViewState
Protected Overrides Function SaveViewState() As Object
Session("__ISREFRESH") = _refreshState
Dim allStates(2) As Object
allStates(0) = MyBase.SaveViewState()
allStates(1) = Not _refreshState
Return allStates
End Function 'SaveViewState

End Class 'Page

End NameSpace
********************************************** *

In my page, I have to add an inherits:

<%@ Page Language="VB" trace="true" ContentType="text/html"
ResponseEncoding="iso-8859-1" Inherits="MyFunctions.Page"%>
...
trace.warn("is Refresh = " & IsRefresh())

Then it works.

I am confused as to the code with the "allStates" and how it works.

In LoadViewState:
Dim allStates As Object() = CType(savedState, Object())
MyBase.LoadViewState(allStates(0))
_refreshState = CBool(allStates(1))

It seems to be getting the savedState from the parameters list, then I
think
it does the normal LoadViewState passing allStates(0) (which is really
just
savedState - so why not just pass savedState??).

Also, what is allStates(1)?

In SaveViewState:
Dim allStates(2) As Object
allStates(0) = MyBase.SaveViewState()
allStates(1) = Not _refreshState
Return allStates

I assume the MyBase.SaveViewState() is just doing the normal
SaveViewState
and passes back the changed ViewState into allStates(0), what is
allStates(1) and why are we passing back allStates - does this overwrite
the
ViewState that was written out by MyBase.SaveViewState()?

Thanks,

Tom

<to**************@uniroma1.itwrote in message
news:11*********************@m73g2000cwd.googleg roups.com...
Hi Tom,

see the remarks here made by a User:

http://jarednevans.typepad.com/techn..._techno_b.html

this method seems to fail when back button is pressed. You can
download
a demo.

Tommaso

tshad ha scritto:

I was looking for a way to handle refreshes (user pressed refresh
button)
and found a piece of code to check if a Web page was refreshed but I
can't
get it to work.

The code is:
************************************************ ************
Namespace StevenBey.Web.UI

Public Class Page
Inherits System.Web.UI.Page
Private _refreshState As Boolean
Private _isRefresh As Boolean
Public ReadOnly Property IsRefresh() As Boolean
Get
Return _isRefresh
End Get
End Property
Protected Overrides Sub LoadViewState(savedState As Object)
Dim allStates As Object() = CType(savedState, Object())
MyBase.LoadViewState(allStates(0))
_refreshState = CBool(allStates(1))
_isRefresh = _refreshState = CBool(Session("__ISREFRESH"))
End Sub 'LoadViewState
Protected Overrides Function SaveViewState() As Object
Session("__ISREFRESH") = _refreshState
Dim allStates(2) As Object
allStates(0) = MyBase.SaveViewState()
allStates(1) = Not _refreshState
Return allStates
End Function 'SaveViewState
End Class 'Page

End NameSpace
************************************************ ************

If I do a:

trace.warn("is Refresh = " & Page.IsRefresh)

or

trace.warn("is Refresh = " & IsRefresh)

I get the error:

BC30456 'IsRefresh' is not a member of 'System.Web.UI.Page'

I took the compiled version (StevenBey.Web.UI.dll) and put it in my
Bin
directory.

If you look at trace page you won't see __ISREFRESH?

Is there something else I need to do to get this to work?

Thanks,

Tom


Aug 24 '06 #6

P: n/a
Hi Tom,

I am afraid that is not exactly an "elementary" topic. Here is a more
authoritative source:

http://msdn.microsoft.com/msdnmag/is...8/CuttingEdge/

Let us know about the best solution... :)

Tommaso

tshad ha scritto:
"tshad" <ts**********@ftsolutions.comwrote in message
news:%2******************@TK2MSFTNGP05.phx.gbl...
<to**************@uniroma1.itwrote in message
news:11**********************@b28g2000cwb.googlegr oups.com...
But what about the back button problem.
I think that user was right. It doesn't work in that case.

Actually, I did some work with the program and found that it also doesn't
work on the inital page. It only works for PostBacks.

Looking into it I found that since LoadViewState is not called on an initial
page load (makes sense as there would be no ViewState saved here), there is
nothing to test.

I need to find some other way to make it work or find a way to find out if
the initial page.

Tom
I know that that is also a problem and I am going to address that problem
in a bit. But I needed to get the refresh problem taken care of right
away.
>
The (almost) funny thing is that a guy has copied this article and
published it on Code Project site.
He also copied the bugs clearly ... :)
I agree.

But that allows someone else to at least get a starting point to work
from.

Tom
>
Tommaso

tshad ha scritto:

I was able to get it to work after closer reading of the article.

************************************************* ******************
Namespace MyFunctions

Public Class Page
Inherits System.Web.UI.Page
Private _refreshState As Boolean
Private _isRefresh As Boolean
Public ReadOnly Property IsRefresh() As Boolean
Get
Return _isRefresh
End Get
End Property
Protected Overrides Sub LoadViewState(savedState As Object)
Dim allStates As Object() = CType(savedState, Object())
MyBase.LoadViewState(allStates(0))
_refreshState = CBool(allStates(1))
_isRefresh = _refreshState = CBool(Session("__ISREFRESH"))
End Sub 'LoadViewState
Protected Overrides Function SaveViewState() As Object
Session("__ISREFRESH") = _refreshState
Dim allStates(2) As Object
allStates(0) = MyBase.SaveViewState()
allStates(1) = Not _refreshState
Return allStates
End Function 'SaveViewState

End Class 'Page

End NameSpace
***********************************************

In my page, I have to add an inherits:

<%@ Page Language="VB" trace="true" ContentType="text/html"
ResponseEncoding="iso-8859-1" Inherits="MyFunctions.Page"%>
...
trace.warn("is Refresh = " & IsRefresh())

Then it works.

I am confused as to the code with the "allStates" and how it works.

In LoadViewState:
Dim allStates As Object() = CType(savedState, Object())
MyBase.LoadViewState(allStates(0))
_refreshState = CBool(allStates(1))

It seems to be getting the savedState from the parameters list, then I
think
it does the normal LoadViewState passing allStates(0) (which is really
just
savedState - so why not just pass savedState??).

Also, what is allStates(1)?

In SaveViewState:
Dim allStates(2) As Object
allStates(0) = MyBase.SaveViewState()
allStates(1) = Not _refreshState
Return allStates

I assume the MyBase.SaveViewState() is just doing the normal
SaveViewState
and passes back the changed ViewState into allStates(0), what is
allStates(1) and why are we passing back allStates - does this overwrite
the
ViewState that was written out by MyBase.SaveViewState()?

Thanks,

Tom

<to**************@uniroma1.itwrote in message
news:11*********************@m73g2000cwd.googlegr oups.com...
Hi Tom,

see the remarks here made by a User:

http://jarednevans.typepad.com/techn..._techno_b.html

this method seems to fail when back button is pressed. You can
download
a demo.

Tommaso

tshad ha scritto:

I was looking for a way to handle refreshes (user pressed refresh
button)
and found a piece of code to check if a Web page was refreshed but I
can't
get it to work.

The code is:
************************************************* ***********
Namespace StevenBey.Web.UI

Public Class Page
Inherits System.Web.UI.Page
Private _refreshState As Boolean
Private _isRefresh As Boolean
Public ReadOnly Property IsRefresh() As Boolean
Get
Return _isRefresh
End Get
End Property
Protected Overrides Sub LoadViewState(savedState As Object)
Dim allStates As Object() = CType(savedState, Object())
MyBase.LoadViewState(allStates(0))
_refreshState = CBool(allStates(1))
_isRefresh = _refreshState = CBool(Session("__ISREFRESH"))
End Sub 'LoadViewState
Protected Overrides Function SaveViewState() As Object
Session("__ISREFRESH") = _refreshState
Dim allStates(2) As Object
allStates(0) = MyBase.SaveViewState()
allStates(1) = Not _refreshState
Return allStates
End Function 'SaveViewState
End Class 'Page

End NameSpace
************************************************* ***********

If I do a:

trace.warn("is Refresh = " & Page.IsRefresh)

or

trace.warn("is Refresh = " & IsRefresh)

I get the error:

BC30456 'IsRefresh' is not a member of 'System.Web.UI.Page'

I took the compiled version (StevenBey.Web.UI.dll) and put it in my
Bin
directory.

If you look at trace page you won't see __ISREFRESH?

Is there something else I need to do to get this to work?

Thanks,

Tom

Aug 24 '06 #7

P: n/a
<to**************@uniroma1.itwrote in message
news:11**********************@i3g2000cwc.googlegro ups.com...
Hi Tom,

I am afraid that is not exactly an "elementary" topic. Here is a more
authoritative source:

http://msdn.microsoft.com/msdnmag/is...8/CuttingEdge/
I looked there, but the article is about Script Callbacks.

That was interesting also, as I was looking at doing this (like Ajax) but in
..net.

I knew you could do this in .net 1.1, but if what this is saying is correct
then I can't do it.

"ASP.NET 2.0 provides a built-in mechanism for client callbacks based on the
services of a COM object that ships with Internet Explorer 5.0 and later. By
using the same object, you can implement a script callback mechanism in
ASP.NET 1.x as well"

I don't want to do it if you have to depend on the user using IE.

Thanks,

Tom
>
Let us know about the best solution... :)

Tommaso

tshad ha scritto:
>"tshad" <ts**********@ftsolutions.comwrote in message
news:%2******************@TK2MSFTNGP05.phx.gbl. ..
<to**************@uniroma1.itwrote in message
news:11**********************@b28g2000cwb.googlegr oups.com...
But what about the back button problem.
I think that user was right. It doesn't work in that case.

Actually, I did some work with the program and found that it also doesn't
work on the inital page. It only works for PostBacks.

Looking into it I found that since LoadViewState is not called on an
initial
page load (makes sense as there would be no ViewState saved here), there
is
nothing to test.

I need to find some other way to make it work or find a way to find out
if
the initial page.

Tom
I know that that is also a problem and I am going to address that
problem
in a bit. But I needed to get the refresh problem taken care of right
away.
The (almost) funny thing is that a guy has copied this article and
published it on Code Project site.
He also copied the bugs clearly ... :)

I agree.

But that allows someone else to at least get a starting point to work
from.

Tom
Tommaso

tshad ha scritto:

I was able to get it to work after closer reading of the article.

************************************************ *******************
Namespace MyFunctions

Public Class Page
Inherits System.Web.UI.Page
Private _refreshState As Boolean
Private _isRefresh As Boolean
Public ReadOnly Property IsRefresh() As Boolean
Get
Return _isRefresh
End Get
End Property
Protected Overrides Sub LoadViewState(savedState As Object)
Dim allStates As Object() = CType(savedState, Object())
MyBase.LoadViewState(allStates(0))
_refreshState = CBool(allStates(1))
_isRefresh = _refreshState = CBool(Session("__ISREFRESH"))
End Sub 'LoadViewState
Protected Overrides Function SaveViewState() As Object
Session("__ISREFRESH") = _refreshState
Dim allStates(2) As Object
allStates(0) = MyBase.SaveViewState()
allStates(1) = Not _refreshState
Return allStates
End Function 'SaveViewState

End Class 'Page

End NameSpace
********************************************** *

In my page, I have to add an inherits:

<%@ Page Language="VB" trace="true" ContentType="text/html"
ResponseEncoding="iso-8859-1" Inherits="MyFunctions.Page"%>
...
trace.warn("is Refresh = " & IsRefresh())

Then it works.

I am confused as to the code with the "allStates" and how it works.

In LoadViewState:
Dim allStates As Object() = CType(savedState, Object())
MyBase.LoadViewState(allStates(0))
_refreshState = CBool(allStates(1))

It seems to be getting the savedState from the parameters list, then
I
think
it does the normal LoadViewState passing allStates(0) (which is
really
just
savedState - so why not just pass savedState??).

Also, what is allStates(1)?

In SaveViewState:
Dim allStates(2) As Object
allStates(0) = MyBase.SaveViewState()
allStates(1) = Not _refreshState
Return allStates

I assume the MyBase.SaveViewState() is just doing the normal
SaveViewState
and passes back the changed ViewState into allStates(0), what is
allStates(1) and why are we passing back allStates - does this
overwrite
the
ViewState that was written out by MyBase.SaveViewState()?

Thanks,

Tom

<to**************@uniroma1.itwrote in message
news:11*********************@m73g2000cwd.googleg roups.com...
Hi Tom,

see the remarks here made by a User:

http://jarednevans.typepad.com/techn..._techno_b.html

this method seems to fail when back button is pressed. You can
download
a demo.

Tommaso

tshad ha scritto:

I was looking for a way to handle refreshes (user pressed refresh
button)
and found a piece of code to check if a Web page was refreshed but
I
can't
get it to work.

The code is:
************************************************ ************
Namespace StevenBey.Web.UI

Public Class Page
Inherits System.Web.UI.Page
Private _refreshState As Boolean
Private _isRefresh As Boolean
Public ReadOnly Property IsRefresh() As Boolean
Get
Return _isRefresh
End Get
End Property
Protected Overrides Sub LoadViewState(savedState As Object)
Dim allStates As Object() = CType(savedState, Object())
MyBase.LoadViewState(allStates(0))
_refreshState = CBool(allStates(1))
_isRefresh = _refreshState = CBool(Session("__ISREFRESH"))
End Sub 'LoadViewState
Protected Overrides Function SaveViewState() As Object
Session("__ISREFRESH") = _refreshState
Dim allStates(2) As Object
allStates(0) = MyBase.SaveViewState()
allStates(1) = Not _refreshState
Return allStates
End Function 'SaveViewState
End Class 'Page

End NameSpace
************************************************ ************

If I do a:

trace.warn("is Refresh = " & Page.IsRefresh)

or

trace.warn("is Refresh = " & IsRefresh)

I get the error:

BC30456 'IsRefresh' is not a member of
'System.Web.UI.Page'

I took the compiled version (StevenBey.Web.UI.dll) and put it in
my
Bin
directory.

If you look at trace page you won't see __ISREFRESH?

Is there something else I need to do to get this to work?

Thanks,

Tom


Aug 24 '06 #8

P: n/a
Hi Tom,

I posted the wrong link. Here it is:

http://msdn.microsoft.com/library/de...rockaspnet.asp

"Trap the Browser Refresh
In an article originally published on aspnetPRO Magazine several months
ago, I outlined the steps needed to detect when the user presses the F5
browser button to refresh the current page. The page refresh is the
browser's response to a specific user action-hitting the F5 key or
clicking the toolbar button. The page refresh action is a sort of
internal browser operation, for which the browser doesn't provide any
external notification

see also:
http://www.joel.net/code/refresh_capture.aspx

Let me know if you find a definitive solution...

Tommaso

tshad ha scritto:
<to**************@uniroma1.itwrote in message
news:11**********************@i3g2000cwc.googlegro ups.com...
Hi Tom,

I am afraid that is not exactly an "elementary" topic. Here is a more
authoritative source:

http://msdn.microsoft.com/msdnmag/is...8/CuttingEdge/

I looked there, but the article is about Script Callbacks.

That was interesting also, as I was looking at doing this (like Ajax) but in
.net.

I knew you could do this in .net 1.1, but if what this is saying is correct
then I can't do it.

"ASP.NET 2.0 provides a built-in mechanism for client callbacks based on the
services of a COM object that ships with Internet Explorer 5.0 and later. By
using the same object, you can implement a script callback mechanism in
ASP.NET 1.x as well"

I don't want to do it if you have to depend on the user using IE.

Thanks,

Tom

Let us know about the best solution... :)

Tommaso

tshad ha scritto:
"tshad" <ts**********@ftsolutions.comwrote in message
news:%2******************@TK2MSFTNGP05.phx.gbl...
<to**************@uniroma1.itwrote in message
news:11**********************@b28g2000cwb.googlegr oups.com...
But what about the back button problem.
I think that user was right. It doesn't work in that case.


Actually, I did some work with the program and found that it also doesn't
work on the inital page. It only works for PostBacks.

Looking into it I found that since LoadViewState is not called on an
initial
page load (makes sense as there would be no ViewState saved here), there
is
nothing to test.

I need to find some other way to make it work or find a way to find out
if
the initial page.

Tom

I know that that is also a problem and I am going to address that
problem
in a bit. But I needed to get the refresh problem taken care of right
away.


The (almost) funny thing is that a guy has copied this article and
published it on Code Project site.
He also copied the bugs clearly ... :)

I agree.

But that allows someone else to at least get a starting point to work
from.

Tom


Tommaso

tshad ha scritto:

I was able to get it to work after closer reading of the article.

************************************************* ******************
Namespace MyFunctions

Public Class Page
Inherits System.Web.UI.Page
Private _refreshState As Boolean
Private _isRefresh As Boolean
Public ReadOnly Property IsRefresh() As Boolean
Get
Return _isRefresh
End Get
End Property
Protected Overrides Sub LoadViewState(savedState As Object)
Dim allStates As Object() = CType(savedState, Object())
MyBase.LoadViewState(allStates(0))
_refreshState = CBool(allStates(1))
_isRefresh = _refreshState = CBool(Session("__ISREFRESH"))
End Sub 'LoadViewState
Protected Overrides Function SaveViewState() As Object
Session("__ISREFRESH") = _refreshState
Dim allStates(2) As Object
allStates(0) = MyBase.SaveViewState()
allStates(1) = Not _refreshState
Return allStates
End Function 'SaveViewState

End Class 'Page

End NameSpace
***********************************************

In my page, I have to add an inherits:

<%@ Page Language="VB" trace="true" ContentType="text/html"
ResponseEncoding="iso-8859-1" Inherits="MyFunctions.Page"%>
...
trace.warn("is Refresh = " & IsRefresh())

Then it works.

I am confused as to the code with the "allStates" and how it works.

In LoadViewState:
Dim allStates As Object() = CType(savedState, Object())
MyBase.LoadViewState(allStates(0))
_refreshState = CBool(allStates(1))

It seems to be getting the savedState from the parameters list, then
I
think
it does the normal LoadViewState passing allStates(0) (which is
really
just
savedState - so why not just pass savedState??).

Also, what is allStates(1)?

In SaveViewState:
Dim allStates(2) As Object
allStates(0) = MyBase.SaveViewState()
allStates(1) = Not _refreshState
Return allStates

I assume the MyBase.SaveViewState() is just doing the normal
SaveViewState
and passes back the changed ViewState into allStates(0), what is
allStates(1) and why are we passing back allStates - does this
overwrite
the
ViewState that was written out by MyBase.SaveViewState()?

Thanks,

Tom

<to**************@uniroma1.itwrote in message
news:11*********************@m73g2000cwd.googlegr oups.com...
Hi Tom,

see the remarks here made by a User:

http://jarednevans.typepad.com/techn..._techno_b.html

this method seems to fail when back button is pressed. You can
download
a demo.

Tommaso

tshad ha scritto:

I was looking for a way to handle refreshes (user pressed refresh
button)
and found a piece of code to check if a Web page was refreshed but
I
can't
get it to work.

The code is:
************************************************* ***********
Namespace StevenBey.Web.UI

Public Class Page
Inherits System.Web.UI.Page
Private _refreshState As Boolean
Private _isRefresh As Boolean
Public ReadOnly Property IsRefresh() As Boolean
Get
Return _isRefresh
End Get
End Property
Protected Overrides Sub LoadViewState(savedState As Object)
Dim allStates As Object() = CType(savedState, Object())
MyBase.LoadViewState(allStates(0))
_refreshState = CBool(allStates(1))
_isRefresh = _refreshState = CBool(Session("__ISREFRESH"))
End Sub 'LoadViewState
Protected Overrides Function SaveViewState() As Object
Session("__ISREFRESH") = _refreshState
Dim allStates(2) As Object
allStates(0) = MyBase.SaveViewState()
allStates(1) = Not _refreshState
Return allStates
End Function 'SaveViewState
End Class 'Page

End NameSpace
************************************************* ***********

If I do a:

trace.warn("is Refresh = " & Page.IsRefresh)

or

trace.warn("is Refresh = " & IsRefresh)

I get the error:

BC30456 'IsRefresh' is not a member of
'System.Web.UI.Page'

I took the compiled version (StevenBey.Web.UI.dll) and put it in
my
Bin
directory.

If you look at trace page you won't see __ISREFRESH?

Is there something else I need to do to get this to work?

Thanks,

Tom
Aug 24 '06 #9

P: n/a
<to**************@uniroma1.itwrote in message
news:11**********************@h48g2000cwc.googlegr oups.com...
Hi Tom,

I posted the wrong link. Here it is:

http://msdn.microsoft.com/library/de...rockaspnet.asp
I'll take a look at that to see what it says.
"Trap the Browser Refresh
In an article originally published on aspnetPRO Magazine several months
ago, I outlined the steps needed to detect when the user presses the F5
browser button to refresh the current page. The page refresh is the
browser's response to a specific user action-hitting the F5 key or
clicking the toolbar button. The page refresh action is a sort of
internal browser operation, for which the browser doesn't provide any
external notification

see also:
http://www.joel.net/code/refresh_capture.aspx
Already looking at this one.

I am looking at using ideas from his code to solve the problem from my
program dealing with the LoadViewState on the initial load problem.

I did find another problem that I didn't notice before with my code. I
didn't notice that my SaveViewState is being executed twice and I don't know
why. I put a trace statement in the function and found that it is being
called before the normal SaveViewState and during the SaveViewState.

aspx.page Begin Init
aspx.page End Init 0.000079 0.000079
aspx.page Begin PreRender 0.000116 0.000037
aspx.page End PreRender 0.000165 0.000049
Inside refresh.cs at SaveViewState 0.000880 0.000715
aspx.page Begin SaveViewState 0.001932 0.001053
Inside refresh.cs at SaveViewState 0.002330 0.000398
aspx.page End SaveViewState 0.002384 0.000054
aspx.page Begin Render 0.002411 0.000027
aspx.page End Render 0.137321 0.134909
Why would this happen?

Thanks,

Tom
>
Let me know if you find a definitive solution...

Tommaso

tshad ha scritto:
><to**************@uniroma1.itwrote in message
news:11**********************@i3g2000cwc.googlegr oups.com...
Hi Tom,

I am afraid that is not exactly an "elementary" topic. Here is a more
authoritative source:

http://msdn.microsoft.com/msdnmag/is...8/CuttingEdge/

I looked there, but the article is about Script Callbacks.

That was interesting also, as I was looking at doing this (like Ajax) but
in
.net.

I knew you could do this in .net 1.1, but if what this is saying is
correct
then I can't do it.

"ASP.NET 2.0 provides a built-in mechanism for client callbacks based on
the
services of a COM object that ships with Internet Explorer 5.0 and later.
By
using the same object, you can implement a script callback mechanism in
ASP.NET 1.x as well"

I don't want to do it if you have to depend on the user using IE.

Thanks,

Tom
>
Let us know about the best solution... :)

Tommaso

tshad ha scritto:

"tshad" <ts**********@ftsolutions.comwrote in message
news:%2******************@TK2MSFTNGP05.phx.gbl. ..
<to**************@uniroma1.itwrote in message
news:11**********************@b28g2000cwb.googlegr oups.com...
But what about the back button problem.
I think that user was right. It doesn't work in that case.
Actually, I did some work with the program and found that it also
doesn't
work on the inital page. It only works for PostBacks.

Looking into it I found that since LoadViewState is not called on an
initial
page load (makes sense as there would be no ViewState saved here),
there
is
nothing to test.

I need to find some other way to make it work or find a way to find
out
if
the initial page.

Tom

I know that that is also a problem and I am going to address that
problem
in a bit. But I needed to get the refresh problem taken care of
right
away.
The (almost) funny thing is that a guy has copied this article and
published it on Code Project site.
He also copied the bugs clearly ... :)

I agree.

But that allows someone else to at least get a starting point to
work
from.

Tom
Tommaso

tshad ha scritto:

I was able to get it to work after closer reading of the article.

************************************************ *******************
Namespace MyFunctions

Public Class Page
Inherits System.Web.UI.Page
Private _refreshState As Boolean
Private _isRefresh As Boolean
Public ReadOnly Property IsRefresh() As Boolean
Get
Return _isRefresh
End Get
End Property
Protected Overrides Sub LoadViewState(savedState As Object)
Dim allStates As Object() = CType(savedState, Object())
MyBase.LoadViewState(allStates(0))
_refreshState = CBool(allStates(1))
_isRefresh = _refreshState = CBool(Session("__ISREFRESH"))
End Sub 'LoadViewState
Protected Overrides Function SaveViewState() As Object
Session("__ISREFRESH") = _refreshState
Dim allStates(2) As Object
allStates(0) = MyBase.SaveViewState()
allStates(1) = Not _refreshState
Return allStates
End Function 'SaveViewState

End Class 'Page

End NameSpace
********************************************** *

In my page, I have to add an inherits:

<%@ Page Language="VB" trace="true" ContentType="text/html"
ResponseEncoding="iso-8859-1" Inherits="MyFunctions.Page"%>
...
trace.warn("is Refresh = " & IsRefresh())

Then it works.

I am confused as to the code with the "allStates" and how it
works.

In LoadViewState:
Dim allStates As Object() = CType(savedState, Object())
MyBase.LoadViewState(allStates(0))
_refreshState = CBool(allStates(1))

It seems to be getting the savedState from the parameters list,
then
I
think
it does the normal LoadViewState passing allStates(0) (which is
really
just
savedState - so why not just pass savedState??).

Also, what is allStates(1)?

In SaveViewState:
Dim allStates(2) As Object
allStates(0) = MyBase.SaveViewState()
allStates(1) = Not _refreshState
Return allStates

I assume the MyBase.SaveViewState() is just doing the normal
SaveViewState
and passes back the changed ViewState into allStates(0), what is
allStates(1) and why are we passing back allStates - does this
overwrite
the
ViewState that was written out by MyBase.SaveViewState()?

Thanks,

Tom

<to**************@uniroma1.itwrote in message
news:11*********************@m73g2000cwd.googleg roups.com...
Hi Tom,

see the remarks here made by a User:

http://jarednevans.typepad.com/techn..._techno_b.html

this method seems to fail when back button is pressed. You can
download
a demo.

Tommaso

tshad ha scritto:

I was looking for a way to handle refreshes (user pressed
refresh
button)
and found a piece of code to check if a Web page was refreshed
but
I
can't
get it to work.

The code is:
************************************************ ************
Namespace StevenBey.Web.UI

Public Class Page
Inherits System.Web.UI.Page
Private _refreshState As Boolean
Private _isRefresh As Boolean
Public ReadOnly Property IsRefresh() As Boolean
Get
Return _isRefresh
End Get
End Property
Protected Overrides Sub LoadViewState(savedState As Object)
Dim allStates As Object() = CType(savedState, Object())
MyBase.LoadViewState(allStates(0))
_refreshState = CBool(allStates(1))
_isRefresh = _refreshState = CBool(Session("__ISREFRESH"))
End Sub 'LoadViewState
Protected Overrides Function SaveViewState() As Object
Session("__ISREFRESH") = _refreshState
Dim allStates(2) As Object
allStates(0) = MyBase.SaveViewState()
allStates(1) = Not _refreshState
Return allStates
End Function 'SaveViewState
End Class 'Page

End NameSpace
************************************************ ************

If I do a:

trace.warn("is Refresh = " & Page.IsRefresh)

or

trace.warn("is Refresh = " & IsRefresh)

I get the error:

BC30456 'IsRefresh' is not a member of
'System.Web.UI.Page'

I took the compiled version (StevenBey.Web.UI.dll) and put it
in
my
Bin
directory.

If you look at trace page you won't see __ISREFRESH?

Is there something else I need to do to get this to work?

Thanks,

Tom



Aug 25 '06 #10

P: n/a
Below.

"tshad" <ts**********@ftsolutions.comwrote in message
news:OB****************@TK2MSFTNGP04.phx.gbl...
<to**************@uniroma1.itwrote in message
news:11**********************@h48g2000cwc.googlegr oups.com...
>Hi Tom,

I posted the wrong link. Here it is:

http://msdn.microsoft.com/library/de...rockaspnet.asp
I'll take a look at that to see what it says.
>"Trap the Browser Refresh
In an article originally published on aspnetPRO Magazine several months
ago, I outlined the steps needed to detect when the user presses the F5
browser button to refresh the current page. The page refresh is the
browser's response to a specific user action-hitting the F5 key or
clicking the toolbar button. The page refresh action is a sort of
internal browser operation, for which the browser doesn't provide any
external notification

see also:
http://www.joel.net/code/refresh_capture.aspx

Already looking at this one.

I am looking at using ideas from his code to solve the problem from my
program dealing with the LoadViewState on the initial load problem.

I did find another problem that I didn't notice before with my code. I
didn't notice that my SaveViewState is being executed twice and I don't
know why. I put a trace statement in the function and found that it is
being called before the normal SaveViewState and during the SaveViewState.

aspx.page Begin Init
aspx.page End Init 0.000079 0.000079
aspx.page Begin PreRender 0.000116 0.000037
aspx.page End PreRender 0.000165 0.000049
Inside refresh.cs at SaveViewState 0.000880 0.000715
aspx.page Begin SaveViewState 0.001932 0.001053
Inside refresh.cs at SaveViewState 0.002330 0.000398
aspx.page End SaveViewState 0.002384 0.000054
aspx.page Begin Render 0.002411 0.000027
aspx.page End Render 0.137321 0.134909
Why would this happen?
I found that this happens if I have trace="true" on the page. If I have
trace="false", it works fine.

Setting trace="true" causes the SVS to fire a second time (I assume the
event that fires between the normal page events is from this).

Any idea why that is?

Thanks,

Tom
>
Thanks,

Tom
>>
Let me know if you find a definitive solution...

Tommaso

tshad ha scritto:
>><to**************@uniroma1.itwrote in message
news:11**********************@i3g2000cwc.googleg roups.com...
Hi Tom,

I am afraid that is not exactly an "elementary" topic. Here is a more
authoritative source:

http://msdn.microsoft.com/msdnmag/is...8/CuttingEdge/

I looked there, but the article is about Script Callbacks.

That was interesting also, as I was looking at doing this (like Ajax)
but in
.net.

I knew you could do this in .net 1.1, but if what this is saying is
correct
then I can't do it.

"ASP.NET 2.0 provides a built-in mechanism for client callbacks based on
the
services of a COM object that ships with Internet Explorer 5.0 and
later. By
using the same object, you can implement a script callback mechanism in
ASP.NET 1.x as well"

I don't want to do it if you have to depend on the user using IE.

Thanks,

Tom
Let us know about the best solution... :)

Tommaso

tshad ha scritto:

"tshad" <ts**********@ftsolutions.comwrote in message
news:%2******************@TK2MSFTNGP05.phx.gbl.. .
<to**************@uniroma1.itwrote in message
news:11**********************@b28g2000cwb.googleg roups.com...
But what about the back button problem.
I think that user was right. It doesn't work in that case.
Actually, I did some work with the program and found that it also
doesn't
work on the inital page. It only works for PostBacks.

Looking into it I found that since LoadViewState is not called on an
initial
page load (makes sense as there would be no ViewState saved here),
there
is
nothing to test.

I need to find some other way to make it work or find a way to find
out
if
the initial page.

Tom

I know that that is also a problem and I am going to address that
problem
in a bit. But I needed to get the refresh problem taken care of
right
away.
The (almost) funny thing is that a guy has copied this article and
published it on Code Project site.
He also copied the bugs clearly ... :)

I agree.

But that allows someone else to at least get a starting point to
work
from.

Tom
Tommaso

tshad ha scritto:

I was able to get it to work after closer reading of the article.

*********************************************** ********************
Namespace MyFunctions

Public Class Page
Inherits System.Web.UI.Page
Private _refreshState As Boolean
Private _isRefresh As Boolean
Public ReadOnly Property IsRefresh() As Boolean
Get
Return _isRefresh
End Get
End Property
Protected Overrides Sub LoadViewState(savedState As Object)
Dim allStates As Object() = CType(savedState, Object())
MyBase.LoadViewState(allStates(0))
_refreshState = CBool(allStates(1))
_isRefresh = _refreshState = CBool(Session("__ISREFRESH"))
End Sub 'LoadViewState
Protected Overrides Function SaveViewState() As Object
Session("__ISREFRESH") = _refreshState
Dim allStates(2) As Object
allStates(0) = MyBase.SaveViewState()
allStates(1) = Not _refreshState
Return allStates
End Function 'SaveViewState

End Class 'Page

End NameSpace
********************************************* **

In my page, I have to add an inherits:

<%@ Page Language="VB" trace="true" ContentType="text/html"
ResponseEncoding="iso-8859-1" Inherits="MyFunctions.Page"%>
...
trace.warn("is Refresh = " & IsRefresh())

Then it works.

I am confused as to the code with the "allStates" and how it
works.

In LoadViewState:
Dim allStates As Object() = CType(savedState, Object())
MyBase.LoadViewState(allStates(0))
_refreshState = CBool(allStates(1))

It seems to be getting the savedState from the parameters list,
then
I
think
it does the normal LoadViewState passing allStates(0) (which is
really
just
savedState - so why not just pass savedState??).

Also, what is allStates(1)?

In SaveViewState:
Dim allStates(2) As Object
allStates(0) = MyBase.SaveViewState()
allStates(1) = Not _refreshState
Return allStates

I assume the MyBase.SaveViewState() is just doing the normal
SaveViewState
and passes back the changed ViewState into allStates(0), what is
allStates(1) and why are we passing back allStates - does this
overwrite
the
ViewState that was written out by MyBase.SaveViewState()?

Thanks,

Tom

<to**************@uniroma1.itwrote in message
news:11*********************@m73g2000cwd.google groups.com...
Hi Tom,

see the remarks here made by a User:

http://jarednevans.typepad.com/techn..._techno_b.html

this method seems to fail when back button is pressed. You can
download
a demo.

Tommaso

tshad ha scritto:

I was looking for a way to handle refreshes (user pressed
refresh
button)
and found a piece of code to check if a Web page was refreshed
but
I
can't
get it to work.

The code is:
*********************************************** *************
Namespace StevenBey.Web.UI

Public Class Page
Inherits System.Web.UI.Page
Private _refreshState As Boolean
Private _isRefresh As Boolean
Public ReadOnly Property IsRefresh() As Boolean
Get
Return _isRefresh
End Get
End Property
Protected Overrides Sub LoadViewState(savedState As Object)
Dim allStates As Object() = CType(savedState, Object())
MyBase.LoadViewState(allStates(0))
_refreshState = CBool(allStates(1))
_isRefresh = _refreshState = CBool(Session("__ISREFRESH"))
End Sub 'LoadViewState
Protected Overrides Function SaveViewState() As Object
Session("__ISREFRESH") = _refreshState
Dim allStates(2) As Object
allStates(0) = MyBase.SaveViewState()
allStates(1) = Not _refreshState
Return allStates
End Function 'SaveViewState
End Class 'Page

End NameSpace
*********************************************** *************

If I do a:

trace.warn("is Refresh = " & Page.IsRefresh)

or

trace.warn("is Refresh = " & IsRefresh)

I get the error:

BC30456 'IsRefresh' is not a member of
'System.Web.UI.Page'

I took the compiled version (StevenBey.Web.UI.dll) and put it
in
my
Bin
directory.

If you look at trace page you won't see __ISREFRESH?

Is there something else I need to do to get this to work?

Thanks,

Tom




Aug 26 '06 #11

P: n/a
Hi Tom,

Hmm, could that be due to some ASCX control on the page or does it
happen eve with an empty page? Here is some detail on ASP.NET page life
cycle.

http://www.c-sharpcorner.com/UploadF...d-6cb9a54593c5

Tommaso

tshad ha scritto:
Below.

"tshad" <ts**********@ftsolutions.comwrote in message
news:OB****************@TK2MSFTNGP04.phx.gbl...
<to**************@uniroma1.itwrote in message
news:11**********************@h48g2000cwc.googlegr oups.com...
Hi Tom,

I posted the wrong link. Here it is:

http://msdn.microsoft.com/library/de...rockaspnet.asp
I'll take a look at that to see what it says.
"Trap the Browser Refresh
In an article originally published on aspnetPRO Magazine several months
ago, I outlined the steps needed to detect when the user presses the F5
browser button to refresh the current page. The page refresh is the
browser's response to a specific user action-hitting the F5 key or
clicking the toolbar button. The page refresh action is a sort of
internal browser operation, for which the browser doesn't provide any
external notification

see also:
http://www.joel.net/code/refresh_capture.aspx
Already looking at this one.

I am looking at using ideas from his code to solve the problem from my
program dealing with the LoadViewState on the initial load problem.

I did find another problem that I didn't notice before with my code. I
didn't notice that my SaveViewState is being executed twice and I don't
know why. I put a trace statement in the function and found that it is
being called before the normal SaveViewState and during the SaveViewState.

aspx.page Begin Init
aspx.page End Init 0.000079 0.000079
aspx.page Begin PreRender 0.000116 0.000037
aspx.page End PreRender 0.000165 0.000049
Inside refresh.cs at SaveViewState 0.000880 0.000715
aspx.page Begin SaveViewState 0.001932 0.001053
Inside refresh.cs at SaveViewState 0.002330 0.000398
aspx.page End SaveViewState 0.002384 0.000054
aspx.page Begin Render 0.002411 0.000027
aspx.page End Render 0.137321 0.134909
Why would this happen?

I found that this happens if I have trace="true" on the page. If I have
trace="false", it works fine.

Setting trace="true" causes the SVS to fire a second time (I assume the
event that fires between the normal page events is from this).

Any idea why that is?

Thanks,

Tom

Thanks,

Tom
>
Let me know if you find a definitive solution...

Tommaso

tshad ha scritto:

<to**************@uniroma1.itwrote in message
news:11**********************@i3g2000cwc.googlegr oups.com...
Hi Tom,

I am afraid that is not exactly an "elementary" topic. Here is a more
authoritative source:

http://msdn.microsoft.com/msdnmag/is...8/CuttingEdge/

I looked there, but the article is about Script Callbacks.

That was interesting also, as I was looking at doing this (like Ajax)
but in
.net.

I knew you could do this in .net 1.1, but if what this is saying is
correct
then I can't do it.

"ASP.NET 2.0 provides a built-in mechanism for client callbacks based on
the
services of a COM object that ships with Internet Explorer 5.0 and
later. By
using the same object, you can implement a script callback mechanism in
ASP.NET 1.x as well"

I don't want to do it if you have to depend on the user using IE.

Thanks,

Tom
Let us know about the best solution... :)

Tommaso

tshad ha scritto:

"tshad" <ts**********@ftsolutions.comwrote in message
news:%2******************@TK2MSFTNGP05.phx.gbl. ..
<to**************@uniroma1.itwrote in message
news:11**********************@b28g2000cwb.googlegr oups.com...
But what about the back button problem.
I think that user was right. It doesn't work in that case.
Actually, I did some work with the program and found that it also
doesn't
work on the inital page. It only works for PostBacks.

Looking into it I found that since LoadViewState is not called on an
initial
page load (makes sense as there would be no ViewState saved here),
there
is
nothing to test.

I need to find some other way to make it work or find a way to find
out
if
the initial page.

Tom

I know that that is also a problem and I am going to address that
problem
in a bit. But I needed to get the refresh problem taken care of
right
away.
The (almost) funny thing is that a guy has copied this article and
published it on Code Project site.
He also copied the bugs clearly ... :)

I agree.

But that allows someone else to at least get a starting point to
work
from.

Tom
Tommaso

tshad ha scritto:

I was able to get it to work after closer reading of the article.

************************************************ *******************
Namespace MyFunctions

Public Class Page
Inherits System.Web.UI.Page
Private _refreshState As Boolean
Private _isRefresh As Boolean
Public ReadOnly Property IsRefresh() As Boolean
Get
Return _isRefresh
End Get
End Property
Protected Overrides Sub LoadViewState(savedState As Object)
Dim allStates As Object() = CType(savedState, Object())
MyBase.LoadViewState(allStates(0))
_refreshState = CBool(allStates(1))
_isRefresh = _refreshState = CBool(Session("__ISREFRESH"))
End Sub 'LoadViewState
Protected Overrides Function SaveViewState() As Object
Session("__ISREFRESH") = _refreshState
Dim allStates(2) As Object
allStates(0) = MyBase.SaveViewState()
allStates(1) = Not _refreshState
Return allStates
End Function 'SaveViewState

End Class 'Page

End NameSpace
********************************************** *

In my page, I have to add an inherits:

<%@ Page Language="VB" trace="true" ContentType="text/html"
ResponseEncoding="iso-8859-1" Inherits="MyFunctions.Page"%>
...
trace.warn("is Refresh = " & IsRefresh())

Then it works.

I am confused as to the code with the "allStates" and how it
works.

In LoadViewState:
Dim allStates As Object() = CType(savedState, Object())
MyBase.LoadViewState(allStates(0))
_refreshState = CBool(allStates(1))

It seems to be getting the savedState from the parameters list,
then
I
think
it does the normal LoadViewState passing allStates(0) (which is
really
just
savedState - so why not just pass savedState??).

Also, what is allStates(1)?

In SaveViewState:
Dim allStates(2) As Object
allStates(0) = MyBase.SaveViewState()
allStates(1) = Not _refreshState
Return allStates

I assume the MyBase.SaveViewState() is just doing the normal
SaveViewState
and passes back the changed ViewState into allStates(0), what is
allStates(1) and why are we passing back allStates - does this
overwrite
the
ViewState that was written out by MyBase.SaveViewState()?

Thanks,

Tom

<to**************@uniroma1.itwrote in message
news:11*********************@m73g2000cwd.googleg roups.com...
Hi Tom,

see the remarks here made by a User:

http://jarednevans.typepad.com/techn..._techno_b.html

this method seems to fail when back button is pressed. You can
download
a demo.

Tommaso

tshad ha scritto:

I was looking for a way to handle refreshes (user pressed
refresh
button)
and found a piece of code to check if a Web page was refreshed
but
I
can't
get it to work.

The code is:
************************************************ ************
Namespace StevenBey.Web.UI

Public Class Page
Inherits System.Web.UI.Page
Private _refreshState As Boolean
Private _isRefresh As Boolean
Public ReadOnly Property IsRefresh() As Boolean
Get
Return _isRefresh
End Get
End Property
Protected Overrides Sub LoadViewState(savedState As Object)
Dim allStates As Object() = CType(savedState, Object())
MyBase.LoadViewState(allStates(0))
_refreshState = CBool(allStates(1))
_isRefresh = _refreshState = CBool(Session("__ISREFRESH"))
End Sub 'LoadViewState
Protected Overrides Function SaveViewState() As Object
Session("__ISREFRESH") = _refreshState
Dim allStates(2) As Object
allStates(0) = MyBase.SaveViewState()
allStates(1) = Not _refreshState
Return allStates
End Function 'SaveViewState
End Class 'Page

End NameSpace
************************************************ ************

If I do a:

trace.warn("is Refresh = " & Page.IsRefresh)

or

trace.warn("is Refresh = " & IsRefresh)

I get the error:

BC30456 'IsRefresh' is not a member of
'System.Web.UI.Page'

I took the compiled version (StevenBey.Web.UI.dll) and put it
in
my
Bin
directory.

If you look at trace page you won't see __ISREFRESH?

Is there something else I need to do to get this to work?

Thanks,

Tom

Aug 26 '06 #12

P: n/a

<to**************@uniroma1.itwrote in message
news:11**********************@m73g2000cwd.googlegr oups.com...
Hi Tom,

Hmm, could that be due to some ASCX control on the page or does it
happen eve with an empty page? Here is some detail on ASP.NET page life
cycle.
This happens on an empty page. Took a while to zero in on that. The
interesting thing is that the first SVS is called between events (at least
the events that are displayed in the trace page). If you turn trace off, it
only calls it once.

My question is why would this be called twice??? Trace seems to be calling
it. But why would it be doing this and what call would cause this to
happen?
>
http://www.c-sharpcorner.com/UploadF...d-6cb9a54593c5
Good article. I have been looking at a few articles on the Page Life cycle
of late and understand it pretty well now. But this seems to be happening
outside of the normal events.

Thanks,

Tom
Tommaso

tshad ha scritto:
>Below.

"tshad" <ts**********@ftsolutions.comwrote in message
news:OB****************@TK2MSFTNGP04.phx.gbl...
<to**************@uniroma1.itwrote in message
news:11**********************@h48g2000cwc.googlegr oups.com...
Hi Tom,

I posted the wrong link. Here it is:

http://msdn.microsoft.com/library/de...rockaspnet.asp

I'll take a look at that to see what it says.

"Trap the Browser Refresh
In an article originally published on aspnetPRO Magazine several
months
ago, I outlined the steps needed to detect when the user presses the
F5
browser button to refresh the current page. The page refresh is the
browser's response to a specific user action-hitting the F5 key or
clicking the toolbar button. The page refresh action is a sort of
internal browser operation, for which the browser doesn't provide any
external notification

see also:
http://www.joel.net/code/refresh_capture.aspx

Already looking at this one.

I am looking at using ideas from his code to solve the problem from my
program dealing with the LoadViewState on the initial load problem.

I did find another problem that I didn't notice before with my code. I
didn't notice that my SaveViewState is being executed twice and I don't
know why. I put a trace statement in the function and found that it is
being called before the normal SaveViewState and during the
SaveViewState.

aspx.page Begin Init
aspx.page End Init 0.000079 0.000079
aspx.page Begin PreRender 0.000116 0.000037
aspx.page End PreRender 0.000165 0.000049
Inside refresh.cs at SaveViewState 0.000880 0.000715
aspx.page Begin SaveViewState 0.001932 0.001053
Inside refresh.cs at SaveViewState 0.002330 0.000398
aspx.page End SaveViewState 0.002384 0.000054
aspx.page Begin Render 0.002411 0.000027
aspx.page End Render 0.137321 0.134909
Why would this happen?

I found that this happens if I have trace="true" on the page. If I have
trace="false", it works fine.

Setting trace="true" causes the SVS to fire a second time (I assume the
event that fires between the normal page events is from this).

Any idea why that is?

Thanks,

Tom
>
Thanks,

Tom

Let me know if you find a definitive solution...

Tommaso

tshad ha scritto:

<to**************@uniroma1.itwrote in message
news:11**********************@i3g2000cwc.googleg roups.com...
Hi Tom,

I am afraid that is not exactly an "elementary" topic. Here is a
more
authoritative source:

http://msdn.microsoft.com/msdnmag/is...8/CuttingEdge/

I looked there, but the article is about Script Callbacks.

That was interesting also, as I was looking at doing this (like Ajax)
but in
.net.

I knew you could do this in .net 1.1, but if what this is saying is
correct
then I can't do it.

"ASP.NET 2.0 provides a built-in mechanism for client callbacks based
on
the
services of a COM object that ships with Internet Explorer 5.0 and
later. By
using the same object, you can implement a script callback mechanism
in
ASP.NET 1.x as well"

I don't want to do it if you have to depend on the user using IE.

Thanks,

Tom
Let us know about the best solution... :)

Tommaso

tshad ha scritto:

"tshad" <ts**********@ftsolutions.comwrote in message
news:%2******************@TK2MSFTNGP05.phx.gbl.. .
<to**************@uniroma1.itwrote in message
news:11**********************@b28g2000cwb.googleg roups.com...
But what about the back button problem.
I think that user was right. It doesn't work in that case.
Actually, I did some work with the program and found that it also
doesn't
work on the inital page. It only works for PostBacks.

Looking into it I found that since LoadViewState is not called on
an
initial
page load (makes sense as there would be no ViewState saved here),
there
is
nothing to test.

I need to find some other way to make it work or find a way to
find
out
if
the initial page.

Tom

I know that that is also a problem and I am going to address
that
problem
in a bit. But I needed to get the refresh problem taken care of
right
away.
The (almost) funny thing is that a guy has copied this article
and
published it on Code Project site.
He also copied the bugs clearly ... :)

I agree.

But that allows someone else to at least get a starting point to
work
from.

Tom
Tommaso

tshad ha scritto:

I was able to get it to work after closer reading of the
article.

*********************************************** ********************
Namespace MyFunctions

Public Class Page
Inherits System.Web.UI.Page
Private _refreshState As Boolean
Private _isRefresh As Boolean
Public ReadOnly Property IsRefresh() As Boolean
Get
Return _isRefresh
End Get
End Property
Protected Overrides Sub LoadViewState(savedState As Object)
Dim allStates As Object() = CType(savedState, Object())
MyBase.LoadViewState(allStates(0))
_refreshState = CBool(allStates(1))
_isRefresh = _refreshState = CBool(Session("__ISREFRESH"))
End Sub 'LoadViewState
Protected Overrides Function SaveViewState() As Object
Session("__ISREFRESH") = _refreshState
Dim allStates(2) As Object
allStates(0) = MyBase.SaveViewState()
allStates(1) = Not _refreshState
Return allStates
End Function 'SaveViewState

End Class 'Page

End NameSpace
********************************************* **

In my page, I have to add an inherits:

<%@ Page Language="VB" trace="true" ContentType="text/html"
ResponseEncoding="iso-8859-1" Inherits="MyFunctions.Page"%>
...
trace.warn("is Refresh = " & IsRefresh())

Then it works.

I am confused as to the code with the "allStates" and how it
works.

In LoadViewState:
Dim allStates As Object() = CType(savedState, Object())
MyBase.LoadViewState(allStates(0))
_refreshState = CBool(allStates(1))

It seems to be getting the savedState from the parameters
list,
then
I
think
it does the normal LoadViewState passing allStates(0) (which
is
really
just
savedState - so why not just pass savedState??).

Also, what is allStates(1)?

In SaveViewState:
Dim allStates(2) As Object
allStates(0) = MyBase.SaveViewState()
allStates(1) = Not _refreshState
Return allStates

I assume the MyBase.SaveViewState() is just doing the normal
SaveViewState
and passes back the changed ViewState into allStates(0), what
is
allStates(1) and why are we passing back allStates - does this
overwrite
the
ViewState that was written out by MyBase.SaveViewState()?

Thanks,

Tom

<to**************@uniroma1.itwrote in message
news:11*********************@m73g2000cwd.google groups.com...
Hi Tom,

see the remarks here made by a User:

http://jarednevans.typepad.com/techn..._techno_b.html

this method seems to fail when back button is pressed. You
can
download
a demo.

Tommaso

tshad ha scritto:

I was looking for a way to handle refreshes (user pressed
refresh
button)
and found a piece of code to check if a Web page was
refreshed
but
I
can't
get it to work.

The code is:
*********************************************** *************
Namespace StevenBey.Web.UI

Public Class Page
Inherits System.Web.UI.Page
Private _refreshState As Boolean
Private _isRefresh As Boolean
Public ReadOnly Property IsRefresh() As Boolean
Get
Return _isRefresh
End Get
End Property
Protected Overrides Sub LoadViewState(savedState As
Object)
Dim allStates As Object() = CType(savedState, Object())
MyBase.LoadViewState(allStates(0))
_refreshState = CBool(allStates(1))
_isRefresh = _refreshState =
CBool(Session("__ISREFRESH"))
End Sub 'LoadViewState
Protected Overrides Function SaveViewState() As Object
Session("__ISREFRESH") = _refreshState
Dim allStates(2) As Object
allStates(0) = MyBase.SaveViewState()
allStates(1) = Not _refreshState
Return allStates
End Function 'SaveViewState
End Class 'Page

End NameSpace
*********************************************** *************

If I do a:

trace.warn("is Refresh = " & Page.IsRefresh)

or

trace.warn("is Refresh = " & IsRefresh)

I get the error:

BC30456 'IsRefresh' is not a member of
'System.Web.UI.Page'

I took the compiled version (StevenBey.Web.UI.dll) and put
it
in
my
Bin
directory.

If you look at trace page you won't see __ISREFRESH?

Is there something else I need to do to get this to work?

Thanks,

Tom




Aug 28 '06 #13

P: n/a
>
Let me know if you find a definitive solution...
Finally got it to work.

I used as my basic code the code from
http://jarednevans.typepad.com/techn..._techno_b.html.

The problem with this code is it doesn't take into account the Initial page
load being refreshed, which may not be a problem most of the time. But if
you do any database processing, setting session variables etc. - this could
be a problem.

What I added was the setting of a session variable to the URL and when I
enter the page, if the page is NOT a Post Backed page I compare the current
URL with what is in the Session variable. If they don't match or the
Session Variable is not there, then this is not a refresh. If it is there,
then the page was either refreshed or they just reentered the URL in the
Browsers Address line (really the same thing).

I also found that SVS (SaveViewState) is executed twice if you have tracing
on (trace="true"). You'll notice the first SVS is done between events (at
least the events that display on the trace page). I assume this one is the
being executed by Trace in some way because right after that the
SaveViewState is executed. If trace is off, you only get the SVS.

Also, the trace statement is the only thing that is in my SVS call. So
something is calling my SVS event other than the Page SVS and there are no
controls on the page.

aspx.page Begin Init
aspx.page End Init 0.000079 0.000079
aspx.page Begin PreRender 0.000116 0.000037
aspx.page End PreRender 0.000165 0.000049
Inside refresh.cs at SaveViewState 0.000880 0.000715
aspx.page Begin SaveViewState 0.001932 0.001053
Inside refresh.cs at SaveViewState 0.002330 0.000398
aspx.page End SaveViewState 0.002384 0.000054
aspx.page Begin Render 0.002411 0.000027
aspx.page End Render 0.137321 0.134909

So what I do set a variable, _firstTime, that I set to false in the 1st SVS
so that the 2nd SVS will execute normally but won't set anything.

To make this work for only specific pages, you need to add
Inherits="MyFunctions.Page" to the Page tag on the page:

<%@ Page Language="VB" trace="true" debug="true" ContentType="text/html"
ResponseEncoding="iso-8859-1" Inherits="MyFunctions.Page" %>

or if you want this to work for every page I believe you can set it in your
Web.Config file:

<pages Inherits="MyFunctions.Page" />

The one problem I have not been able figure out (if you are only doing this
for specific pages) is when you load a page (which would set the Session
variable) then go directly another set of pages not using this procedure.
If you then go back to the 1st page, it will set the session variable set
and think this is a refresh, when in fact it isn't.

Here is the actual code (in c# as that is what I created this in, but then I
converted it to vb for here).
************************************************** *************************
using System;
using System.IO;
using System.Web;
using System.Web.UI;
using System.Web.SessionState;

namespace MyFunctions
{

// This program deals with 2 situations of refresh.
// Initial load of a page
// Checks to see if this is a PostBack.
// If not - Checks to see if Session("__LASTPAGEPROCESSED") matches the
current URL.
// If it does, the page has either been refreshed or re-entered in the
address line of Browser
// PostBack
// Checks to see if _refreshState that is stored in the ViewState matches
Session["__ISREFRESH"].
// if it does, we have a refresh. This is because we set the Session
variable and put the reverse
// value in the viewstate. If we refresh the page we get the old
viewstate back and the old viewstate
// value should match our new Session value. Remember we keep reversing
the values.

public class Page : System.Web.UI.Page
{
private bool _refreshState;
private bool _isRefresh;

// This variable is only important to signify whether SaveViewState is
executing for a second time
// During the same Page Life Cycle. Have only seen this when you have
tracing on (Trace="true")

private bool _firstTime = true;

public bool IsRefresh
{
get
{
if (!IsPostBack)
{
if ((Session["__INITIALREFRESHPAGE"] != null) &&
((string)Session["__INITIALREFRESHPAGE"] ==
HttpContext.Current.Request.Url.ToString()))
{
// Need to set this here as this is a refresh or was re-executed from
the URL line.
// In either case, the results are the same.
_isRefresh = true;
}
}
return _isRefresh;
}
}

public bool IsFirstTime
{
get
{
return _firstTime;
}
set
{
_firstTime = value;
}
}

protected override void LoadViewState(object savedState)
{
object[] allStates = (object[]) savedState;
base.LoadViewState(allStates[0]);
_refreshState = (bool) allStates[1];
_isRefresh = _refreshState == (bool) Session["__ISREFRESH"];
Session.Remove("__INITIALREFRESHPAGE"); // Only there to test initial
page load refresh
}

protected override object SaveViewState()
{
object[] allStates = new object[2];
allStates[0] = base.SaveViewState();

// This test is mainly when trace="true". SaveViewState will execute twice
and we only need to process this once

if (_firstTime)
{
// This only happens on the initial load of a page where there is no
LoadViewState
if (!IsPostBack)
{
Session["__INITIALREFRESHPAGE"] =
HttpContext.Current.Request.Url.ToString();
}
Session["__ISREFRESH"] = _refreshState;
allStates[1] = !_refreshState;
}
else
{
// We already reversed _refresh states. To do it again would put it back
the way it was.
// Need to store the _refreshState anyway as we need to return the
ViewState.

allStates[1] = !_refreshState;
}
_firstTime = false;
return allStates;
}
}
}
************************************************** **************************

Here is the converted VB code (but I haven't tested it)
************************************************** ***************
Imports System
Imports System.IO
Imports System.Web
Imports System.Web.UI
Imports System.Web.SessionState

' This program deals with 2 situations of refresh.
' Initial load of a page
' Checks to see if this is a PostBack.
' If not - Checks to see if Session("__LASTPAGEPROCESSED") matches the
current URL.
' If it does, the page has either been refreshed or re-entered in the
address line of Browser
' PostBack
' Checks to see if _refreshState that is stored in the ViewState matches
Session["__ISREFRESH"].
' if it does, we have a refresh. This is because we set the Session
variable and put the reverse
' value in the viewstate. If we refresh the page we get the old viewstate
back and the old viewstate
' value should match our new Session value. Remember we keep reversing
the values.
Public Class Page
Inherits System.Web.UI.Page
Private _refreshState As Boolean
Private _isRefresh As Boolean

' This variable is only important to signify whether SaveViewState is
executing for a second time
' During the same Page Life Cycle. Have only seen this when you have
tracing on (Trace="true")
Private _firstTime As Boolean = True

Public ReadOnly Property IsRefresh() As Boolean
Get
If Not IsPostBack Then
If Not (Session("__INITIALREFRESHPAGE") Is Nothing) And
CStr(Session("__INITIALREFRESHPAGE")) =
HttpContext.Current.Request.Url.ToString() Then
' Need to set this here as this is a refresh or was
re-executed from the URL line.
' In either case, the results are the same.
_isRefresh = True
End If
End If
Return _isRefresh
End Get
End Property

Public Property IsFirstTime() As Boolean
Get
Return _firstTime
End Get
Set
_firstTime = value
End Set
End Property

Protected Overrides Sub LoadViewState(savedState As Object)
Dim allStates As Object() = CType(savedState, Object())
MyBase.LoadViewState(allStates(0))
_refreshState = CBool(allStates(1))
_isRefresh = _refreshState = CBool(Session("__ISREFRESH"))
Session.Remove("__INITIALREFRESHPAGE") ' Only there to test initial
page load refresh
End Sub 'LoadViewState

Protected Overrides Function SaveViewState() As Object
Dim allStates(2) As Object
allStates(0) = MyBase.SaveViewState()

' This test is mainly when trace="true". SaveViewState will execute
twice and we only need to process this once
If _firstTime Then
' This only happens on the initial load of a page where there is
no LoadViewState
If Not IsPostBack Then
Session("__INITIALREFRESHPAGE") =
HttpContext.Current.Request.Url.ToString()
End If
Session("__ISREFRESH") = _refreshState
allStates(1) = Not _refreshState
Else
' We already reversed _refresh states. To do it again would put
it back the way it was.
' Need to store the _refreshState anyway as we need to return the
ViewState.
allStates(1) = Not _refreshState
End If
_firstTime = False
Return allStates
End Function 'SaveViewState
End Class 'Page
************************************************** ***************************

In your code, you only have to check the property:

if IsRefresh...

Seems to work OK, except for the one problem I stated.

Someone mentioned a problem with the backbutton in the other program, but I
am not dealing with that here - just a refresh.

Tom
Tommaso

tshad ha scritto:
><to**************@uniroma1.itwrote in message
news:11**********************@i3g2000cwc.googlegr oups.com...
Hi Tom,

I am afraid that is not exactly an "elementary" topic. Here is a more
authoritative source:

http://msdn.microsoft.com/msdnmag/is...8/CuttingEdge/

I looked there, but the article is about Script Callbacks.

That was interesting also, as I was looking at doing this (like Ajax) but
in
.net.

I knew you could do this in .net 1.1, but if what this is saying is
correct
then I can't do it.

"ASP.NET 2.0 provides a built-in mechanism for client callbacks based on
the
services of a COM object that ships with Internet Explorer 5.0 and later.
By
using the same object, you can implement a script callback mechanism in
ASP.NET 1.x as well"

I don't want to do it if you have to depend on the user using IE.

Thanks,

Tom
>
Let us know about the best solution... :)

Tommaso

tshad ha scritto:

"tshad" <ts**********@ftsolutions.comwrote in message
news:%2******************@TK2MSFTNGP05.phx.gbl. ..
<to**************@uniroma1.itwrote in message
news:11**********************@b28g2000cwb.googlegr oups.com...
But what about the back button problem.
I think that user was right. It doesn't work in that case.
Actually, I did some work with the program and found that it also
doesn't
work on the inital page. It only works for PostBacks.

Looking into it I found that since LoadViewState is not called on an
initial
page load (makes sense as there would be no ViewState saved here),
there
is
nothing to test.

I need to find some other way to make it work or find a way to find
out
if
the initial page.

Tom

I know that that is also a problem and I am going to address that
problem
in a bit. But I needed to get the refresh problem taken care of
right
away.
The (almost) funny thing is that a guy has copied this article and
published it on Code Project site.
He also copied the bugs clearly ... :)

I agree.

But that allows someone else to at least get a starting point to
work
from.

Tom
Tommaso

tshad ha scritto:

I was able to get it to work after closer reading of the article.

************************************************ *******************
Namespace MyFunctions

Public Class Page
Inherits System.Web.UI.Page
Private _refreshState As Boolean
Private _isRefresh As Boolean
Public ReadOnly Property IsRefresh() As Boolean
Get
Return _isRefresh
End Get
End Property
Protected Overrides Sub LoadViewState(savedState As Object)
Dim allStates As Object() = CType(savedState, Object())
MyBase.LoadViewState(allStates(0))
_refreshState = CBool(allStates(1))
_isRefresh = _refreshState = CBool(Session("__ISREFRESH"))
End Sub 'LoadViewState
Protected Overrides Function SaveViewState() As Object
Session("__ISREFRESH") = _refreshState
Dim allStates(2) As Object
allStates(0) = MyBase.SaveViewState()
allStates(1) = Not _refreshState
Return allStates
End Function 'SaveViewState

End Class 'Page

End NameSpace
********************************************** *

In my page, I have to add an inherits:

<%@ Page Language="VB" trace="true" ContentType="text/html"
ResponseEncoding="iso-8859-1" Inherits="MyFunctions.Page"%>
...
trace.warn("is Refresh = " & IsRefresh())

Then it works.

I am confused as to the code with the "allStates" and how it
works.

In LoadViewState:
Dim allStates As Object() = CType(savedState, Object())
MyBase.LoadViewState(allStates(0))
_refreshState = CBool(allStates(1))

It seems to be getting the savedState from the parameters list,
then
I
think
it does the normal LoadViewState passing allStates(0) (which is
really
just
savedState - so why not just pass savedState??).

Also, what is allStates(1)?

In SaveViewState:
Dim allStates(2) As Object
allStates(0) = MyBase.SaveViewState()
allStates(1) = Not _refreshState
Return allStates

I assume the MyBase.SaveViewState() is just doing the normal
SaveViewState
and passes back the changed ViewState into allStates(0), what is
allStates(1) and why are we passing back allStates - does this
overwrite
the
ViewState that was written out by MyBase.SaveViewState()?

Thanks,

Tom

<to**************@uniroma1.itwrote in message
news:11*********************@m73g2000cwd.googleg roups.com...
Hi Tom,

see the remarks here made by a User:

http://jarednevans.typepad.com/techn..._techno_b.html

this method seems to fail when back button is pressed. You can
download
a demo.

Tommaso

tshad ha scritto:

I was looking for a way to handle refreshes (user pressed
refresh
button)
and found a piece of code to check if a Web page was refreshed
but
I
can't
get it to work.

The code is:
************************************************ ************
Namespace StevenBey.Web.UI

Public Class Page
Inherits System.Web.UI.Page
Private _refreshState As Boolean
Private _isRefresh As Boolean
Public ReadOnly Property IsRefresh() As Boolean
Get
Return _isRefresh
End Get
End Property
Protected Overrides Sub LoadViewState(savedState As Object)
Dim allStates As Object() = CType(savedState, Object())
MyBase.LoadViewState(allStates(0))
_refreshState = CBool(allStates(1))
_isRefresh = _refreshState = CBool(Session("__ISREFRESH"))
End Sub 'LoadViewState
Protected Overrides Function SaveViewState() As Object
Session("__ISREFRESH") = _refreshState
Dim allStates(2) As Object
allStates(0) = MyBase.SaveViewState()
allStates(1) = Not _refreshState
Return allStates
End Function 'SaveViewState
End Class 'Page

End NameSpace
************************************************ ************

If I do a:

trace.warn("is Refresh = " & Page.IsRefresh)

or

trace.warn("is Refresh = " & IsRefresh)

I get the error:

BC30456 'IsRefresh' is not a member of
'System.Web.UI.Page'

I took the compiled version (StevenBey.Web.UI.dll) and put it
in
my
Bin
directory.

If you look at trace page you won't see __ISREFRESH?

Is there something else I need to do to get this to work?

Thanks,

Tom



Aug 29 '06 #14

P: n/a
Thanks Tom for sharing your efforts. You have done a lot of research
on this subject! Actually you could write an article on that and place
it on a website: I am sure many programmers would like it and could be
a source of further suggestion.

In case you do, let us know the URL :)

Thanks you very much,

Tommaso

tshad ha scritto:

Let me know if you find a definitive solution...
Finally got it to work.

I used as my basic code the code from
http://jarednevans.typepad.com/techn..._techno_b.html.

The problem with this code is it doesn't take into account the Initial page
load being refreshed, which may not be a problem most of the time. But if
you do any database processing, setting session variables etc. - this could
be a problem.

What I added was the setting of a session variable to the URL and when I
enter the page, if the page is NOT a Post Backed page I compare the current
URL with what is in the Session variable. If they don't match or the
Session Variable is not there, then this is not a refresh. If it is there,
then the page was either refreshed or they just reentered the URL in the
Browsers Address line (really the same thing).

I also found that SVS (SaveViewState) is executed twice if you have tracing
on (trace="true"). You'll notice the first SVS is done between events (at
least the events that display on the trace page). I assume this one is the
being executed by Trace in some way because right after that the
SaveViewState is executed. If trace is off, you only get the SVS.

Also, the trace statement is the only thing that is in my SVS call. So
something is calling my SVS event other than the Page SVS and there are no
controls on the page.

aspx.page Begin Init
aspx.page End Init 0.000079 0.000079
aspx.page Begin PreRender 0.000116 0.000037
aspx.page End PreRender 0.000165 0.000049
Inside refresh.cs at SaveViewState 0.000880 0.000715
aspx.page Begin SaveViewState 0.001932 0.001053
Inside refresh.cs at SaveViewState 0.002330 0.000398
aspx.page End SaveViewState 0.002384 0.000054
aspx.page Begin Render 0.002411 0.000027
aspx.page End Render 0.137321 0.134909

So what I do set a variable, _firstTime, that I set to false in the 1st SVS
so that the 2nd SVS will execute normally but won't set anything.

To make this work for only specific pages, you need to add
Inherits="MyFunctions.Page" to the Page tag on the page:

<%@ Page Language="VB" trace="true" debug="true" ContentType="text/html"
ResponseEncoding="iso-8859-1" Inherits="MyFunctions.Page" %>

or if you want this to work for every page I believe you can set it in your
Web.Config file:

<pages Inherits="MyFunctions.Page" />

The one problem I have not been able figure out (if you are only doing this
for specific pages) is when you load a page (which would set the Session
variable) then go directly another set of pages not using this procedure.
If you then go back to the 1st page, it will set the session variable set
and think this is a refresh, when in fact it isn't.

Here is the actual code (in c# as that is what I created this in, but then I
converted it to vb for here).
************************************************** *************************
using System;
using System.IO;
using System.Web;
using System.Web.UI;
using System.Web.SessionState;

namespace MyFunctions
{

// This program deals with 2 situations of refresh.
// Initial load of a page
// Checks to see if this is a PostBack.
// If not - Checks to see if Session("__LASTPAGEPROCESSED") matches the
current URL.
// If it does, the page has either been refreshed or re-entered in the
address line of Browser
// PostBack
// Checks to see if _refreshState that is stored in the ViewState matches
Session["__ISREFRESH"].
// if it does, we have a refresh. This is because we set the Session
variable and put the reverse
// value in the viewstate. If we refresh the page we get the old
viewstate back and the old viewstate
// value should match our new Session value. Remember we keep reversing
the values.

public class Page : System.Web.UI.Page
{
private bool _refreshState;
private bool _isRefresh;

// This variable is only important to signify whether SaveViewState is
executing for a second time
// During the same Page Life Cycle. Have only seen this when you have
tracing on (Trace="true")

private bool _firstTime = true;

public bool IsRefresh
{
get
{
if (!IsPostBack)
{
if ((Session["__INITIALREFRESHPAGE"] != null) &&
((string)Session["__INITIALREFRESHPAGE"] ==
HttpContext.Current.Request.Url.ToString()))
{
// Need to set this here as this is a refresh or was re-executed from
the URL line.
// In either case, the results are the same.
_isRefresh = true;
}
}
return _isRefresh;
}
}

public bool IsFirstTime
{
get
{
return _firstTime;
}
set
{
_firstTime = value;
}
}

protected override void LoadViewState(object savedState)
{
object[] allStates = (object[]) savedState;
base.LoadViewState(allStates[0]);
_refreshState = (bool) allStates[1];
_isRefresh = _refreshState == (bool) Session["__ISREFRESH"];
Session.Remove("__INITIALREFRESHPAGE"); // Only there to test initial
page load refresh
}

protected override object SaveViewState()
{
object[] allStates = new object[2];
allStates[0] = base.SaveViewState();

// This test is mainly when trace="true". SaveViewState will execute twice
and we only need to process this once

if (_firstTime)
{
// This only happens on the initial load of a page where there is no
LoadViewState
if (!IsPostBack)
{
Session["__INITIALREFRESHPAGE"] =
HttpContext.Current.Request.Url.ToString();
}
Session["__ISREFRESH"] = _refreshState;
allStates[1] = !_refreshState;
}
else
{
// We already reversed _refresh states. To do it again would put it back
the way it was.
// Need to store the _refreshState anyway as we need to return the
ViewState.

allStates[1] = !_refreshState;
}
_firstTime = false;
return allStates;
}
}
}
************************************************** **************************

Here is the converted VB code (but I haven't tested it)
************************************************** ***************
Imports System
Imports System.IO
Imports System.Web
Imports System.Web.UI
Imports System.Web.SessionState

' This program deals with 2 situations of refresh.
' Initial load of a page
' Checks to see if this is a PostBack.
' If not - Checks to see if Session("__LASTPAGEPROCESSED") matches the
current URL.
' If it does, the page has either been refreshed or re-entered in the
address line of Browser
' PostBack
' Checks to see if _refreshState that is stored in the ViewState matches
Session["__ISREFRESH"].
' if it does, we have a refresh. This is because we set the Session
variable and put the reverse
' value in the viewstate. If we refresh the page we get the old viewstate
back and the old viewstate
' value should match our new Session value. Remember we keep reversing
the values.
Public Class Page
Inherits System.Web.UI.Page
Private _refreshState As Boolean
Private _isRefresh As Boolean

' This variable is only important to signify whether SaveViewState is
executing for a second time
' During the same Page Life Cycle. Have only seen this when you have
tracing on (Trace="true")
Private _firstTime As Boolean = True

Public ReadOnly Property IsRefresh() As Boolean
Get
If Not IsPostBack Then
If Not (Session("__INITIALREFRESHPAGE") Is Nothing) And
CStr(Session("__INITIALREFRESHPAGE")) =
HttpContext.Current.Request.Url.ToString() Then
' Need to set this here as this is a refresh or was
re-executed from the URL line.
' In either case, the results are the same.
_isRefresh = True
End If
End If
Return _isRefresh
End Get
End Property

Public Property IsFirstTime() As Boolean
Get
Return _firstTime
End Get
Set
_firstTime = value
End Set
End Property

Protected Overrides Sub LoadViewState(savedState As Object)
Dim allStates As Object() = CType(savedState, Object())
MyBase.LoadViewState(allStates(0))
_refreshState = CBool(allStates(1))
_isRefresh = _refreshState = CBool(Session("__ISREFRESH"))
Session.Remove("__INITIALREFRESHPAGE") ' Only there to test initial
page load refresh
End Sub 'LoadViewState

Protected Overrides Function SaveViewState() As Object
Dim allStates(2) As Object
allStates(0) = MyBase.SaveViewState()

' This test is mainly when trace="true". SaveViewState will execute
twice and we only need to process this once
If _firstTime Then
' This only happens on the initial load of a page where there is
no LoadViewState
If Not IsPostBack Then
Session("__INITIALREFRESHPAGE") =
HttpContext.Current.Request.Url.ToString()
End If
Session("__ISREFRESH") = _refreshState
allStates(1) = Not _refreshState
Else
' We already reversed _refresh states. To do it again would put
it back the way it was.
' Need to store the _refreshState anyway as we need to return the
ViewState.
allStates(1) = Not _refreshState
End If
_firstTime = False
Return allStates
End Function 'SaveViewState
End Class 'Page
************************************************** ***************************

In your code, you only have to check the property:

if IsRefresh...

Seems to work OK, except for the one problem I stated.

Someone mentioned a problem with the backbutton in the other program, but I
am not dealing with that here - just a refresh.

Tom
Tommaso

tshad ha scritto:
<to**************@uniroma1.itwrote in message
news:11**********************@i3g2000cwc.googlegro ups.com...
Hi Tom,

I am afraid that is not exactly an "elementary" topic. Here is a more
authoritative source:

http://msdn.microsoft.com/msdnmag/is...8/CuttingEdge/

I looked there, but the article is about Script Callbacks.

That was interesting also, as I was looking at doing this (like Ajax) but
in
.net.

I knew you could do this in .net 1.1, but if what this is saying is
correct
then I can't do it.

"ASP.NET 2.0 provides a built-in mechanism for client callbacks based on
the
services of a COM object that ships with Internet Explorer 5.0 and later.
By
using the same object, you can implement a script callback mechanism in
ASP.NET 1.x as well"

I don't want to do it if you have to depend on the user using IE.

Thanks,

Tom


Let us know about the best solution... :)

Tommaso

tshad ha scritto:

"tshad" <ts**********@ftsolutions.comwrote in message
news:%2******************@TK2MSFTNGP05.phx.gbl...
<to**************@uniroma1.itwrote in message
news:11**********************@b28g2000cwb.googlegr oups.com...
But what about the back button problem.
I think that user was right. It doesn't work in that case.


Actually, I did some work with the program and found that it also
doesn't
work on the inital page. It only works for PostBacks.

Looking into it I found that since LoadViewState is not called on an
initial
page load (makes sense as there would be no ViewState saved here),
there
is
nothing to test.

I need to find some other way to make it work or find a way to find
out
if
the initial page.

Tom

I know that that is also a problem and I am going to address that
problem
in a bit. But I needed to get the refresh problem taken care of
right
away.


The (almost) funny thing is that a guy has copied this article and
published it on Code Project site.
He also copied the bugs clearly ... :)

I agree.

But that allows someone else to at least get a starting point to
work
from.

Tom


Tommaso

tshad ha scritto:

I was able to get it to work after closer reading of the article.

************************************************* ******************
Namespace MyFunctions

Public Class Page
Inherits System.Web.UI.Page
Private _refreshState As Boolean
Private _isRefresh As Boolean
Public ReadOnly Property IsRefresh() As Boolean
Get
Return _isRefresh
End Get
End Property
Protected Overrides Sub LoadViewState(savedState As Object)
Dim allStates As Object() = CType(savedState, Object())
MyBase.LoadViewState(allStates(0))
_refreshState = CBool(allStates(1))
_isRefresh = _refreshState = CBool(Session("__ISREFRESH"))
End Sub 'LoadViewState
Protected Overrides Function SaveViewState() As Object
Session("__ISREFRESH") = _refreshState
Dim allStates(2) As Object
allStates(0) = MyBase.SaveViewState()
allStates(1) = Not _refreshState
Return allStates
End Function 'SaveViewState

End Class 'Page

End NameSpace
***********************************************

In my page, I have to add an inherits:

<%@ Page Language="VB" trace="true" ContentType="text/html"
ResponseEncoding="iso-8859-1" Inherits="MyFunctions.Page"%>
...
trace.warn("is Refresh = " & IsRefresh())

Then it works.

I am confused as to the code with the "allStates" and how it
works.

In LoadViewState:
Dim allStates As Object() = CType(savedState, Object())
MyBase.LoadViewState(allStates(0))
_refreshState = CBool(allStates(1))

It seems to be getting the savedState from the parameters list,
then
I
think
it does the normal LoadViewState passing allStates(0) (which is
really
just
savedState - so why not just pass savedState??).

Also, what is allStates(1)?

In SaveViewState:
Dim allStates(2) As Object
allStates(0) = MyBase.SaveViewState()
allStates(1) = Not _refreshState
Return allStates

I assume the MyBase.SaveViewState() is just doing the normal
SaveViewState
and passes back the changed ViewState into allStates(0), what is
allStates(1) and why are we passing back allStates - does this
overwrite
the
ViewState that was written out by MyBase.SaveViewState()?

Thanks,

Tom

<to**************@uniroma1.itwrote in message
news:11*********************@m73g2000cwd.googlegr oups.com...
Hi Tom,

see the remarks here made by a User:

http://jarednevans.typepad.com/techn..._techno_b.html

this method seems to fail when back button is pressed. You can
download
a demo.

Tommaso

tshad ha scritto:

I was looking for a way to handle refreshes (user pressed
refresh
button)
and found a piece of code to check if a Web page was refreshed
but
I
can't
get it to work.

The code is:
************************************************* ***********
Namespace StevenBey.Web.UI

Public Class Page
Inherits System.Web.UI.Page
Private _refreshState As Boolean
Private _isRefresh As Boolean
Public ReadOnly Property IsRefresh() As Boolean
Get
Return _isRefresh
End Get
End Property
Protected Overrides Sub LoadViewState(savedState As Object)
Dim allStates As Object() = CType(savedState, Object())
MyBase.LoadViewState(allStates(0))
_refreshState = CBool(allStates(1))
_isRefresh = _refreshState = CBool(Session("__ISREFRESH"))
End Sub 'LoadViewState
Protected Overrides Function SaveViewState() As Object
Session("__ISREFRESH") = _refreshState
Dim allStates(2) As Object
allStates(0) = MyBase.SaveViewState()
allStates(1) = Not _refreshState
Return allStates
End Function 'SaveViewState
End Class 'Page

End NameSpace
************************************************* ***********

If I do a:

trace.warn("is Refresh = " & Page.IsRefresh)

or

trace.warn("is Refresh = " & IsRefresh)

I get the error:

BC30456 'IsRefresh' is not a member of
'System.Web.UI.Page'

I took the compiled version (StevenBey.Web.UI.dll) and put it
in
my
Bin
directory.

If you look at trace page you won't see __ISREFRESH?

Is there something else I need to do to get this to work?

Thanks,

Tom

Sep 3 '06 #15

P: n/a
<to**************@uniroma1.itwrote in message
news:11**********************@74g2000cwt.googlegro ups.com...
Thanks Tom for sharing your efforts. You have done a lot of research
on this subject! Actually you could write an article on that and place
it on a website: I am sure many programmers would like it and could be
a source of further suggestion.

In case you do, let us know the URL :)
I might, if I could figure out how to handle the situation I mention below
as well as why the extra SVS happens outside of the actual SVS event.

Thanks,

Tom
>
Thanks you very much,

Tommaso

tshad ha scritto:
>
Let me know if you find a definitive solution...
Finally got it to work.

I used as my basic code the code from
http://jarednevans.typepad.com/techn..._techno_b.html.

The problem with this code is it doesn't take into account the Initial
page
load being refreshed, which may not be a problem most of the time. But
if
you do any database processing, setting session variables etc. - this
could
be a problem.

What I added was the setting of a session variable to the URL and when I
enter the page, if the page is NOT a Post Backed page I compare the
current
URL with what is in the Session variable. If they don't match or the
Session Variable is not there, then this is not a refresh. If it is
there,
then the page was either refreshed or they just reentered the URL in the
Browsers Address line (really the same thing).

I also found that SVS (SaveViewState) is executed twice if you have
tracing
on (trace="true"). You'll notice the first SVS is done between events (at
least the events that display on the trace page). I assume this one is
the
being executed by Trace in some way because right after that the
SaveViewState is executed. If trace is off, you only get the SVS.

Also, the trace statement is the only thing that is in my SVS call. So
something is calling my SVS event other than the Page SVS and there are
no
controls on the page.

aspx.page Begin Init
aspx.page End Init 0.000079 0.000079
aspx.page Begin PreRender 0.000116 0.000037
aspx.page End PreRender 0.000165 0.000049
Inside refresh.cs at SaveViewState 0.000880 0.000715
aspx.page Begin SaveViewState 0.001932 0.001053
Inside refresh.cs at SaveViewState 0.002330 0.000398
aspx.page End SaveViewState 0.002384 0.000054
aspx.page Begin Render 0.002411 0.000027
aspx.page End Render 0.137321 0.134909

So what I do set a variable, _firstTime, that I set to false in the 1st
SVS
so that the 2nd SVS will execute normally but won't set anything.

To make this work for only specific pages, you need to add
Inherits="MyFunctions.Page" to the Page tag on the page:

<%@ Page Language="VB" trace="true" debug="true" ContentType="text/html"
ResponseEncoding="iso-8859-1" Inherits="MyFunctions.Page" %>

or if you want this to work for every page I believe you can set it in
your
Web.Config file:

<pages Inherits="MyFunctions.Page" />

The one problem I have not been able figure out (if you are only doing
this
for specific pages) is when you load a page (which would set the Session
variable) then go directly another set of pages not using this procedure.
If you then go back to the 1st page, it will set the session variable set
and think this is a refresh, when in fact it isn't.

Here is the actual code (in c# as that is what I created this in, but
then I
converted it to vb for here).
************************************************* **************************
using System;
using System.IO;
using System.Web;
using System.Web.UI;
using System.Web.SessionState;

namespace MyFunctions
{

// This program deals with 2 situations of refresh.
// Initial load of a page
// Checks to see if this is a PostBack.
// If not - Checks to see if Session("__LASTPAGEPROCESSED") matches the
current URL.
// If it does, the page has either been refreshed or re-entered in the
address line of Browser
// PostBack
// Checks to see if _refreshState that is stored in the ViewState
matches
Session["__ISREFRESH"].
// if it does, we have a refresh. This is because we set the Session
variable and put the reverse
// value in the viewstate. If we refresh the page we get the old
viewstate back and the old viewstate
// value should match our new Session value. Remember we keep
reversing
the values.

public class Page : System.Web.UI.Page
{
private bool _refreshState;
private bool _isRefresh;

// This variable is only important to signify whether SaveViewState is
executing for a second time
// During the same Page Life Cycle. Have only seen this when you have
tracing on (Trace="true")

private bool _firstTime = true;

public bool IsRefresh
{
get
{
if (!IsPostBack)
{
if ((Session["__INITIALREFRESHPAGE"] != null) &&
((string)Session["__INITIALREFRESHPAGE"] ==
HttpContext.Current.Request.Url.ToString()))
{
// Need to set this here as this is a refresh or was re-executed from
the URL line.
// In either case, the results are the same.
_isRefresh = true;
}
}
return _isRefresh;
}
}

public bool IsFirstTime
{
get
{
return _firstTime;
}
set
{
_firstTime = value;
}
}

protected override void LoadViewState(object savedState)
{
object[] allStates = (object[]) savedState;
base.LoadViewState(allStates[0]);
_refreshState = (bool) allStates[1];
_isRefresh = _refreshState == (bool) Session["__ISREFRESH"];
Session.Remove("__INITIALREFRESHPAGE"); // Only there to test
initial
page load refresh
}

protected override object SaveViewState()
{
object[] allStates = new object[2];
allStates[0] = base.SaveViewState();

// This test is mainly when trace="true". SaveViewState will execute
twice
and we only need to process this once

if (_firstTime)
{
// This only happens on the initial load of a page where there is no
LoadViewState
if (!IsPostBack)
{
Session["__INITIALREFRESHPAGE"] =
HttpContext.Current.Request.Url.ToString();
}
Session["__ISREFRESH"] = _refreshState;
allStates[1] = !_refreshState;
}
else
{
// We already reversed _refresh states. To do it again would put it
back
the way it was.
// Need to store the _refreshState anyway as we need to return the
ViewState.

allStates[1] = !_refreshState;
}
_firstTime = false;
return allStates;
}
}
}
************************************************* ***************************

Here is the converted VB code (but I haven't tested it)
************************************************* ****************
Imports System
Imports System.IO
Imports System.Web
Imports System.Web.UI
Imports System.Web.SessionState

' This program deals with 2 situations of refresh.
' Initial load of a page
' Checks to see if this is a PostBack.
' If not - Checks to see if Session("__LASTPAGEPROCESSED") matches the
current URL.
' If it does, the page has either been refreshed or re-entered in the
address line of Browser
' PostBack
' Checks to see if _refreshState that is stored in the ViewState matches
Session["__ISREFRESH"].
' if it does, we have a refresh. This is because we set the Session
variable and put the reverse
' value in the viewstate. If we refresh the page we get the old
viewstate
back and the old viewstate
' value should match our new Session value. Remember we keep reversing
the values.
Public Class Page
Inherits System.Web.UI.Page
Private _refreshState As Boolean
Private _isRefresh As Boolean

' This variable is only important to signify whether SaveViewState is
executing for a second time
' During the same Page Life Cycle. Have only seen this when you have
tracing on (Trace="true")
Private _firstTime As Boolean = True

Public ReadOnly Property IsRefresh() As Boolean
Get
If Not IsPostBack Then
If Not (Session("__INITIALREFRESHPAGE") Is Nothing) And
CStr(Session("__INITIALREFRESHPAGE")) =
HttpContext.Current.Request.Url.ToString() Then
' Need to set this here as this is a refresh or was
re-executed from the URL line.
' In either case, the results are the same.
_isRefresh = True
End If
End If
Return _isRefresh
End Get
End Property

Public Property IsFirstTime() As Boolean
Get
Return _firstTime
End Get
Set
_firstTime = value
End Set
End Property

Protected Overrides Sub LoadViewState(savedState As Object)
Dim allStates As Object() = CType(savedState, Object())
MyBase.LoadViewState(allStates(0))
_refreshState = CBool(allStates(1))
_isRefresh = _refreshState = CBool(Session("__ISREFRESH"))
Session.Remove("__INITIALREFRESHPAGE") ' Only there to test initial
page load refresh
End Sub 'LoadViewState

Protected Overrides Function SaveViewState() As Object
Dim allStates(2) As Object
allStates(0) = MyBase.SaveViewState()

' This test is mainly when trace="true". SaveViewState will
execute
twice and we only need to process this once
If _firstTime Then
' This only happens on the initial load of a page where there
is
no LoadViewState
If Not IsPostBack Then
Session("__INITIALREFRESHPAGE") =
HttpContext.Current.Request.Url.ToString()
End If
Session("__ISREFRESH") = _refreshState
allStates(1) = Not _refreshState
Else
' We already reversed _refresh states. To do it again would
put
it back the way it was.
' Need to store the _refreshState anyway as we need to return
the
ViewState.
allStates(1) = Not _refreshState
End If
_firstTime = False
Return allStates
End Function 'SaveViewState
End Class 'Page
************************************************* ****************************

In your code, you only have to check the property:

if IsRefresh...

Seems to work OK, except for the one problem I stated.

Someone mentioned a problem with the backbutton in the other program, but
I
am not dealing with that here - just a refresh.

Tom
Tommaso

tshad ha scritto:

<to**************@uniroma1.itwrote in message
news:11**********************@i3g2000cwc.googlegr oups.com...
Hi Tom,

I am afraid that is not exactly an "elementary" topic. Here is a
more
authoritative source:

http://msdn.microsoft.com/msdnmag/is...8/CuttingEdge/

I looked there, but the article is about Script Callbacks.

That was interesting also, as I was looking at doing this (like Ajax)
but
in
.net.

I knew you could do this in .net 1.1, but if what this is saying is
correct
then I can't do it.

"ASP.NET 2.0 provides a built-in mechanism for client callbacks based
on
the
services of a COM object that ships with Internet Explorer 5.0 and
later.
By
using the same object, you can implement a script callback mechanism
in
ASP.NET 1.x as well"

I don't want to do it if you have to depend on the user using IE.

Thanks,

Tom
Let us know about the best solution... :)

Tommaso

tshad ha scritto:

"tshad" <ts**********@ftsolutions.comwrote in message
news:%2******************@TK2MSFTNGP05.phx.gbl. ..
<to**************@uniroma1.itwrote in message
news:11**********************@b28g2000cwb.googlegr oups.com...
But what about the back button problem.
I think that user was right. It doesn't work in that case.
Actually, I did some work with the program and found that it also
doesn't
work on the inital page. It only works for PostBacks.

Looking into it I found that since LoadViewState is not called on
an
initial
page load (makes sense as there would be no ViewState saved here),
there
is
nothing to test.

I need to find some other way to make it work or find a way to find
out
if
the initial page.

Tom

I know that that is also a problem and I am going to address that
problem
in a bit. But I needed to get the refresh problem taken care of
right
away.
The (almost) funny thing is that a guy has copied this article
and
published it on Code Project site.
He also copied the bugs clearly ... :)

I agree.

But that allows someone else to at least get a starting point to
work
from.

Tom
Tommaso

tshad ha scritto:

I was able to get it to work after closer reading of the
article.

************************************************ *******************
Namespace MyFunctions

Public Class Page
Inherits System.Web.UI.Page
Private _refreshState As Boolean
Private _isRefresh As Boolean
Public ReadOnly Property IsRefresh() As Boolean
Get
Return _isRefresh
End Get
End Property
Protected Overrides Sub LoadViewState(savedState As Object)
Dim allStates As Object() = CType(savedState, Object())
MyBase.LoadViewState(allStates(0))
_refreshState = CBool(allStates(1))
_isRefresh = _refreshState = CBool(Session("__ISREFRESH"))
End Sub 'LoadViewState
Protected Overrides Function SaveViewState() As Object
Session("__ISREFRESH") = _refreshState
Dim allStates(2) As Object
allStates(0) = MyBase.SaveViewState()
allStates(1) = Not _refreshState
Return allStates
End Function 'SaveViewState

End Class 'Page

End NameSpace
********************************************** *

In my page, I have to add an inherits:

<%@ Page Language="VB" trace="true" ContentType="text/html"
ResponseEncoding="iso-8859-1" Inherits="MyFunctions.Page"%>
...
trace.warn("is Refresh = " & IsRefresh())

Then it works.

I am confused as to the code with the "allStates" and how it
works.

In LoadViewState:
Dim allStates As Object() = CType(savedState, Object())
MyBase.LoadViewState(allStates(0))
_refreshState = CBool(allStates(1))

It seems to be getting the savedState from the parameters list,
then
I
think
it does the normal LoadViewState passing allStates(0) (which is
really
just
savedState - so why not just pass savedState??).

Also, what is allStates(1)?

In SaveViewState:
Dim allStates(2) As Object
allStates(0) = MyBase.SaveViewState()
allStates(1) = Not _refreshState
Return allStates

I assume the MyBase.SaveViewState() is just doing the normal
SaveViewState
and passes back the changed ViewState into allStates(0), what
is
allStates(1) and why are we passing back allStates - does this
overwrite
the
ViewState that was written out by MyBase.SaveViewState()?

Thanks,

Tom

<to**************@uniroma1.itwrote in message
news:11*********************@m73g2000cwd.googleg roups.com...
Hi Tom,

see the remarks here made by a User:

http://jarednevans.typepad.com/techn..._techno_b.html

this method seems to fail when back button is pressed. You
can
download
a demo.

Tommaso

tshad ha scritto:

I was looking for a way to handle refreshes (user pressed
refresh
button)
and found a piece of code to check if a Web page was
refreshed
but
I
can't
get it to work.

The code is:
************************************************ ************
Namespace StevenBey.Web.UI

Public Class Page
Inherits System.Web.UI.Page
Private _refreshState As Boolean
Private _isRefresh As Boolean
Public ReadOnly Property IsRefresh() As Boolean
Get
Return _isRefresh
End Get
End Property
Protected Overrides Sub LoadViewState(savedState As
Object)
Dim allStates As Object() = CType(savedState, Object())
MyBase.LoadViewState(allStates(0))
_refreshState = CBool(allStates(1))
_isRefresh = _refreshState =
CBool(Session("__ISREFRESH"))
End Sub 'LoadViewState
Protected Overrides Function SaveViewState() As Object
Session("__ISREFRESH") = _refreshState
Dim allStates(2) As Object
allStates(0) = MyBase.SaveViewState()
allStates(1) = Not _refreshState
Return allStates
End Function 'SaveViewState
End Class 'Page

End NameSpace
************************************************ ************

If I do a:

trace.warn("is Refresh = " & Page.IsRefresh)

or

trace.warn("is Refresh = " & IsRefresh)

I get the error:

BC30456 'IsRefresh' is not a member of
'System.Web.UI.Page'

I took the compiled version (StevenBey.Web.UI.dll) and put
it
in
my
Bin
directory.

If you look at trace page you won't see __ISREFRESH?

Is there something else I need to do to get this to work?

Thanks,

Tom


Sep 5 '06 #16

This discussion thread is closed

Replies have been disabled for this discussion.