This cannot be done, at least not perfectly. Most browsers only allow you to popup a message when the user is trying to close the window. But you can't prevent the window from closing.
You can use javascript code to wireup an event:
$(document).ready(function () {
window.onbeforeunload = WindowClosing;
});
function WindowClosing()
{
return "Bye bye";
}