1

I want the clients to be able to download a PDF file. So I've put on a LinkButton with the code:

Markup:

<asp:LinkButton ID="lnkPrintHere" runat="server" OnClick="lnkPrintHere_Click" Text="Click here" />

Code behind:

protected void lnkPrintHere_Click(object sender, EventArgs e)
{
    Response.ContentType = "application/pdf";
    Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName);
    Response.TransmitFile("/_layouts/Files/" + fileName);
    Response.End();
}

Everything works fine the first time the link is clicked. Subsequent clicks don't raise the OnClick event. Has anyone any idea why this might be happening?

PS: Should you need more info, please feel free to ask.

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Gastón
  • 167
  • 2
  • 10

2 Answers2

0

This happens if you use Response.End()

This is mentioned here : Post Back does not work after writing files to response in ASP.NET

One workaround is to use a query string approach. When you click linkbutton redirect to same page with query string. And in page load, if you detect query string, emit the PDF and return.

Community
  • 1
  • 1
Madhur Ahuja
  • 22,211
  • 14
  • 71
  • 124
  • Oddly enough,I had already tried to run the same code without the Response.End() and it didn't worked. I'm not very keen on using query strings on this particular solution, but should nothing else work, I'll give it a try. – Gastón Dec 28 '10 at 20:34
  • When you say didn't worked, you mean you saw the same problem ? You can check out the same thread for alternatives but from my personal experience, there is no clean way out. – Madhur Ahuja Dec 28 '10 at 20:38
  • Yes, I meant the same problem. I'll take a look at other options as you said. Thanks! – Gastón Dec 28 '10 at 20:47
0

You can always try alternatives in the response method itself, I used your code with Response.Close and its working fine. But I know, this is not the right option , but using query string for download is not a right option too.

user555320
  • 21
  • 1
  • 1
    Why is using a query-string for downloads not the right option? That's pretty much exactly what the GET verb is for, particularly as the example he gave is idempotent. – RB. Dec 28 '10 at 21:15