Solved

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

Posted on 2012-04-11
3
162 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Angular - break the dataset in chunks? 7 51
C# 2015 Downloading text file and preserving the line breaks 5 38
Reading the Web.Config using IIS 7.5? 4 39
Graphics 2 30
Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

831 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