Programmatically close aspx page from code behind

前端 未结 13 2110
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-29 05:05

What is the best way to close an ASPX page from the code-behind?

I have a button event handler that I want to close the page after the user has clicked an ASP.NET

13条回答
  •  攒了一身酷
    2020-11-29 05:45

    You would typically do something like:

    protected void btnClose_Click(object sender, EventArgs e)
    {
        ClientScript.RegisterStartupScript(typeof(Page), "closePage", "window.close();", true);
    }
    

    However, keep in mind that different things will happen in different scenerios. Firefox won't let you close a window that wasn't opened by you (opened with window.open()).

    IE7 will prompt the user with a "This page is trying to close (Yes | No)" dialog.

    In any case, you should be prepared to deal with the window not always closing!

    One fix for the 2 above issues is to use:

    protected void btnClose_Click(object sender, EventArgs e) {
        ClientScript.RegisterStartupScript(typeof(Page), "closePage", "window.open('close.html', '_self', null);", true);
    }
    

    And create a close.html:

    
     
     
     
     
         

    Please Wait...

    Note that close.html will redirect to default.aspx if the window does not close after 2 sec for some reason.

提交回复
热议问题