Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2012-04-11
3
Medium Priority
?
167 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 6

Accepted Solution

by:
kswathi earned 2000 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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

598 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