closing a window after Response.End

2019-05-27 22:11发布

e.g. I have a form with a ListView that is in edit mode.
Something happens so that the table the Listview is using is no longer available.
I just want to be able to close the window if the user hits 'save'.

In Page_Load, I check if the table is available, if not, I call RegisterClientScriptBlock(type,name,"window.close()"). However, processing still occurs, and it goes to ListView1_ItemUpdating event.

In Page_Load, if the table doesn't exist, I can call Response.End to stop processing, however, the window still stays up since the script never registered.

Any way to stop processing and close the window without having to put a custom IsTableValid() check in all of my methods?

4条回答
Fickle 薄情
2楼-- · 2019-05-27 22:15

one solution, kludgey, but can be used elsewhere:

Response.Redirect("close.html")

where close.html just has

  <script>
  window.close();
  </script>
查看更多
贼婆χ
3楼-- · 2019-05-27 22:21

Try this instead:

HttpContext.Current.ApplicationInstance.CompleteRequest();

Microsoft themselves say that Response.End is there only for backward compatibility:

This method is provided only for compatibility with ASP—that is, for compatibility with COM-based Web-programming technology that preceded ASP.NET

Not sure it will solve your problem but at least Microsoft won't have an excuse.. :)

查看更多
Juvenile、少年°
4楼-- · 2019-05-27 22:32

The answer provided by Oded doesn't work from Page_Load, I don't know why. The answer provided by eych works. Yet, if you don't want to keep an extra html file and make a redirect, you can use something like:

Response.Clear();
Response.Write("<script>window.close();</script>");
Response.Flush();
Response.End();
查看更多
Explosion°爆炸
5楼-- · 2019-05-27 22:37

Flush the response to send all data to the browser before ending it:

RegisterClientScriptBlock(type,name,"window.close()")
Response.Flush()
Response.End()

You may want to Clear the response before registering the script, in order to ensure that there is nothing else in the response buffer.

There are also ClearHeaders and ClearContent methods if you only want to clear one and not the other.

查看更多
登录 后发表回答