Solved

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

Posted on 2012-04-11
3
160 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
Comment Utility
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
Comment Utility
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
Comment Utility
ok
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

728 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now