Hardworker
Hardworker

Reputation: 229

GridView Hyperlink opening file

Can anyone help me on how to open a file from gridview using either Hyperlink or LinkButton controls? In my application i want to open files from a path specified inside my application (as an example like "c://example/") and show all the file names in the gridview as a hyperlink till this much it is working properly, but when i click on the file name which is as a hyperlink nothing happens, i set the navigateurl as:

<asp:HyperLink ID="HyperLink1" runat="server" **Text='<%# Eval("Name") %>' 
  NavigateUrl='<%# bind("FullName") %>'**></asp:HyperLink>

plz help me out

Upvotes: 0

Views: 10505

Answers (5)

Aman Sharma
Aman Sharma

Reputation: 21

It can be done on Row command event of Gridview.

protected void grdAttachment_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "ViewFile")
    {
        string fileName = Server.MapPath("~/Attachment/" + e.CommandArgument.ToString());           
        Process process = new Process();
        process.StartInfo.UseShellExecute = true;
        process.StartInfo.FileName = fileName;
        process.Start();
    }
}

In fileName give your file Path. When You click on Link Button this file will be open.

You can Follow this link for full example:

How to Open or view Images & Docx Files in Gridview in Asp.Net (C#)?

Upvotes: 0

cal5barton
cal5barton

Reputation: 1616

I like to do it this way to make the link text dynamic.

<ItemTemplate>                                   
              <asp:HyperLink ID="HyperLink1" runat="server" Target="_blank" Text='<%# Bind("DataField") %>' NavigateUrl='<%# DataBinder.Eval(Container, "DataItem.DataField", "~/Folder/{0}") %>'></asp:HyperLink>
</ItemTemplate>

Upvotes: 0

Thaabiet
Thaabiet

Reputation: 21

In an <asp:TemplateField>, I've added:

<a id="A1" runat="server" target="_blank" href='<%# DataBinder.Eval(Container, "DataItem.ProofOfPayment", "~/uploads/payments/{0}") %>'>View Payment</a>

Upvotes: 2

Massimiliano Peluso
Massimiliano Peluso

Reputation: 26727

you should specify the "file" suffix. The Url should look like the below:

// “file://\\Server\Folder\FileName.ext“


<asp:HyperLink ID="HyperLink1" runat="server" **Text='<%# Eval("Name") %>' 
 NavigateUrl='file://<%# bind("FullName") %>'**></asp:HyperLink>

you could have issue anyway if the path containt any withspaces as reported here

The best way would be to use a HTML link

<a runat="server" target="_blank"  href='<%# DataBinder.Eval(Container, "DataItem.FilePath") %>'>

Upvotes: 0

Paddy
Paddy

Reputation: 33857

What is 'FullName' - if it is something like C:/Somefile, then this is not going to work as it points to a location on the hard drive of your server, not a URL. You'll either need a virtual directory pointing at the location of your files, or some kind of a page to read and serve these items.

Upvotes: 0

Related Questions