473,503 Members | 1,929 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Can I prevent the user from exiting a page?

I would like to prevent a user from exiting a page by clicking on
links. the only way he should be able to exit is clicking on a 'save'
button or a 'cancel' button. I can see that javascript is the only
way to prevent him from closing his browser completely before doing a
'save', but I was wondering if asp.net had a way of preventing him
from leaving the page by clicking on links. The following code does
not work - in fact it gives an error when it is tried
(response.redirect is the statement that fails). Is there any way to
accomplish what I'm trying to do?

Protected Sub Page_Unload(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Unload
If Not Session("ExitViaButton") Then
Session("BadExitOfDOEditPage") = True
Response.Redirect("~/Pages/home.aspx?
Pageload=DailyOutreachFormEdit")
End If
End Sub

-- Marv
Nov 14 '08 #1
3 4709
"WISEMANOFNARNIA" <CO*********@LYCOS.COMwrote in message
news:5d**********************************@a26g2000 prf.googlegroups.com...
I would like to prevent a user from exiting a page by clicking on links
Not 100%. You can experiment with the client-side JavaScript events
window.onunload and window.onbeforeunload events, but these are not
reliable:
http://www.google.co.uk/search?hl=en...reunload&meta=
The following code does not work
Protected Sub Page_Unload
It appears you're under the misapprehension that the Page_Unload method runs
when a user closes / leaves a page. The Page_Unload method (like all other
server-side events) runs on the server, not the client. The Page_Unload
method is the final server-side method which is called after the page has
been fully rendered, streamed to the client, and is ready to be discarded.
At this point, page properties such as Response and Request are unloaded and
any cleanup is performed.

http://www.google.co.uk/search?hl=en...ifecycle&meta=
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Nov 14 '08 #2
I would say that you are going to have to manually disable all of the
links except the save or cancel buttons. There are a couple of ways
to do that...

1. Add css to not show anchor tags. This may cause UI issues.

a {
display:none;
}

2. If the links are linkbutton or hyperlink controls you can set the
enabled property to false.

3. If they are not server controls you can remove the href property
from the anchor tag manually.

<a>Disabled Link</a>
instead of
<a href="http://www.google.com">Enabled Link</a>
Nov 15 '08 #3
the idea is to attach OnClick event listener to every link on the page.
The best way to do it is to use JQuery library or you can iterate through
all link yourself.
But here is how it's done with JQuery

$('a').click(function(event){
event.preventDefault();
});
The $('a') selects all links and .click method attaches to each link the
OnClick event that performs JavaScript function.
event.preventDefault will prevent from clicking on a link to go through.
George.


"WISEMANOFNARNIA" <CO*********@LYCOS.COMwrote in message
news:5d**********************************@a26g2000 prf.googlegroups.com...
>I would like to prevent a user from exiting a page by clicking on
links. the only way he should be able to exit is clicking on a 'save'
button or a 'cancel' button. I can see that javascript is the only
way to prevent him from closing his browser completely before doing a
'save', but I was wondering if asp.net had a way of preventing him
from leaving the page by clicking on links. The following code does
not work - in fact it gives an error when it is tried
(response.redirect is the statement that fails). Is there any way to
accomplish what I'm trying to do?

Protected Sub Page_Unload(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Unload
If Not Session("ExitViaButton") Then
Session("BadExitOfDOEditPage") = True
Response.Redirect("~/Pages/home.aspx?
Pageload=DailyOutreachFormEdit")
End If
End Sub

-- Marv
Nov 16 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
2269
by: Daniel Walzenbach | last post by:
Hi I am faced with the following problem: I have a page (let’s call this page page1.aspx) containing some TextBoxes and a hyperlink which opens another page (let’s call this page page2.aspx)...
1
2677
by: The Fox | last post by:
How to prevent user to add web reference to my web services? Can I add password to web services so that only the users who know the password can add a web reference? Thanks in advance.
1
5709
by: User | last post by:
Hi, Are there ways to let browser forget about the previous submitted form data? Or prevent user from pressing F5 to submit the same form again Or prevent user from presising back on the...
4
1742
neo008
by: neo008 | last post by:
Hi All, Can I prevent user to press cross button? I want to disable all three buttons for minimize, maximize and close for parent as well as child window. Is it possible? I'm using VB6. ...
4
6639
by: Luqman | last post by:
How can I Prevent user from accessing any page directly without Login ? User should not be able to by pass the Login screen by typing the Url of any page directly. I am using ASP.Net 2.0 with...
4
18127
by: =?Utf-8?B?QXNhZg==?= | last post by:
Hi, How can I prevent from an ASP.NET page to resend all the data again when the user press the Refresh button or F5 on the browser? Thanks in advanced, Asaf
5
20898
by: alvintiow | last post by:
Hi, I intend to use barcode for input and prevent user to modify the barcode they scan, user are not allow to input the barcode number by keyboard. Please advise how to do this. Thanks.
0
1450
by: helveticus | last post by:
I have a master/details configuration that includes multiple user controls. The details page is configured to cache data via VaryByCustom. This works fine. One of the user controls contains an...
0
1320
by: Appyks | last post by:
How Do I prevent User from Locking the System? Obviously I can set following Windows registry value But it doesn’t serve my propose "DisableLockWorkstation"=dword:00000001 I need...
0
979
by: Roger Davies | last post by:
I have a Userform in VBA 2007 Excel on which I have several textboxes and a listbox. I am able to initiate the drag and drop but cannot work out how to prevent user from dropping the data into the...
0
7064
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7261
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7315
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7445
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5559
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4991
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4665
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1492
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
721
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.