Solved

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

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

696 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