如何关闭asp.net页面窗口? [英] How to close a asp.net page window?

查看:104
本文介绍了如何关闭asp.net页面窗口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好朋友,

场景:

我们有一个aspx页面,其中包含两个按钮控件.在第一个按钮的按钮单击事件中,将生成excel文件并提示打开/保存对话框.

要求:

生成excel文件并弹出打开/保存"对话框后,应关闭主页窗口(具有两个按钮控件).

我们正在使用以下代码返回excel文件:

Hello Friends,

Scenario:

We have an aspx page which contains two button controls. On button click event of first button, excel file is getting generated and open/save dialog will be prompted.

Requirement:

Once the excel file generated and open/save dialog pops up the main page window (which has two button controls) should be closed.

We are using the following code to return excel file:

Response.Clear();
//Response.ContentType = "application/vnd.ms-excel";
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
string excelReportName = "MpmCostDataTemplate";
Response.AddHeader("Content-Disposition", "attachment; filename=" + excelReportName + ".xlsx");




之后,应关闭主窗口.

我们正在使用以下语句来关闭主窗口,当删除与提示excel文件打开/保存对话框相关的代码时,该窗口将起作用:




After this the main window should be closed.

We are using the following statement to close the main window which is working when we remove code related to prompt excel file open/save dialog:

Page.ClientScript.RegisterStartupScript(this.GetType(), "myCloseScript", "window.close();", true);



如果我们有Excel打开/保存对话框代码,则该语句不起作用.

您能帮我们解决这个问题吗?

谢谢
Raheem MA



This statement is not working in case if we have Excel open/save dialog code.

Can you please help us to get rid of the issue.

Thank you,
Raheem MA

推荐答案

1. 如果您要从服务器代码中设置以下响应类型,则浏览器显然会忽略您的窗口关闭脚本(因为javascript代码具有不同的响应类型),这是肯定的.
1. If you are setting the following response type from the server code, browser is obviously going to ignore your window close script (Because, the javascript code is of a different response type), that''s for sure.
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";



因此,实际上不可能以这种方式关闭窗口.

2.
从理论上讲,您使用ajax使用异步请求从服务器获取文件并让用户下载文件,这听起来非常好.首先,这是不可能的,因为文件下载的Ajax响应不会简单地起作用.即使可行,您的主要问题也无法解决(关闭窗口).为什么?参见下文.

3.
获得ajax行为的另一种方法是使用iframe从服务器下载文件.参见 http://encosia.com/2007/02/23/ajax-file- downloads-and-iframes/ [ ^ ].这说明了如何动态使用iframe(使用javascript在dom中添加了创建iframe的功能.irame指向响应时写入文件的URL),当用户单击下载按钮时,从服务器下载文件.实际上,这类似于"ajax"行为,因为当将下载请求发送到服务器并出现下载提示时,您仍然是主窗口中的javascript.但是您会看到以下问题:

-如果您在DOM中设置了iframe(用于下载)后关闭浏览器,则将无法下载(因为,在出现下载提示之前,该窗口将关闭)

-如果您要等待下载以某种方式完成(使用计时器或其他方式),则无法进行此操作.因为,您不知道实际下载何时完成,或者用户是否单击确定"或取消"按钮.该提示不会给您任何事件,您可以订阅以获取有关发生的情况的通知.

因此,总结是,鉴于当前浏览器的功能,在我看来这不是可以实现的要求.抱歉,我的朋友.



So, its virtually not possible to close the window in this way.

2.
Theoretically, it sounds very good that, u use an asynchronous request using ajax to get the file from the server and let user download it. First of all, this is not possible because the Ajax response for file download wouldn''t simply work. Even if it would work, your main problem wouldn''t be solved (Closing the window). Why? see below.

3.
An alternative to get ajax behaviour is to use an iframe for downloading file from server. See http://encosia.com/2007/02/23/ajax-file-downloads-and-iframes/[^]. This shows how to use an iframe on the fly (Using javascript an iframe is created added in the dom. The irame is pointed to an URL that writes the file in response) to download a file from the server, when user clicks the download button. This actually mimics the "ajax" behavior, as, you are still the javascript in the main window when the download request is sent to server and the download prompt appears. But you will see the following issues:

--If you close the browser after setting the iframe (For download) in the DOM, you won''t be able to download (Cause, the window will be closed before the download prompt appears)

--If you want to wait for the download to complete somehow (Using timer or something), this is not possible. Because, you don''t know when the actual download is going to be complete, or, whether user clicked the "OK" or "Cancel" button. That prompt does not give you any event that you can subscribe to get notification of what happened.

So, the summary is, given the current browser''s functionality, this looks to me not a requirement that is possible to implement. Sorry, my friend.


这是因为"Response.Clear();"这一行.

完成此操作后,它将删除实际上包含提到的JS的早期响应.
It is because of ''Response.Clear();'' line.

Once you do that, it erases the earlier response that actually contains the JS mentioned.


您尝试做的是正确的.似乎没有任何问题response.clear().

问题是,当您单击该按钮时,它将向服务器发送请求,并且您正在从服务器发送响应的内容类型为浏览器必须显示保存对话框的内容类型,这不是正常的html响应,它可能由浏览器处理.因此,当您注册用于关闭窗口的脚本时,浏览器会忽略该脚本,因为响应的类型不是正常的html处理应由浏览器处理的类型.

我想以这种方式实现此目标是不可能的,因为您要发送具有不同内容类型的响应的脚本.

您可以在此处执行的操作是,单击button1时,而不是在服务器端处理该事件,而是在客户端处理该事件.只需调用javascript函数,该函数应异步调用其他可能返回excel文件的页面(使用与您提到的代码相同的页面).发出ajax调用后,可以调用window.close().

这样,您将获得xsl文件,浏览器窗口也将关闭.让我知道您是否仍然遇到此问题.
What you are trying to do is correct. There doesn''t seem any issue with the response.clear().

The issue is that when you click on the button, it sends a request to the server and from the server you are sending the response with the contenttype for which browser gotta show the save dialogue box, it''s not normal html response that could be handled by browser. Therefore when you register the script for closing the window, it is overlooked by the browser because the response was not of the type that should be handled by the browser for normal html processing.

I think it''s not possible to achieve it in this way when you want to send the script with the response that has different content type.

What you can do here is that when you click on the button1, instead of handling the event on the server side handle it on the client side. Just call a javascript function, that should make an asynchronous call to the the same page of some other that could return your excel file (use the same code as you mentioned). After placing the ajax call you can call window.close().

This way you would be get the xsl file and browser window will also be closed. Let me know if you still face some issue with this.


这篇关于如何关闭asp.net页面窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆