AspdotnetCodes.com
Search
Articles
Books
Resources
Asp.Net News
Downloads
Free Tech Magazines
Asp.Net Web Hosting
Archives
Softwares
Newsletter
Suggest Us
Link to Us
Feeds Subscription
Articles
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
Hi,
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
Hi,
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.

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

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,

<script>
var canExit = true;

function fnSetFlag()
{
canExit = true;
}

function fnRemoveFlag()
{
canExit = false;
}


window.onbeforeunload=fnConfirmOnExit;

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


</script>


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
Sexy Web Design - Free 71 Page Preview!
Backup and Recovery Best Practices for Microsoft SQL Server 2005
Introduction to Web Applications Development