response.redirect和response.end

时间:2013-03-06 21:59:12

标签: asp.net httpresponse

我有一些代码可以创建excel表格,然后提示用户将其保存在某个地方,并将其附加到电子邮件中并将其发送到某个地方。当我有Response.end()时,一切正常,但我想删除它以重定向到新页面。当我删除它并用response.redirect替换它时,它不会提示用户保存它。任何想法为什么这不会促使你再保存?

创建/保存Excel工作表的代码

    Protected Sub ExportToExcel(sender As Object, e As EventArgs, ByVal strPath As String)
    Response.ClearContent()

    Response.AddHeader("content-disposition", "attachment; filename=GridViewToExcel.xls")

    Response.ContentType = "application/excel"

    Dim sWriter As New StringWriter()

    Dim hTextWriter As New HtmlTextWriter(sWriter)

    Dim hForm As New HtmlForm()

    Panel1.Parent.Controls.Add(hForm)

    hForm.Attributes("runat") = "server"

    hForm.Controls.Add(Panel1)

    hForm.RenderControl(hTextWriter)

    ' Write below code to add cell border to empty cells in Excel file
    ' If we don't add this line then empty cells will be shown as blank white space

    Dim sBuilder As New StringBuilder()

    sBuilder.Append("<html xmlns:v=""urn:schemas-microsoft-com:vml"" xmlns:o=""urn:schemas-microsoft-com:office:office"" xmlns:x=""urn:schemas-microsoft-com:office:excel"" xmlns=""http://www.w3.org/TR/REC-html40""> <head><meta http-equiv=""Content-Type"" content=""text/html;charset=windows-1252""><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>ExportToExcel</x:Name><x:WorksheetOptions><x:Panes></x:Panes></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head> <body>")
    sBuilder.Append(Convert.ToString(sWriter) & "</body></html>")

    Response.Write(sBuilder.ToString())

    Dim fStream As FileStream = File.Create(strPath)
    fStream.Close()
    Dim Writer As New StreamWriter(strPath)
    Writer.Write(sBuilder)
    Writer.Flush()
    Writer.Close()
End Sub

运行以重定向的最后一个代码

Response.Redirect("~/Default.aspx")

1 个答案:

答案 0 :(得分:1)

您正在做的是发送保存提示的响应,并发送重定向响应。您只能在一个回复中发送一个或另一个。

您可以执行的操作是在新标签/窗口中打开要从中打开的页面,然后将调用页面重定向到~/Default.aspx