Can I prevent the user from exiting a page?

  • Thread starter Thread starter WISEMANOFNARNIA
  • Start date Start date
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
 
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>
 
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.
 
Back
Top