Solved

How to set the following datagrid content as link so that it opens file.

Posted on 2012-04-11
3
163 Views
Last Modified: 2012-04-12
How to set the file name as link so that when user clicks it, it gets open.

var lstFiles = Directory.GetFiles(MapPath("Files/"), string.Format("{0}_*.pdf".ToUpper(), TextBox1.Text), SearchOption.AllDirectories).Select(f => new { FileName = f.Substring(f.LastIndexOf('\\') + 1)}).ToList();

GridView1.DataSource = lstFiles;
        GridView1.DataBind();
0
Comment
Question by:searchsanjaysharma
3 Comments
 
LVL 6

Accepted Solution

by:
kswathi earned 500 total points
ID: 37831853
In your gridview add an Template column you can use the anchor/hyperlink
for anchor set the href your path & for hyperlink set NavigateUrl Property
Or just an Hyperlink field.
 <asp:GridView ID="GridView1" runat ="server" CellPadding ="3" CellSpacing ="3" >
  <Columns >
    <asp:HyperLinkField Text ='<%#Eval("YourFileName") %>' HeaderText ="File" NavigateUrl ='<%#Eval("YourFilePath") %>' />
</Columns>
</asp:GridView>

Open in new window

or
 <asp:GridView ID="GridView1" runat ="server" CellPadding ="3" CellSpacing ="3" >
  <Columns >
      <asp:TemplateField HeaderText ="File">
    <ItemTemplate >

    <asp:HyperLink ID ="hyFile" runat="server" Text ='<%#Eval("YourFileName") %>' NavigateUrl ='<%#Eval("YourFilePath") %>' ></asp:HyperLink>
    </ItemTemplate>
</Columns>
</asp:GridView>

Open in new window

or
 
  <asp:TemplateField HeaderText ="File">
    <ItemTemplate >
    <a href ='<%#Eval("YourFilePath") %>' ><%#Eval("YourFileName") %></a>
    
    </ItemTemplate>
    </asp:TemplateField>

Open in new window


Hope it helps.
0
 
LVL 20

Expert Comment

by:BuggyCoder
ID: 37831894
Here is the complete code:-

Markup:-

<asp:GridView runat="server" ID="grdTest" AutoGenerateColumns="False">
                <Columns>
                    <asp:TemplateField HeaderText="File">
                        <ItemTemplate>
                            <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%#Eval("FilePath") %>">' Text='<%#Eval("FileName") %>'></asp:HyperLink>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView> 

Open in new window


Here is the code behind:-

var lstFiles = Directory.GetFiles(MapPath("Files/"), string.Format("{0}_*.pdf".ToUpper(), TextBox1.Text), SearchOption.AllDirectories).Select(f => new { FileName = f.Substring(f.LastIndexOf('\\') + 1), FilePath=f}).ToList();

grdTest.DataSource = lstFiles;
grdTest.DataBind();

Open in new window

0
 

Author Closing Comment

by:searchsanjaysharma
ID: 37841199
ok
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Hey!! 5 37
FTP file download using c# 3 25
RegEx with optional part 4 42
I need help adding validation to my MVC.Net view 6 10
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

827 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question