2015-07-21 29 views
0

這裏有能夠查看pdf文件的代碼,並且當前文件可以在同一頁面中查看。我希望在gridview中點擊linkbutton時在新頁面中顯示文件。據瞭解,文字嵌入鏈接需要改變,但我不確定。我的問題是如何在新頁面中查看文件?在另一個頁面中查看pdf文件

的GridView代碼:背後

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Width="100%" CellPadding="4" ForeColor="#333333" GridLines="None"> 
     <AlternatingRowStyle BackColor="White" /> 
     <Columns> 
      <asp:BoundField DataField="comName" HeaderText="Company Name" SortExpression="comName" /> 
      <asp:BoundField DataField="sName" HeaderText="Stock Name" SortExpression="sName" /> 
      <asp:BoundField DataField="annDate" HeaderText="Date Announced" SortExpression="annDate" /> 
      <asp:BoundField DataField="fYearEnd" HeaderText="Financial Year End" SortExpression="fYearEnd" /> 
      <asp:BoundField DataField="quarterr" HeaderText="Quarter" SortExpression="quarterr" /> 
      <asp:BoundField DataField="QFREDate" HeaderText="Financial Period Ended " SortExpression="QFREDate" /> 
      <asp:BoundField DataField="figure" HeaderText="Figure" SortExpression="figure" /> 
      <asp:TemplateField ItemStyle-HorizontalAlign="Center"> 
     <ItemTemplate> 
      <asp:LinkButton ID="lnkView" runat="server" Text="View" OnClick="View" CommandArgument='<%# Eval("id") %>'></asp:LinkButton> 
     </ItemTemplate> 

代碼視圖:

protected void View(object sender, EventArgs e) 
    { 
     int id = int.Parse((sender as LinkButton).CommandArgument); 
     string embed = "<object data=\"{0}{1}\" type=\"application/pdf\" width=\"100%\" height=\"100%\">"; 
     embed += "If you are unable to view file, you can download from <a href = \"{0}{1}&download=1\">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("~/FileCS.ashx?Id="), id); 
    } 

FileCS.ashx代碼:

public void ProcessRequest(HttpContext context) 
{ 
    int id = int.Parse(context.Request.QueryString["Id"]); 
    byte[] bytes; 
    string fileName, contentType; 
    string constr = ConfigurationManager.ConnectionStrings["connection"].ConnectionString; 
    using (SqlConnection con = new SqlConnection(constr)) 
    { 
     using (SqlCommand cmd = new SqlCommand()) 
     { 
      cmd.CommandText = "SELECT fName, contentType, data FROM announ WHERE [email protected]"; 
      cmd.Parameters.AddWithValue("@Id", id); 
      cmd.Connection = con; 
      con.Open(); 
      using (SqlDataReader sdr = cmd.ExecuteReader()) 
      { 
       sdr.Read(); 
       bytes = (byte[])sdr["data"]; 
       contentType = sdr["contentType"].ToString(); 
       fileName = sdr["fName"].ToString(); 
      } 
      con.Close(); 
     } 
    } 

    context.Response.Buffer = true; 
    context.Response.Charset = ""; 
    if (context.Request.QueryString["download"] == "1") 
    { 
     context.Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName); 
    } 
    context.Response.Cache.SetCacheability(HttpCacheability.NoCache); 
    context.Response.ContentType = "application/pdf"; 
    context.Response.BinaryWrite(bytes); 
    context.Response.Flush(); 
    context.Response.End(); 
} 

回答

0
在你的鏈接按鈕

添加的OnClientClick =「aspnetForm.target = '_空白';」

<asp:LinkButton ID="lnkView" runat="server" Text="View" OnClick="View" OnClientClick="aspnetForm.target ='_blank';" CommandArgument='<%# Eval("id") %>'></asp:LinkButton> 

這將打開一個新選項卡。

+0

可以說新的頁面是Pdf.aspx。怎麼做? –

+0

試過這個,但它不起作用 –