0

I need to open a some types of files like .jpg, word, .pdf when the user clicks on link in gridview. Right now i am using this code and its not opening up.

It is a web application and i have to open the file which is present in the local drive of user. I would be binding the path of file in NavigateUrl property of the hyperlink

<asp:hyperlink ID="HyplnkName" runat="server" NavigateUrl= '<%# ConfigurationManager.AppSettings["ImagesFilePath"]) %>' Target="_top" Text='<%# DataBinder.Eval(Container, "DataItem.FileName") %>' />
4

2 回答 2

0

这是我在项目中使用的

<asp:HyperLink ID="hlPdf" runat="server" NavigateUrl="~/PdfHandler.ashx" Target="_blank">Click to view PDF</asp:HyperLink>

这是 ashx 处理程序文件

using System;
using System.Web;
using System.IO;

public class PdfHandler : IHttpHandler 
{

  public void ProcessRequest (HttpContext context) 
  {
    byte[] data = File.ReadAllBytes(@"Your Path");
    context.Response.ContentType = "application/pdf";
    context.Response.OutputStream.Write(data, 0, data.Length);
  }

  public bool IsReusable {
    get {
        return false;
    }
  }
}
于 2012-04-09T08:11:08.193 回答
0

改用 OnRowDataBound :

页面:

<asp:GridView ID="GridView1" runat="server" 
    onrowdatabound="GridView1_RowDataBound">
<Columns>
<asp:TemplateField>
<ItemTemplate>
    <asp:HyperLink ID="HL" runat="server" Target ="_blank"></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

后面的代码:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{

    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        HyperLink hlink = (HyperLink)e.Row.FindControl("HL");
        string url = "~/Docs/" + e.Row.Cells[1].Text;
        hlink.NavigateUrl = url;
        hlink.Text = "Read";
    }

}
于 2012-04-09T08:15:31.623 回答