joetinger
joetinger

Reputation: 2729

response.redirect and response.end

I have some code that creates an excel sheet then prompts the user to save it somewhere and also attaches it to an email and sends it somewhere. It all works fine when I have Response.end() but I would like to remove this in order to redirect to a new page. When I remove it and replace it with response.redirect it doesn't prompt the user to save it. Any ideas why this is not prompting to save anymore?

code to create/save excel sheet

    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

last code that runs to redirect

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

Upvotes: 0

Views: 829

Answers (1)

Owen
Owen

Reputation: 501

What you're doing is sending a response for the save prompt, and sending a response of a redirect. You're only able to send one or the other in one response.

What you might be able to do instead is have the page where you are downloading from open in a new tab/window, then redirect the calling page to ~/Default.aspx.

Upvotes: 1

Related Questions