This is a migrated thread and some comments may be shown as answers.

confirmation when closing

7 Answers 171 Views
Window
This is a migrated thread and some comments may be shown as answers.
Benjamin
Top achievements
Rank 1
Benjamin asked on 08 Apr 2011, 04:06 AM
hi all,


I need to show confirmation when closing window. how to do ?



thanks,
benjamin

7 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 08 Apr 2011, 06:02 AM
Hello Benjamin,

You could subscribe to the event OnClientBeforeClose and call the confirm from there.
Here is a sample code:
aspx:
<telerik:RadWindow  ID="id1" OnClientBeforeClose="OnClientBeforeClose"  runat="server" OpenerElementID="Button1"></telerik:RadWindow>

Javascript:
   function OnClientBeforeClose(sender, args)
  {
       args.set_cancel(!confirm('Are you sure...?') );
  }

Thanks,
Shinu.
0
Albert Shenker
Top achievements
Rank 1
Veteran
Iron
answered on 16 Jun 2011, 09:31 PM
The solution provide here works. However, i am trying to do this with radconfirm instead of confirm. I have the following:

function OnBeforeClose(sender, eventArgs) {
function callbackFunction(eventArgs) {
     if (eventArgs) {
        sender.remove_beforeClose(OnBeforeClose); 
                    sender.close();
     }
  }
        

eventArgs.set_cancel(

true);

 

radconfirm(
'Are you sure you want to close this window? Any unsaved changes will be lost.', callbackFunction, 400, 140, null, 'Close Window');

}


The first time I open and close the window, everything works fine. The radconfirm is displayed and if the user selects "Yes" the window closes. However, if they immediately click on the link that opens the rad window again, then if they click on the window Close "Button", the window closes without the confirmation being shown. I am guessing it has something to do with the

sender.remove_beforeClose(OnBeforeClose); 
line,

but if I omit this, the confirm keeps popping up over and over again.
0
Cori
Top achievements
Rank 2
answered on 17 Jun 2011, 12:47 PM
Hello Albert,

After the line, sender.close();, why don't you add sender.add_beforeClose(OnBeforeClose) to add back the event handler.

I hope that helps.
0
Albert Shenker
Top achievements
Rank 1
Veteran
Iron
answered on 21 Jun 2011, 05:13 PM
That did the trick. I did run into one issue, though. When the page loads in the popup RadWindow, I do some verification to  see if the required data exists. If it does not, I display an alert to the user and close the window automatically. It seems that this forced closure is causing the close confirmation to display. The javascript which handles the close confirmation is on the main page, not on the page that loads in the radwindow (ie not in the place where the logic for autoclosing is determined). I'm wondering if there is any way to handle this scenario. Even if I were not to auto close the popup in this situation, when the user chose to close it manually, the confirm dialog would appear. Again, since the user never looked at anything that could be saved, this is not an ideal experience for them.
0
Marin Bratanov
Telerik team
answered on 22 Jun 2011, 08:57 AM
Hi Albert,

  You can determine whether the window should be closed automatically in the page that loads it in its pageLoad event and then call a JavaScript function from the main window that adds the confirmation function (handler for the OnClientBeforeClose event) if needed or directly close the window.

For your convenience I created and attached a simple page illustrating the approach.


Best wishes,
Marin
the Telerik team

Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.

0
Albert Shenker
Top achievements
Rank 1
Veteran
Iron
answered on 02 Feb 2012, 10:58 PM
Thanks for the project. I know its been a while but I have run into a new scenario. I have a dialog which opens in a RadWindow. I open the dialog window using radopen. The dialog has a "Close" button and a "Save" button. If the user clicks on "Close" I wish to display the confirmation dialog. If they click on "Save", I wish to perform some server-side actions and close the window when complete without a confirmation. I have got it working so that the confirmation appears when they click on "Close", or if they click on the rad window "X" icon for that matter.

On the parent page, I have:

function openWindow() {
  
var oWin =  window.radopen(url, name);                    
oWin.add_beforeClose(onBeforeCloseConfirm)                     
}
  
function onBeforeCloseConfirm(sender, eventArgs) {
    function callbackFunction(eventArgs) {
        if (eventArgs) {
            sender.remove_beforeClose(onBeforeCloseConfirm);
            sender.close();
            sender.add_beforeClose(onBeforeCloseConfirm);
        }
    }
    eventArgs.set_cancel(true);
      
    setTimeout(function() {
        var oConfirm = radconfirm('Are you sure you want to close this window? Any unsaved changes will be lost.', callbackFunction, 400, 140, null, 'Close Window');
    }, 0);
      
}


if they click on the "X" or on "Close" the confirmation is displayed.

However, when I try to explicitly close the window after the server-side code is run (by using

ClientScript.RegisterClientScriptBlock and calling closeActiveWindow:

function getParentRadWindow()   
{
    var oWindow = null;   
    if (window.radWindow) oWindow = window.radWindow;
    else if (window.frameElement.radWindow) oWindow = window.frameElement.radWindow;
    else if (parent.radWindow) oWindow = parent.radWindow;
    return oWindow;   
}  
  
function closeActiveWindow() {
    var oWin = getParentRadWindow();
    oWin.close();
}


I get the confirmation to show, which I don't want.  I tried to insert the following prior to calling closeActiveWindow() in the server side close call;

var oWin = getParentRadWindow();
oWin.remove_beforeClose(onBeforeCloseConfirm);
closeActiveWindow()

but I get a "too much recursion " javascript error.

I also tried the following and got the same error:
var oWin = getParentRadWindow(); 
oWin.remove_beforeClose(onBeforeCloseConfirm); 
closeActiveWindow() 
sender.add_beforeClose(onBeforeCloseConfirm);

0
Marin Bratanov
Telerik team
answered on 06 Feb 2012, 02:35 PM
Hi Albert,

What I would advise is that you add the BeforeClose handler only once - when you open the RadWindow. You do not need it in the callback function of the RadConfirm - if you choose OK it will be closed and the opening function will add it if called, if you click Cancel you have not removed it.

As for the recursion error - this is simply because you have created an endless recursion. What you would have needed to do is to call oWin.close() instead of closeActiveWindow() from within itself. What I would advise is that you call a function from the parent page that will remove the handler if you want to close it programmatically without a confirmation. One again - if reopened the opening function will add the handler, so you needn't add ti again when closing.

These changes show the proper behavior on my end: http://screencast.com/t/C5ZFXB69. I am also attaching my test pages here as a reference. I hope they help.


Regards,
Marin
the Telerik team
Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
Tags
Window
Asked by
Benjamin
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Albert Shenker
Top achievements
Rank 1
Veteran
Iron
Cori
Top achievements
Rank 2
Marin Bratanov
Telerik team
Share this question
or