How to close popup window RRS feed

  • Question

  • User615288127 posted

    I'm using Visual Web Developer 2010 Express with SQL Server 2008 Express, VB.

    A two part question:

    1. I have an area where I want to open a popup window, indicating work in progress, and after the work is completed automatically close the window.  My code is below and opens the window fine but does not close it.  Hopefully someone has a solution for me.

    2. How can I change the window.open to center the popup rather than specifying coordinates?

        Protected Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click
            Dim script As String = "window.open('Test3.aspx','_blank','height=200,width=300,left=250,top=250,scrollbars=no,toolbar=no, directories=no, status=no, menubar=no, resizable=no, copyhistory=no');"
            ScriptManager.RegisterStartupScript(Me, Me.GetType(), "popup", script, True)
            System.Threading.Thread.Sleep(8000)   'In reality doing other tasks at this point
            Dim script1 As String = "window.close();"
            System.Web.UI.ScriptManager.RegisterClientScriptBlock(Page, Me.[GetType](), "CloseWindow", script1, True)
        End Sub


    Saturday, August 22, 2015 12:32 AM


  • User281315223 posted

    One of the issues here is that all of the code within your Button Click handler is going to execute at the same time (so the Thread.Sleep() part is really only delaying this behavior). The script to actually open your window and then to close it will both be added to the response simultaneously, which could explain why the close isn't occurring.

    What you might consider doing would be to define a WebMethod that you could call via jQuery / Javascript and display something while it is executing and then within the complete / success event, perform something (e.g. hide a window, display a success message, etc.)

    2. How can I change the window.open to center the popup rather than specifying coordinates?

    Sadly if you are going to be using an actual new window (and not a Javascript driven modal popup), you'll need to supply some coordinates. However, you should be able to use a little bit of math to handle this :

    var left = (screen.width/2)-(w/2);
    var top = (screen.height/2)-(h/2);
    window.open(..., '_blank', '...,top=' + top + ',left=' + left);

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, August 22, 2015 1:24 PM