Search code examples
c#asp.netc#-4.0threadabortexception

Execute code after CompleteRequest()


I have to execute some more code after downloading a file, but itsn't happening. The compiler hits ClearControls(); after DownloadFile();, but the function is not executed. On the page I get the prompt to open or save the file. But the textboxes are not cleared. What should be done in this case.

protected void btnOk_Click(object sender, EventArgs e)
{
//ClearControls(); // not working here too.
DownloadFile();
ClearControls();
}

private void DownloadFile()
{
Response.ContentType = "application/pdf";
Response.AppendHeader("Content-Disposition","attachment;filename="+sName+".pdf");
Response.TransmitFile(Server.MapPath("~/documents/ready/" + strPdfName));
//Response.End();
HttpContext.Current.ApplicationInstance.CompleteRequest();
}

private void ClearControls()
{
  txtOne.Text="";
  txtTwo.Text="";
}

Solution

  • Your download is basically overriding the writing to the browser, you're getting one response back and it's the file with all the header information.

    You're kinda stuck if you really need to update the form at this point. Best bet would be to open the download in a new tab or window using javascript or ajax passing in the values you need via the querystring.