LinkButton 在第二次单击时不进行回发
我希望客户能够下载 PDF 文件。因此,我在 LinkButton 上添加了以下代码:
标记:
<asp:LinkButton ID="lnkPrintHere" runat="server" OnClick="lnkPrintHere_Click" Text="Click here" />
代码隐藏:
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();
}
单击链接时第一次一切正常。后续点击不会引发 OnClick
事件。有谁知道为什么会发生这种情况?
PS:如果您需要更多信息,请随时询问。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您使用 Response.End() ,就会发生这种情况,
这里提到了这一点:
回发在以下时间后不起作用在 ASP.NET 中写入文件以响应
一种解决方法是使用查询字符串方法。当您单击链接按钮时,将重定向到带有查询字符串的同一页面。在页面加载中,如果检测到查询字符串,则发出 PDF 并返回。
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.
您始终可以尝试响应方法本身的替代方案,我将您的代码与 Response.Close 一起使用,并且工作正常。但我知道,这不是正确的选择,但是使用查询字符串进行下载也不是正确的选择。
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.