i am trying to open PDF file in new Tab in browser, but its open same Tab.. iam using gridview Template field to open pdf..
How To Open PDF Files In New Tab In Browser Using GridView Row Command In ASP.NET C#
ASP.NET
<asp:GridView ID="gvwPDF" runat="server" CssClass="mGrid" CellPadding="20" CellSpacing="20" AutoGenerateColumns="false" EmptyDataText="No files uploaded" Width="100%">
<Columns>
<asp:BoundField DataField="Text" HeaderText="File Name" />
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lnkRead" runat="server" Text="✉ Read" CommandName="Read" CssClass="gvwedit" ForeColor="Green" OnClick="ReadPDFFile" CommandArgument='<%# Eval("Value") %>'></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
C#
protected void Page_Load(object sender, EventArgs e)
{
try
{
if (!IsPostBack)
{
string[] filePaths = Directory.GetFiles(Server.MapPath("~/Uploads/"));
List<ListItem> files = new List<ListItem>();
foreach (string filePath in filePaths)
{
files.Add(new ListItem(Path.GetFileName(filePath), filePath));
}
gvwPDF.DataSource = files;
gvwPDF.DataBind();
}
}
catch (Exception ex)
{
//PopMsg.Pop(ex.Message.ToString(), BmGate.WebFormUserControls.Common.MessageBox.IconError, "Error");
ScriptManager.RegisterStartupScript(this, GetType(), "Error Message", "alert('" + ex.Message.ToString() + "')", true);
}
}
protected void ReadPDFFile(object sender, EventArgs e)
{
try
{
string path = (sender as LinkButton).CommandArgument;
WebClient client = new WebClient();
Byte[] buffer = client.DownloadData(path);
if (buffer != null)
{
Response.ContentType = "application/pdf";
Response.AddHeader("content-length", buffer.Length.ToString());
Response.BinaryWrite(buffer);
ClientScript.RegisterClientScriptBlock(this.GetType(), "Message", "window.open('application/pdf','_blank');", true);
}
}
catch (Exception ex)
{
ScriptManager.RegisterStartupScript(this, GetType(), "Error Message", "alert('" + ex.Message.ToString() + "')", true);
}
}
help to solve this issue..