简体   繁体   English

以编程方式从后面的代码关闭aspx页面

[英]Programmatically close aspx page from code behind

What is the best way to close an ASPX page from the code-behind? 从代码隐藏中关闭ASPX页面的最佳方法是什么?

I have a button event handler that I want to close the page after the user has clicked an ASP.NET button on the page. 我有一个按钮事件处理程序,我想在用户单击页面上的ASP.NET按钮后关闭页面。 I have tried to programmatically add a JavaScript method that contains a window.close() command to the OnClientClick event to close the page but it does not work. 我试图以编程方式将包含window.close()命令的JavaScript方法添加到OnClientClick事件以关闭页面但它不起作用。 The button is also a asp:AsyncPostBoskTrigger for an AJAX Update Panel. 该按钮也是AJAX更新面板的asp:AsyncPostBoskTrigger

The application uses .NET Framework 3.5. 该应用程序使用.NET Framework 3.5。

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() ). Firefox不会让你关闭一个你没有打开的窗口(用window.open()打开)。

IE7 will prompt the user with a "This page is trying to close (Yes | No)" dialog. IE7将提示用户“此页面正在尝试关闭(是|否)”对话框。

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: 上述2个问题的一个解决方法是使用:

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: 并创建一个close.html:

<html><head>
 <title></title>
 <script language="javascript" type="text/javascript">
     var redirectTimerId = 0;
     function closeWindow()
     {
         window.opener = top;
         redirectTimerId = window.setTimeout('redirect()', 2000);
         window.close(); 
     }

     function stopRedirect()
     {
         window.clearTimeout(redirectTimerId);
     }

     function redirect()
     {
         window.location = 'default.aspx';
     }
 </script>
 </head>
 <body onload="closeWindow()" onunload="stopRedirect()" style="">
     <center><h1>Please Wait...</h1></center>
 </body></html>

Note that close.html will redirect to default.aspx if the window does not close after 2 sec for some reason. 请注意,如果由于某种原因窗口在2秒后没有关闭,close.html将重定向到default.aspx。

 protected void btnOK_Click(object sender, EventArgs e)
        {

          // Your code goes here.
          if(isSuccess)
          {
                  string  close =    @"<script type='text/javascript'>
                                window.returnValue = true;
                                window.close();
                                </script>";
            base.Response.Write(close);
            }

        }

UPDATE: I have taken all of your input and came up with the following solution: 更新:我已经完成了所有的输入并提出了以下解决方案:

In code behind: 代码背后:

protected void Page_Load(object sender, EventArgs e)    
{
    Page.ClientScript.RegisterOnSubmitStatement(typeof(Page), "closePage", "window.onunload = CloseWindow();");
}

In aspx page: 在aspx页面中:

function CloseWindow() {
    window.close();
}

A postback is the process of re-loading a page, so if you want the page to close after the postback then you need to set your window.close() javascript to run with the browser's onload event during that postback, normally done using the ClientScript.RegisterStartupScript() function. 回发是重新加载页面的过程,因此如果您希望页面在回发后关闭,那么您需要设置window.close()javascript以在回发期间使用浏览器的onload事件运行,通常使用ClientScript.RegisterStartupScript()函数。

But are you sure this is what you want to do? 但你确定这是你想做的吗? Closing pages tends to piss off users. 关闭页面往往会惹恼用户。

您应该注入一个启动脚本,该脚本将在回发完成后关闭页面。

ClientScript.RegisterStartupScript(typeof(Page), "closePage", "<script type='text/JavaScript'>window.close();</script>"); 

You just need to add this property in your asp:Button element (for example): 您只需要在asp:Button元素中添加此属性(例如):

OnClientClick="javascript:window.close();"

It works perfectly. 它完美地运作。

If you using RadAjaxManager then here is the code which helps: 如果您使用RadAjaxManager,那么这里的代码有助于:

RadAjaxManager1.ResponseScripts.Add("window.opener.location.href = '../CaseManagement/LCCase.aspx?" + caseId + "'; 
window.close();");

您可以通过简单地粘贴标记中按钮的OnClientClick事件中的窗口关闭代码来关闭窗口

For anyone wondering why they cannot get the provided answers to work it's because the page must have been opened by javascript in order to be closed by javascript. 对于任何想知道为什么他们无法获得提供的答案工作的人,因为页面必须已经通过javascript打开才能被javascript关闭。

Since most people finding this asp question are likely using an asp:hyperlink or an asp redirect of some sort to navigate to the page that needs to be closed. 由于大多数人发现这个asp问题可能使用asp:hyperlink或某种asp重定向导航到需要关闭的页面。 These methods of redirection don't use javascript and therefore will not close by javascript. 这些重定向方法不使用javascript,因此不会通过javascript关闭。

I found a simple solution for my application and that's eliminating the NavigateUrl and using the asp:Hyperlink.Attributes to add an onclick to the hyperlink which uses java script to open the window that needs to be closed by javascript. 我为我的应用程序找到了一个简单的解决方案,它正在消除NavigateUrl并使用asp:Hyperlink.Attributesasp:Hyperlink.Attributes添加onclick ,该超链接使用java脚本打开需要通过javascript关闭的窗口。

aspHyperlink.NavigateUrl = "https://www.google.com";

The above NavigateUrl is removed and instead we attach our click event. 上面的NavigateUrl已删除,我们将附加点击事件。

aspHyperlink.Attributes.Add("onclick", "javascript:openInNewTab('https://www.google.com');");

And in the code behind of the page containing our aspHyperlink we have the javascript for opening the url provided by Rinto 在包含我们的aspHyperlink的页面后面的代码中,我们有用于打开Rinto提供的URL的javascript

function openInNewTab(url) {
         var win = window.open(url, '_blank');
         win.focus();
     } 

Now all pages opened with openInNewTab can be closed with the provided answers. 现在,使用openInNewTab打开的所有页面都可以使用提供的答案关闭。

window.close();

For closing a asp.net windows application, 关闭asp.net windows应用程序,

  • Just drag a button into the design page 只需将按钮拖到设计页面即可
  • Then write the below given code inside its click event 然后在其click事件中写下以下给定的代码

      " this.close(); " 

ie: 即:

 private void button2_Click(object sender, EventArgs e)  
 {
        this.Close();
 }
  protected void Button1_Click(object sender, EventArgs e)
  {
    Button1.Attributes.Add("onclick"," CloseWindow();");
  }

    <script type="text/javascript">
        function CloseWindow() 
      {
         window.close();
      }
    </script>

You can just simply use this.. short and easy. 你可以简单地使用它......简单而容易。

Response.Write("<script>window.close();</script>");

Hope this helps. 希望这可以帮助。

if you are opening page on JavaScript popup then 如果你打开JavaScript弹出页面然后

 Response.Write("<script>javascript:window.close();</script>");

will do the job 会做的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM