ASP .NET从数据绑定网格链接到pdf

时间:2016-03-01 19:55:40

标签: asp.net pdf hyperlink

我的数据字段字段显示日期,类型,分数和等级。我已使用以下代码添加了“查看PDF”列。

enter code here

<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="LinkButton4" runat="server" Text="View PDF"    
onclientclick='redirect()'> 
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>

New Code 3-3-2016
<asp:GridView id="gvInspection" runat="server"    
DataSourceID="dsInspectionHistory"
AutoGenerateColumns="False" AllowPaging="True"
Width="100%" Height="1px"
GridLines="None" PageSize="25"
BorderColor="Black" BorderStyle="Solid" BorderWidth="1px">
<HeaderStyle BackColor="#DFE3C7" ForeColor="Black"/>
<Columns>

<asp:BoundField DataField="Inspection_Date" HeaderText="Date" 
SortExpression="Inspection_Date" DataFormatString="{0:MMM d yyyy}" 
ReadOnly="True">
<ItemStyle HorizontalAlign="Left"/>
<HeaderStyle HorizontalAlign="Left"/>
</asp:BoundField>

<asp:BoundField DataField="ServiceType" HeaderText="Inspection Type" 
SortExpression="ServiceType" ReadOnly="True">
<ItemStyle HorizontalAlign="Center" Width="192px"/>
<HeaderStyle HorizontalAlign="Center" Width="192px"/>
</asp:BoundField>

<asp:BoundField DataField="Score" HeaderText="Score" SortExpression="Score" 
ReadOnly="True">
<ItemStyle HorizontalAlign="Center"/>
<HeaderStyle HorizontalAlign="Center"/>
</asp:BoundField>

<asp:BoundField DataField="Grade" HeaderText="Grade" SortExpression="Grade" 
ReadOnly="True">
<ItemStyle HorizontalAlign="Center" Width="96px"/>
<HeaderStyle HorizontalAlign="Center" Width="96px"/>
</asp:BoundField>

<asp:TemplateField HeaderText="Report(s)">
<ItemTemplate>
<asp:LinkButton ID="LinkButton4" runat="server" Text="View PDF"    
CommandArgument='<%#Bind("Serial_Number") %>' OnClick="LinkButton_Click">
</asp:LinkButton>
</ItemTemplate>
<ItemStyle horizontalalign="Center" width="64px"/>
<HeaderStyle horizontalalign="Center" width="64px"/>
</asp:TemplateField>

</Columns>

<EmptyDataTemplate>
    No Inspections To Date
</EmptyDataTemplate>
</asp:GridView></td></tr>

我的目标是首先只显示查看PDF链接,如果找到的PDF与记录集中的序列号匹配。然后,如果单击“查看PDF”,则打开关联的PDF。请假设PDF存储在本地。

我感谢任何帮助。提前谢谢。

2 个答案:

答案 0 :(得分:0)

您可以将序列号作为click事件上链接按钮的命令参数传递。这样的事情可能是:

<asp:TemplateField>
    <ItemTemplate>
        <asp:LinkButton ID="LinkButton4" runat="server" Text="View PDF" CommandArgument='<%#Bind("SerialNumber") %>' OnClick="LinkButton_Click"></asp:LinkButton>
    </ItemTemplate>
</asp:TemplateField>

然后在代码背后

protected void LinkButton_Click(object sender, EventArgs e)
{
    var linkButton = (LinkButton)sender;
    var serial_number = linkButton.CommandArgument;
    var pdf_path = Server.MapPath(string.Format("~/PDF/FILE/LOCATION/{0}.pdf", serial_number));

    if (System.IO.File.Exists(pdf_path))
    {
        HttpResponse response = HttpContext.Current.Response;
        response.ContentType = "application/pdf";
        response.TransmitFile(pdf_path);
    }
}

VB版

Protected Sub LinkButton_Click(sender As Object, e As EventArgs)
    Dim linkButton = DirectCast(sender, LinkButton)
    Dim serial_number = linkButton.CommandArgument
    Dim pdf_path = Server.MapPath(String.Format("~/PDF/FILE/LOCATION/{0}.pdf", serial_number))

    If System.IO.File.Exists(pdf_path) Then
        Dim response As HttpResponse = HttpContext.Current.Response
        response.ContentType = "application/pdf"
        response.AppendHeader("Content-Disposition", "attachment; filename=testing.pdf")
        response.TransmitFile(pdf_path)
        response.End()
    End If
End Sub

答案 1 :(得分:0)

无法执行linkbutton事件,因为javascript首先运行。有关更多信息,请参阅注释。更改为存在于网格外但仍使用网格数据的超链接事件。