How can I stop the postback from refreshing the page on the client

When I start the postback using __doPostBack, the file is created and returned to the user for upload to HttpContext.Current.Response .

Since I am changing Response , the page, including its javascript values, is not changing

But when I don’t have a file for output, the page refreshes (due to postback) and the javascript modification on the page is lost.

How can I “stop” the postback from continuing and save the current page? I cannot use async postback because I need a postback so that the user can upload the file.

EDIT: more information after some questions in the comments:

  • The file is requested in the webservice request. A web service is needed to complete a heavy request to determine if a file will be created. I Assume this happens only once.
  • The user can drag and drop some filters that will be used in the request file. If the file is not accessible, the user should be able to change its filters, therefore, the page should not be changed.
+7
source share
1 answer

From the W3 and RFC 2616 standards:

10.2.5 204 No content. The server has completed the request, but it does not need to return the body of the entity and may want to return updated meta-information. The answer MAY include new or updated meta information in the form of entity headers, which, if present, SHOULD be associated with the requested option.
If the client is a user agent, he SHOULD NOT change the look of his document from what caused the request to be sent. This answer is primarily intended to allow the entry of actions without causing a change in the active viewing of the agent document, although any new or updated meta information MUST be applied to the document in the currently active user agent mode. Answer 204 MUST NOT include the body of the message, and thus always end first with an empty line after the header fields.

Pay attention to the bold. I have not tried it myself; however, setting the HTTP status to 204 and sending an empty document, instead of completely stopping the postback, it’s certainly worth taking a picture.

Good luck, hope this helps.

EDIT: this is the code that does the trick:
System.Web.HttpContext.Current.Response.StatusCode = 204;

+7
source

All Articles