Skip Navigation LinksHome / Community & Support / Developer Productivity Tools Forums / ASP.NET AJAX > Window > confirmation when closing

Not answered confirmation when closing

Feed from this thread
  • Benjamin avatar

    Posted on Apr 7, 2011 (permalink)

    hi all,


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



    thanks,
    benjamin

    Reply

  • Posted on Apr 8, 2011 (permalink)

    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.

    Reply

  • Albert Shenker Master avatar

    Posted on Jun 16, 2011 (permalink)

    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.

    Reply

  • Cori Master avatar

    Posted on Jun 17, 2011 (permalink)

    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.

    Reply

  • Albert Shenker Master avatar

    Posted on Jun 21, 2011 (permalink)

    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.

    Reply

  • Marin Bratanov Marin Bratanov admin's avatar

    Posted on Jun 22, 2011 (permalink)

    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.

    Reply

  • Albert Shenker Master avatar

    Posted on Feb 2, 2012 (permalink)

    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);

    Reply

  • Marin Bratanov Marin Bratanov admin's avatar

    Posted on Feb 6, 2012 (permalink)

    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 >>
    Attached files

    Reply

Back to Top

Skip Navigation LinksHome / Community & Support / Developer Productivity Tools Forums / ASP.NET AJAX > Window > confirmation when closing
Related resources for "confirmation when closing"

ASP.NET Window Features  |  Documentation  |  Demos  |  Telerik TV  |  Self-Paced Trainer  |  Step-by-step Tutorial  ]