2015-07-21 30 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

试过这个,但它不起作用 –