In our application we need to handle browser close event. When a user directly closes the browser, we should give him an alert message and stop him by directly closing the window based on some conditions. This we have handled through body onunload event. The problem is we are getting the alert message but after showing the alert message the window gets closed.
Is there any way to stop closing the window on click of browser close event?
And also when the page is refreshed, the last performed action is getting fired again. Please provide your suggestions for avoiding this.
The short answer to your first question is: no. Application-level events in most browsers cannot be stopped by code in your webpage.
For your second question, you would need to put the code you want to execute into a conditional block:
if(!IsPostback)
{
//execute my code.
}
Related
I'm trying to cancel the load (basically, close the popup window during loading) if I detect that there is some problem. No internet connection to retrieve the data for the popup for instance.
await PopupNavigation.PopAsync() is what is in my Close method that I normally use, but it seems like it is getting called too early. I can still manually close the window by clicking on a Close button which invokes the Close method, but I don't want the users to have to do that.
Is there some way to abort the load, or get a notification when the page is done loading so I can programmatically exit it?
I have an iframe on a usercontrol. I need to print webpage which I load on iframe dynamically. I am able to reach this far. But I am not able to makeout whether user has actually printed or not.
I have javascript function print() called in the webpage on iframe.
When print dialog is displayed, I need to know whether user selected OK(to print) or cancel and pass the same back to usercontrol.
Any ideas as to how this can be done.
Thanks in advance.
It cannot be done. The browser will not inform you if the printing has been done or cancelled.
You can use onafterprint event to some extent but that is only supported on FF and IE. On IE 10 for example, the event is fired before the print dialog is even opened.
https://developer.mozilla.org/en-US/docs/Printing
How to trace on asp.net that user clicked on the browser close button.i tried with page unload event but its not fired when user clicked on the browser close button.I need to perform some operation to remove the temp file that time.
Unfortunately that's not possible from the server side, as the browser doesn't contact the webserver in any way when the window is being closed.
You'll need to use JavaScript in order to trigger some kind of AJAX request when the window is being closed.
Using plain JavaScript try:
window.onbeforeunload = function() { /* your ajax request here */ }
or if you're using JQuery:
jQuery(window).bind("beforeunload", function() { /* your ajax request here */ })
Note that this will trigger everytime a page is unloaded, whether you actually close the page or if you post a form etc.
Hope that helps!
You can use Javascript to listen to the onunload event and then use __doPostBack to trigger an event on the server side.
See: how to use dopostback
This is not the same old question about the validators and update panel compatibility.
Here's my scenario:
I have a aspx page with a script manager. This page has a ascx user control with my functionality. In this control, I have a few panels and areas which will get enabled/disabled or visible/hidden according to what the visitor selects by clicking on some of the radio buttons which will control this behaviour.
Everything is fine until this point. Validators and UpdatePanel are not fighting against each other, however, if I try to submit the page without filling the form, I get a validation message on my summary and it's all good with the validation itself.. Here's the problem, the radio buttons, which are responsible for set visibility for some areas stops working, but it starts working again after the 2nd or 3rd click. It's not delay or anything.. it just seems like the handlers are not bound to the control, then I click again, and it cleans up the summary and all error notifications and starts working again..
I don't know if it adds to the issue, but both the user control and the page have telerik controls such as combo box with load on demand, tooltips and the loading panel..
I have searched a lot about it and all I could find was the question regarding the validation controls not working inside updatepanel or updatepanel not working with validators.. in this case, it actually works, I only have problems after the validation has been performed and the page.isvalid is set to false..
To me it sounds like there is a server error in the first request ajax is sending. So here is my recommendation:
Open your web page in Firefox, then open Firebog and select "network" tab. This tab shows you every single request the browser sends to the server (including request for scripts, images, ... and also ajax async requests).
So now clear the list under "network" in firebog and trigger your ajax for the first time, see if there is any request sent to the server. if there is you can see if the response is a OK 200 response or a 500 server error and ofcourse you can see the full details of the error.
Hope this helps.
I have a button, which updates a value in the database. This value is used to determine what to draw on the page. Because of the page lifecycle though, the page redraws before the button click method is executed, meaning that any changes are not reflected until the page is reloaded again.
What's the best solution for this?
To clarify:
Page has a piece of text, that says "I like cats" if the database value is 1
button 'I hate cats' is pressed, which sets the database value to 0
the page reloads, but still says "I like cats"
the button click event is handled, and database value becomes 0
If the page is refreshed/reloaded, it now correctly says "I hate cats"
It should update when the button is clicked though.
you can use the page prerender event. this fire after the control event in the page lifecycle.
You can solve your problem by below code, use this end of button_click block
for ASPX:(C#)
Response.Redirect(HttpContext.Current.Request.Path);
Where are you querying the database? One easy option would just be to use Response.Redirect back to the page.
I think you could just get the result from the button method and just update a label/literal.
One possible solution is to execute a javascript function that updates the database (using ajax) when the OnClientClick event for the button is raised. In this case, when the page reaches the Page_Load event you will be able to render the appropriate content, as the postback happens after the script is executed. It should work fine if the database update takes a relatively small amount of time.
One curiosity, are using any anything like Page.IsPostBack { do something }.. If so, could you check if your update UI code is outside this check.