3

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?

tshepang
  • 12,111
  • 21
  • 91
  • 136
eych
  • 1,262
  • 2
  • 16
  • 37

4 Answers4

4

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();
Radu Simionescu
  • 4,518
  • 1
  • 35
  • 34
2

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.

Oded
  • 489,969
  • 99
  • 883
  • 1,009
  • tried this, but it still leaves an empty window up. (also tried Clear) – eych Nov 16 '10 at 20:21
  • @eych - What does the HTML source look like? Are you getting any javascript errors? Some windows cannot be closed from script (for instance, normally, only if you opened the window with a script, could you close it by script as well). – Oded Nov 16 '10 at 20:22
  • window was originally opened with script, and there are no javascript errors. I have a separate 'Close' button that when clicked, also calls the same RegisterClientScript(...) method, and that works, closing the form [I guess since there's no more processing after the Button_Click]. – eych Nov 16 '10 at 20:28
  • @Oded - Which source? Source of the blank window? If you need the source of the original, it is on another machine. Anything particular you're looking for? – eych Nov 16 '10 at 20:34
  • @eych - If you are getting an empty HTML page, nothing in it, then the script was not output to the page. I want to confirm that, that's all. – Oded Nov 16 '10 at 20:35
  • yes, it's empty... – eych Nov 16 '10 at 21:00
  • 1
    @eych - try having *Response.Write("");* maybe RegisterClientScriptBlock is being ignored due to Ending the response. – Shadow The GPT Wizard Nov 17 '10 at 10:15
1

one solution, kludgey, but can be used elsewhere:

Response.Redirect("close.html")

where close.html just has

  <script>
  window.close();
  </script>
eych
  • 1,262
  • 2
  • 16
  • 37
0

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.. :)

Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208