如何阻止回发刷新客户端页面
当我使用 __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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
来自 W3 标准和 RFC 2616:
请注意此处的粗体线。我自己没有尝试过;然而,将 HTTP 状态设置为 204 并发回一个空文档,而不是尝试完全停止回发,当然值得一试。
祝你好运,我希望这会有所帮助。
编辑:这是执行此操作的代码:
System.Web.HttpContext.Current.Response.StatusCode = 204;
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;