I want to show my pdf file in new tab when I click the button of gridview. How can I display? Please someone help me .
This is my code.
'my gridview button click event
Protected Sub btnDisplay_Click(sender As Object, e As EventArgs)
Dim grdrow As GridViewRow = CType((CType(sender, Button)).NamingContainer, GridViewRow)
'I save pdf with datetime but showing file name without datetime on screen so I need to
'combine again when I need to open the file from upload folder
Dim dtime As DateTime = grdrow.Cells(2).Text
Dim fname As String = lblFileName.Text.Split(".").First + "_" +
dtime.ToString("yyyyMMddHHmmss") + ".pdf"
Dim FilePath As String = Server.MapPath("~/uploads/" & fname)
Dim User As WebClient = New WebClient()
Dim FileBuffer As Byte() = User.DownloadData(FilePath)
If FileBuffer IsNot Nothing Then
Response.ContentType = "application/pdf"
Response.AddHeader("content-length", FileBuffer.Length.ToString())
Response.BinaryWrite(FileBuffer)
End If
End Sub
–Edit–
I got some idea and it did work for me.
I added some script to open new tab.
html,gridview
//javascript
<script type="text/javascript">
function openInNewTab() {
window.document.forms[0].target = '_blank';
setTimeout(function () { window.document.forms[0].target = ''; }, 0);
}
</script>
<asp:BoundField DataField="FileName" HeaderText="Filename" ItemStyle-Width="200" HtmlEncode="false"><ItemStyle Width="200px"></ItemStyle></asp:BoundField>
<asp:BoundField DataField="Process" HeaderText="Process" ItemStyle-Width="200" HtmlEncode="false"><ItemStyle Width="200px"></ItemStyle></asp:BoundField>
<asp:TemplateField ItemStyle-Width="80px" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:Button ID="btnDisplay" runat="server" Text="Display" OnClick="btnDisplay_Click" OnClientClick="openInNewTab();" Visible='<%# If(Eval("Process").ToString() = "Uploaded", True, False) %>'></asp:Button>
</ItemTemplate>
</asp:TemplateField>
Main.aspx
Protected Sub btnDisplay_Click(sender As Object, e As EventArgs)
Dim grdrow As GridViewRow = CType((CType(sender, Button)).NamingContainer, GridViewRow)
Dim fname As String = grdrow.Cells(2).Text
'pdf Display
Session("pdfname") = fname
Response.Redirect("GeneratePDF.aspx")
End Sub
GeneratePDF.aspx
<form id="form1" runat="server">
<div style ="Display: Inline-block;float: left;">
<asp:Literal ID="ltEmbed" runat="server" />
</div>
</form>
GeneratePDF.aspx.vb
Dim pdf_name As String
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Page.Title = "PDF DISPLAY"
pdf_name = Session("pdfname")
Dim embed As String = "<object data=""{0}"" type=""application/pdf"" width=""2000px"" height=""1000px"">"
embed += "If you are unable to view file, you can download from <a href = ""{0}"">here</a>"
embed += " or download <a target = ""_blank"" href = ""http://get.adobe.com/reader/"">Adobe PDF Reader</a> to view the file."
embed += "</object>"
ltEmbed.Text = String.Format(embed, ResolveUrl("~/uploads/" + pdf_name))
End Sub
2
Answers
i think you are missing something, add Response.End() at the end of the if statement
if it doesnt work, there must be an issue on the Html
First use hyperlink instead of Button and make target =_blank. Then on Page Load of new aspx write code for generating PDF.