asp:Gridview中的超链接在新的浏览器窗口中打开PDF [英] asp:Hyperlink in Gridview open PDF in new browser window

查看:130
本文介绍了asp:Gridview中的超链接在新的浏览器窗口中打开PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有一个gridview可以动态创建PDF文件的asp:Hyperlinks.现在,URL只是将文件下载到本地计算机.使它们在新窗口中打开以便用户可以选择打印或下载的最简单方法是什么. Chrome会自动下载它. IE要求打开或保存它.我只希望它在浏览器窗口中打开.

I currently have a gridview that dynamically creates asp:Hyperlinks to PDF files. Right now, the url just downloads the file to the local machine. What's the easiest way to get them to open in a new window so the user has the option to print or download. Chrome automatically downloads it. IE asks to open or save it. I just want it to open in a browser window.

<asp:TemplateField HeaderText="Name">
    <ItemTemplate>
        <asp:HyperLink ID="lblProductName" runat="server" Text='<%# Eval("name") %>' NavigateUrl="#" Target="_blank"  ></asp:HyperLink>
    </ItemTemplate>
</asp:TemplateField>

然后在后面的代码中创建实际的NavigateUrl

And the actual NavigateUrl gets created in the code behind

HyperLink lblProductName = (HyperLink)e.Row.FindControl("lblProductName");
lblProductName.NavigateUrl = urlLink;

推荐答案

添加一个新页面,例如DownloadFile.aspx并添加以下代码

Add a new page say DownloadFile.aspx and add the following code

protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
    string fileName = Request.QueryString["pdffile"];
    string path = Server.MapPath("~/PDFs/") + fileName;
    Response.ContentType = "application/pdf";
    Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName);
    Response.WriteFile(path);
    Response.Flush();
    Response.End();
}
}

现在在GridView中删除LinkBut​​ton并添加HyperLink,如下所示

Now in GridView remove LinkButton and add HyperLink as shown below

<asp:HyperLink ID="HyperLink1" runat="server" Target = "_Blank" NavigateUrl='<%# Eval("FileName","DownloadFile.aspx?PDFFile={0}") %>'></asp:HyperLink>

这篇关于asp:Gridview中的超链接在新的浏览器窗口中打开PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆