2012-04-09 19 views
0

ユーザーがgridviewのリンクをクリックすると、.jpg、word、.pdf などのファイルを開く必要があります。今すぐ私は このコードを使用していて、それは開いていません。gridviewのリンクを使用してWebページからローカルファイルを開くC#

これはWebアプリケーションで、私は現在のファイル をユーザーのローカルドライブに開く必要があります。私はここで

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

私はuがファイルを処理する.ashxのようなハンドラファイルを使用し、それがある理由@Prabhavith彼らに – Prabhavith

+0

を表示するために持っていると思いますか?私はちょうどローカルドライブにあるファイルを開く必要があります。ファイルはWebサーバーにありません – Anuya

答えて

0

ハイパーリンクの NavigateUrlプロパティにファイルのパスを結合されるだろう、私は

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

私のプロジェクトで使用されるものですこれは

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; 
    } 
    } 
} 
ASHXハンドラファイルです
0

代わりにOnRowDataBoundを使用:

aspxページ:背後

<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"; 
    } 

} 
+0

セルのコントロールを別のセルに移動できます。だから、 'FindControl'アプローチはここで良いはずです。 – Pankaj

関連する問題