Browser close event in vb.net

If I use the following javascript, I can throw an alert when the window is closing.

<script type="text/javascript">
            window.onbeforeunload = WindowCloseHanlder;
function WindowCloseHanlder()
{
window.alert('My Window is closing');
}
            </script>

But I want to give the user options:
Your changes are not saved. Do you want to exit?
Yes - Should close the browser.
No - Should not close the browser and take the user back to the form.

Can I call a server-side function on close event so that I can display a custom message box?
Angel02Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Angel02Author Commented:
Thanks. That might work for me.
Can you please also tell me if I can call a server-side method on beforeunload event?
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

Angel02Author Commented:
I am using .NET 1.1. I am not sure if this would work. Will it?
BuggyCoderCommented:
i tried to show you a prototype, you can always use jquery ajax to call the method instead of ScriptManager doing it for you...
Here is sample of JQuery AJAX for you, there are many more on internet, try google:-

http://viralpatel.net/blogs/2009/04/jquery-ajax-tutorial-example-ajax-jquery-development.html
CodeCruiserCommented:
You can not stop user from closing the browser. When you handle the onbeforeunload, browser would give user the OK/Cancel option and if user clicks OK, window will be closed.
Angel02Author Commented:
In that webpage, the onbeforeunload is also being handler at every checkbox change, radio button selection and butoon click event. How can I avoid that?

<script type="text/javascript">
            window.onbeforeunload = WindowCloseHanlder;
function WindowCloseHanlder()
{
window.alert('My Window is closing');
}
</script>
CodeCruiserCommented:
Its because the page is being posted back so technically speaking, its closing, being sent back to server and then reopening. Do you use Update panels?
Angel02Author Commented:
No I haven't used Update Panels. What can I use in .net 1.1 for the same purpose?
CodeCruiserCommented:
There is no AJAX in 1.1 as for as I can remember. But everytime you make change to a control with runat=server and control which can post back to server, such as textbox, checkbox, button etc, the page posts back and causes the unload event to be executed.
Angel02Author Commented:
My form is full of controls which need runat=server. I do need to alert the user that he is closing an unsaved form when he tries to close the form. Is there any work around?
CodeCruiserCommented:
What else causes the postback? Do you have drop down lists etc?
Angel02Author Commented:
No dropdowns. I have radio buttons and checkboxes that cause the postback.
CodeCruiserCommented:
I am afraid there is no easy solution. One option is to stop radio buttons and checkboxes from postback if you can live with that.
Angel02Author Commented:
I might want to remove all the server-side validation and use javascript only to add client-side validation. This is also a good option because the client hates the flashing of the page due to each postback!

Right now I use labels such as "Please Enter Name" and make them visible and invisible accordingly in the server-side code.

If radiobutton1.SelectedIndex < 0 Then
                    label1.Text = "Please select Yes or No"
End If

I want to use such labels instead of alert("") in javascript client-side validation. Can I do that? Please advise.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.