Asp.Net News
Free Tech Magazines
Asp.Net Web Hosting
Suggest Us
Link to Us
Feeds Subscription
Questions & Answers
Tips & Tricks
 How to make leaving page confirm box ignore datalist page links
Posted by Charlie Childs on  5/13/2010 7:38:17 AM Category: ADO.NET
Add to my favorites
Email to friend
I have implemented the pager taught by aspdotnetcodes ( Datalist Paging Using Paged Data Source - by Moderator 1 ). I use C# with Visual Web Developer 2008 Express.

I need to use a leaving page confirm box associated with the page which the pager is on. For that I have tried several codes like the following javascript:

window.onbeforeunload = function(evt) {
var message = '!! Leaving this page may cause loss of data !!\n';

if (typeof evt == 'undefined')
evt = window.event;

if (evt)
evt.returnValue = message;

return message;

But I find that the message box responds to the pager links so when a user clicks a pager link to view more details on the same page - the message box appears asking the user if they are sure they want to leave the web page.

How to get round this problem?

Many thanks!!

Post Your Reply Post New Question

 Viewer's Reply
Closing Window Alert with JavaScript onbeforeunload event
Answered By Moderator1 on 5/27/2010 10:16:15 PM
The onbeforeunload event will not work for your case as it will fire for all page unload events, even when the pager is clicked or even if you refresh the page.

So need to change logic little bit by setting a flag, make it true if any changes in the pages needs to be alerted before close. Initial value of the flag will be false, so the event will not fire until there is a change in the page contents. Try this logic.
"Set flag" is not in my vocabulary yet.
Answered By Charlie Childs on 6/10/2010 2:25:03 PM
Thanks very much for your response.
I am struggling with how to search for a tutorial re "setting flag" - it appears to be a very generically used in code and web circles.
I would be very grateful if you could point me to an example or tutorial of how to set a flag.

Alert User through JavaScript Before Page Unload (window.onbeforeunload)
Answered By Moderator1 on 6/11/2010 11:57:29 PM

We are not interested in finding some other solutions for you in the internet. This you can do yourself, no need to contact us. We always provide the solutions we found or use in practical. If we don’t have one, then we might delay to reply to your questions. After we found one, we usually reply. This is the specialty of our website. Here below we have posted the solutions for you, not exactly what you want, you must change it to suite your requirement.

Add the below JavaScript code,

var canExit = true;

function fnSetFlag()
canExit = true;

function fnRemoveFlag()
canExit = false;


function fnConfirmOnExit()
if (canExit)
return "!! Leaving this page may cause loss of data !!\n Are you sure you want to exit this page?";


In your code-behind, in the ItemDataBound event, you need to call the fnRemoveFlag, in order to avoid the alert box. The code will be something like this,

protected void dlPaging_ItemDataBound(object sender, DataListItemEventArgs e)
LinkButton lnkbtnPage = (LinkButton)e.Item.FindControl("lnkbtnPaging");
if (lnkbtnPage.CommandArgument.ToString() == CurrentPage.ToString())
lnkbtnPage.Enabled = false;
lnkbtnPage.Font.Bold = true;
lnkbtnPage.Attributes.Add("onclick", "fnRemoveFlag();");
Post Your Reply If you still have problem, post it again under this reply for better response.

Sponsored by
The PHP Anthology: 101 Essential Tips, Tricks & Hacks, 2nd Edition - Free 207 Page Preview!
The JavaScript Anthology: 101 Essential Tips, Tricks & Hacks - Free 158 Page Preview
Programmers Heaven C# School Book - Free 338 Page eBook