Solved

Browser close event in vb.net

Posted on 2012-04-09
15
657 Views
Last Modified: 2012-05-31
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?
0
Comment
Question by:Angel02
  • 7
  • 5
  • 3
15 Comments
 
LVL 20

Accepted Solution

by:
BuggyCoder earned 300 total points
ID: 37825100
0
 

Author Comment

by:Angel02
ID: 37825225
Thanks. That might work for me.
Can you please also tell me if I can call a server-side method on beforeunload event?
0
 
LVL 20

Expert Comment

by:BuggyCoder
ID: 37825244
0
 

Author Comment

by:Angel02
ID: 37825368
I am using .NET 1.1. I am not sure if this would work. Will it?
0
 
LVL 20

Expert Comment

by:BuggyCoder
ID: 37825383
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
0
 
LVL 83

Assisted Solution

by:CodeCruiser
CodeCruiser earned 200 total points
ID: 37833023
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.
0
 

Author Comment

by:Angel02
ID: 37867900
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>
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 83

Expert Comment

by:CodeCruiser
ID: 37875975
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?
0
 

Author Comment

by:Angel02
ID: 37883047
No I haven't used Update Panels. What can I use in .net 1.1 for the same purpose?
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 37908616
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.
0
 

Author Comment

by:Angel02
ID: 37911601
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?
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 37911801
What else causes the postback? Do you have drop down lists etc?
0
 

Author Comment

by:Angel02
ID: 37912046
No dropdowns. I have radio buttons and checkboxes that cause the postback.
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 37922261
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.
0
 

Author Comment

by:Angel02
ID: 37935497
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.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now