Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET
Hello experts,
 
I am working on c# asp.net web page development.
I have to disallow the site from reopening in other tabs or other browser windows.
That I am able to handle generating unique PageId. When the user opens the same site in another tab or new browser window, a new page id is generated,
I check it against the current page id and redirect it to the error page.
But when I refresh the page, I also get the new PageId and not the existing one and so it gets redirected to the error page.
I searched on internet for solutions but none of those solutions are working for me. How can I detect a refresh??
Can you please guide me on how to handle this??
 
Thanks
RaesaRk
Posted 14-Nov-12 1:54am
raesark416

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Your best bet is to look at the page events
 
http://msdn.microsoft.com/en-us/library/aa479007.aspx[^]
 
You can add your custom code in these events to see if your PageID matches and prevent the page from loading.
 
1. Look at the page load, you can do an if test to see if the page is post back
2. Look at the unload event
 
If you dont understand the post back read this article for a basic explaination
 
Detecting Refresh or Postback in ASP.NET[^]
 
Make sure the you have you are calling you function onPostBack like in the example or you wont generate the new id on refreshing the page.
 
<form id="form1" runat="server" onsubmit="onPostBack()">
  Permalink  
v4
Comments
raesark at 14-Nov-12 10:19am
   
Hello,

Thanks for your reply.
But when I refresh, I get a new Id and the code redirects it to the error page.
I am not able to detect the refresh action.

RaesaRK
Mathlab at 14-Nov-12 10:49am
   
If you read the article that I added to the bottom of the solution it might explain why your PageID keeps changing and it gives you ideas on how to manage your refresh problem
raesark at 15-Nov-12 9:24am
   
Hello Mathlab,
 
I read the article that you had posted at the bottom of the solution.
I tried implementing on a test work space and it works fine. But when I implement the same javascript in my code, it doesn't work. When I hit the button or any other control, it does not do any action. I tried debugging the script and it exits at this location:
 
--------------
if (continueSubmit) {
for (i = 0, l = this._onSubmitStatements.length; i < l; i++) {
if (!this._onSubmitStatements[i]()) {
continueSubmit = false;
break;
}
}
}
 
if (!continueSubmit) {
if (evt) {
evt.preventDefault();
}
return;
}
--------------
 
Could this be some settings issue? I had given all the settings that are needed in the web config file.
Can you throw some light as to where the problem is?
 
Thanks
RaesaRK
Mathlab at 15-Nov-12 9:58am
   
I assume your continue sublit is where you check if the page ID has changed. Did you put in the client script to create the new id on refresh?
Mathlab at 15-Nov-12 9:58am
   
You have to add the script tag to the .aspx page for the .js file in the example and in the form tag you have to add the onsubmit behavior and make it equal to the onPostback function in the client script.
raesark at 15-Nov-12 10:18am
   
Thanks for your reply Mathlab.
I did exactly as it has been written in the article and it works fine on the test work space that I built.
I am just not able to figure it out what is wrong in my existing workspace. :(
Mathlab at 15-Nov-12 10:27am
   
do you have this tag
 
<form id="form1" runat="server" önsubmit="onPostBack()">
raesark at 15-Nov-12 10:43am
   
Thanks for the reply.
Yes I do have. Here is my code
---------------------------------------------------------------------------------------------------
<head runat="server">
<title>Design Page</title>
<script type="text/javascript" language="javascript" src="Client-ide_Validn.js"></script>
</head>
<body background="Image/logo.JPG" style="background-repeat: no-repeat";>
<form id="form1" runat="server" defaultbutton="ButtonCalculate" defaultfocus="txtId" submitdisabledcontrols="True" önsubmit="onPostBack()">
--------------------------------------------------------------------------------------------
if (IsPostBack)
{
if ((bool)HttpContext.Current.Items["IsRefresh"])
{
Response.Write("refreshed");
sPageIds = new List();
string sVal = ((new Random()).Next()).ToString();
sPageIds.Add(sVal);
}
else
{
}
}
else
{
{
string sVal = ((new Random()).Next()).ToString();
if (sPageIds.Count == 0)
{
sPageIds.Add(sVal);
}
else
{
if (sPageIds.Contains(sVal) == false)
{
Response.Redirect("ErrorPage.aspx");
}
}
}
}
--------------------------------------------------------
'IsRefresh' is always false here. :(
It works fine in test workspace though.
 
Thanks
RaesaRK
Mathlab at 15-Nov-12 15:44pm
   
The "src" file name is spelt incorrectly in your code, its probably not executing
 
<script type="text/javascript" language="javascript" src="Client-Side_Validn.js"></script>
raesark at 16-Nov-12 3:01am
   
Hello Mathlab,
Thanks for your reply.
I type it wrong above....it is as exactly as shown in the code below
<script type="text/javascript" language="javascript" src="Client-Side_Validn.js"></script>
Even then it doesn't work. :(
I just can't figure out why it fails.
 
RaesaRK

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 371
1 Sergey Alexandrovich Kryukov 286
2 RyanDev 75
3 PhilLenoir 70
4 Pheonyx 50
0 Sergey Alexandrovich Kryukov 6,676
1 OriginalGriff 6,056
2 CPallini 2,473
3 Richard MacCutchan 1,697
4 Abhinav S 1,560


Advertise | Privacy | Mobile
Web04 | 2.8.140821.2 | Last Updated 15 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100