Can I prevent the user from exiting a page?

W

WISEMANOFNARNIA

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
 
J

jacerhea

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>
 
G

George

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top