title: How To Open PDF Files In New Tab In Browser Using GridView Row Command In ASP.NET C#
body: <p>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..</p>
  
  <p>How To Open PDF Files In New Tab In Browser Using GridView Row Command In ASP.NET C#</p>
  
  <p><a href="https://i.stack.imgur.com/gRze5.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/gRze5.jpg" alt="enter image description here"></a></p>
  
  <p><strong>ASP.NET</strong></p>
  
  <pre><code>&lt;asp:GridView ID="gvwPDF" runat="server" CssClass="mGrid" CellPadding="20" CellSpacing="20" AutoGenerateColumns="false" EmptyDataText="No files uploaded" Width="100%"&gt;
     &lt;Columns&gt;
       &lt;asp:BoundField DataField="Text" HeaderText="File Name" /&gt;
      &lt;asp:TemplateField&gt;
           &lt;ItemTemplate&gt;
             &lt;asp:LinkButton ID="lnkRead" runat="server" Text="✉ Read" CommandName="Read" CssClass="gvwedit" ForeColor="Green" OnClick="ReadPDFFile" CommandArgument='&lt;%# Eval("Value") %&gt;'&gt;&lt;/asp:LinkButton&gt;
             &lt;/ItemTemplate&gt;
           &lt;/asp:TemplateField&gt;
  &lt;/Columns&gt;
   &lt;/asp:GridView&gt;
  </code></pre>
  
  <p><strong>C#</strong></p>
  
  <pre><code>protected void Page_Load(object sender, EventArgs e)
      {
          try
          {
              if (!IsPostBack)
              {
                  string[] filePaths = Directory.GetFiles(Server.MapPath("~/Uploads/"));
                  List&lt;ListItem&gt; files = new List&lt;ListItem&gt;();
                  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);
          }
      }
  </code></pre>
  
  <p>help to solve this issue..</p>