如何阻止回发刷新客户端页面

发布于 2024-12-24 22:53:15 字数 466 浏览 1 评论 0原文

当我使用 __doPostBack 启动回发时,会创建一个文件并返回给用户以在 HttpContext.Current.Response 中下载。

因为我更改了 Response,所以页面(包括其 javascript 值)不会被修改

但是当我没有要输出的文件时,页面会刷新(由于回发)并且页面上的 javascript 修改会丢失。

如何“阻止”回发继续并保留当前页面?我无法使用异步回发,因为我需要回发来让用户下载文件。

编辑:评论中的一些问题后的更多信息:

  • 该文件是在网络服务请求中请求的。网络服务需要 执行繁重的查询以确定是否将创建文件。我 更希望这只发生一次。
  • 用户可以拖放一些将在文件中使用的过滤器 要求。如果没有可用的文件,用户应该能够更改 他的过滤器,所以这就是为什么不应该更改页面的原因。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

听不够的曲调 2024-12-31 22:53:15

来自 W3 标准和 RFC 2616:

10.2.5 204 No Content 服务器已满足请求,但不需要返回实体主体,并且可能希望返回更新后的内容
元信息。响应可能包括新的或更新的
实体标题形式的元信息,如果存在的话应该
与请求的变体相关联。
如果客户端是用户代理,则它不应更改导致发送请求的文档视图。此响应是
主要目的是允许输入操作的发生而无需
导致用户代理的活动文档视图发生变化,尽管
任何新的或更新的元信息都应应用于该文档
当前位于用户代理的活动视图中。 204 响应不得
包含消息体,因此总是由第一个消息体终止
标题字段后的空行。

请注意此处的粗体线。我自己没有尝试过;然而,将 HTTP 状态设置为 204 并发回一个空文档,而不是尝试完全停止回发,当然值得一试。

祝你好运,我希望这会有所帮助。

编辑:这是执行此操作的代码:
System.Web.HttpContext.Current.Response.StatusCode = 204;

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;

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文