When I start a postback using __doPostBack, a file is created and going back to the user to download in the HttpContext.Current.Response.
Because I change the Response, the page including its javascript values is not modified
But when I have no file to output, the page is refreshed (because of the postback) and the javascript modification on the page are lost.
How can I ‘stop’ the postback from continuing and persist my current page? I can’t use an async postback, because I need the postback to let the user download the file.
EDIT: more info after some questions in the comments:
- The file is requested in a webservice request. The webservice needs
to execute a heavy query to determine if a file will be created. I
prefer that this only happens once. - The user can drag / drop some filters that will be used in the file
request. If no file is available, the user should be able to change
his filters, so thats why the page should not be changed.
From the W3 standards and RFC 2616:
Note the bolded line here. I have not tried it myself; however, setting the HTTP status to 204 and sending back an empty document, rather than trying to stop postback entirely, is certainly worth a shot.
Good luck, I hope this helps.
EDIT: this is the code that does the trick:
System.Web.HttpContext.Current.Response.StatusCode = 204;