我正在通过我的网络应用程序提供一个 docx 文件。这是为了响应 DropDownList 索引的更改而完成的,我将 AutoPostBack 设置为 true。我有许多可以通过复选框选择的选项,当下拉列表更改时,创建的文件基于选中的选项。
所以我正在做的是当事件触发时我会做一些魔术来创建一个文档然后提供它:
Protected Sub myDropDown_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles myDropDown.SelectedIndexChanged
' a bunch of things happen here but the end result is the contents of a docx file in a MemoryStream with the
' name mainStream
'here is where I set up the response and serve the file:
Response.ClearContent()
Response.ClearHeaders()
Response.AddHeader("content-disposition", "attachment; filename=document.docx")
Response.ContentEncoding = System.Text.Encoding.UTF8
Response.ContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
mainStream.WriteTo(Response.OutputStream)
mainStream.Close()
mainStream.Dispose()
Response.End()
现在我的问题是,此时我该怎么办?看来我必须调用 Response.End() 发送缓冲的内容,但这让我坐在一个页面上,选中了复选框,我的下拉列表显示了已更改的选项。如果我点击页面上导致回发的任何其他按钮,似乎我的下拉列表的选定索引更改事件再次触发。
我想做的是在发送文件后重定向。Response.redirect 将不起作用,因为 Response.End() 停止页面执行。谁能告诉我这种情况通常是如何处理的?谁能告诉我,如果页面上的另一个控件在提供文件后进行回发,为什么我的下拉菜单的选定索引事件会再次触发?任何建议都非常感谢。谢谢!