Detecting Page Refresh

I want to detect if a .net page has refreshed. Any ideas how to do this in VB.net 2008 or embedding JavaScript in vb code to evaluate the same?

Then end result is, that I want skip code if a user clicks the refresh button.

I have this JavaScript that works in an html page:
<html>
<head>
<script language=“JavaScript”> <!–
function checkRefresh()
{
if( document.refreshForm.visited.value == “” )
{
// This is a fresh page load
document.refreshForm.visited.value = “1”;
// You may want to add code here special for
// fresh page loads
alert(“We are now open”);
}
else
{
// This is a page refresh
document.refreshForm.visited.value = “2”;
// Insert code here representing what to do on
// a refresh
alert(“I am refreshed”);
}
}
}–>
</script>
</head>
<body onLoad=“JavaScript:checkRefresh();”>
<form name=“refreshForm”>
<input type=“hidden” name=“visited” value=“” />
</form>
</body>
</html>

Thanks.

Chris S.

In code-behind, in VB.NET


    If  Page.IsPostback then
    'code for a post-back
    End If

Or, if it’s not post-back…


    If  Not Page.IsPostback then
    'code for a inital load  Like OnLoad on JS
    End If

God Bless that Property…

I tried that, but if I click the refresh button on the browser the postback event does not work.

Cruncher, show me your ASP.NET code…for the page-behind, the code that calls the event…

Also, is your “form” tag running at server? <form runat=“server”>all page content goes here</form>…If not, you need it to call server events.

Browser refresh button will not fire a postback unless a postback has already happened on the page. Then a refresh will submit the form again but I don’t think that answers the question in any way.

I have nothing in the code behind right now, but want to know how to apply this code or modify it to run.

If the JavaScript evaluates that there is a refresh, I don’t won’t it to run a block a code that references runat=“server”

If the JavaScript evaluates to not a refesh, I want it to run the block of code that references runat=“server”

You can copy and paste the code above to a .aspx page and it will work with no code. If you add a .net control, it won’t work.

Any thoughts on working around this?

Chris S.

Does the user come to the page directly or via a link? If never directly, then look at the page referer value.

If they can come directly, but you want to make sure they only run the script once, then maybe use a semaphore that disappears after a while.

The user comes to the page directly and initial code runs once and should not run a gain.

I must admit, I had to look up the term semaphor, but I am open to this option, but don’t know how to write the code for it.

If I was grasping the concept appropriately, I would need a binary semaphor.

It would initiate to false, and within a few seconds, it would be set to true and remain that way for that particular browser window.

or psuedocode:

Basically, I want the page to run like so:

If this if the first time the page has been opened/viewed/not refreshed

[INDENT]Run Startup Code
[/INDENT]Otherwise

[INDENT]Skip Startup Code
[/INDENT]End If

I have C# code that works:
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Session[
“CheckRefresh”] =
Server.UrlDecode(System.
DateTime[SIZE=2].Now.ToString());
}

}
[/SIZE]protected void Button1_Click(object sender, EventArgs e)
{
if (Session[“CheckRefresh”].ToString() ==
ViewState[
“CheckRefresh”].ToString())
{
Label1.Text =
“Hello”;
Session[
“CheckRefresh”] =
Server.UrlDecode(System.
DateTime.Now.ToString());
}
else
{
Label1.Text =
“Page Refreshed”;
}
}
protected void Page_PreRender(object sender, EventArgs e)
{
ViewState[
“CheckRefresh”] = Session[“CheckRefresh”];
}
}

but when I try to use my converted VB.NET code, it does not work:
Partial Public Class _Default
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
If Not IsPostBack Then
Session(“CheckRefresh”) = Server.UrlDecode(System.DateTime.Now.ToString())
End If
End [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Sub

[/COLOR][/SIZE][/COLOR][/SIZE]Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
If Session(“CheckRefresh”).ToString() = ViewState(“CheckRefresh”).ToString() Then
Label1.Text = “Hello”
Session(“CheckRefresh”) = Server.UrlDecode(System.DateTime.Now.ToString())
Else
Label1.Text = “Page Refreshed”
End If
End Sub
Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As EventArgs)
ViewState(
“CheckRefresh”) = Session(“CheckRefresh”)
End Sub
End
Class



What you want to do is test for the semaphore’s existence and if it doesn’t exist, do your code and set the semaphore. Next time thru, it exists, so your code won’t run. However, you’ll want to do something to clean up after it. There’s gotta be some kind of timeout or something that you’ll want to use to make sure the semaphore eventually goes away.

It might also go away when IIS restarts the app pool, which it will do periodically. So semaphores might not be the perfect answer either.

If it NEVER needs to run that code again after the first time a particular user gets to a page, use a database table to record that the user got to that page and then test for that each time into the page.

I am not sure how to right the code to do that; do you have a basic example or a link to one?

I posted some C# code; any idea how to get the VB.net code to work?

Your welcome…


Public Partial Class Default2
	Inherits System.Web.UI.Page
	Function Page_Load(sender As Object, e As EventArgs) As protectedvoid
		If Not IsPostBack Then
			Session("CheckRefresh") = Server.UrlDecode(System.DateTime.Now.ToString())
		End If

	End Function
	Function Button1_Click(sender As Object, e As EventArgs) As protectedvoid
		If Session("CheckRefresh").ToString() = ViewState("CheckRefresh").ToString() Then
			Label1.Text = "Hello"
			Session("CheckRefresh") = Server.UrlDecode(System.DateTime.Now.ToString())
		Else
			Label1.Text = "Page Refreshed"
		End If
	End Function
	Function Page_PreRender(sender As Object, e As EventArgs) As protectedvoid
		ViewState("CheckRefresh") = Session("CheckRefresh")
	End Function
End Class

I tried this out. I believe I need something in place of protectedvoid; but I am not sure what that is. Any thoughts?

Chris S.