Reputation: 2433
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:
Upvotes: 7
Views: 1270
Reputation: 13010
From the W3 standards and RFC 2616:
10.2.5 204 No Content The server has fulfilled the request but does not need to return an entity-body, and might want to return updated metainformation. The response MAY include new or updated metainformation in the form of entity-headers, which if present SHOULD be associated with the requested variant.
If the client is a user agent, it SHOULD NOT change its document view from that which caused the request to be sent. This response is primarily intended to allow input for actions to take place without causing a change to the user agent's active document view, although any new or updated metainformation SHOULD be applied to the document currently in the user agent's active view. The 204 response MUST NOT include a message-body, and thus is always terminated by the first empty line after the header fields.
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;
Upvotes: 7