Gridview - Open file

One of the columns of have on my Gridview is called " Filename". This columns display filenames.  How can I turn them into links where when a user click it and it opens a file? I store these file on my local drive. Thank you in advance.
VBdotnet2005Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Pratima PharandeCommented:
asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%#  DataBinder.Eval(Container.DataItem,"FILENAME") %>' Text="Go to event"></asp:HyperLink>


http://www.experts-exchange.com/Programming/Languages/.NET/Q_21924099.html
0
RonaldZaalCommented:
Here is some code, in the datagrid is a template colum with layout et which I didn't want to delete
I used this recently so it's a bit copied and pasted.
- The Datagrid to show files with creation date and clickable files as on your aspx page
- The filling of the datagrid with half coded how to sort
- The action in the datagrid when a user clicks on a file.

If you need more sorting code let me know.



				<asp:datagrid id="dgr_public" style="Z-INDEX: 109; LEFT: 1px; POSITION: absolute; TOP: 3px"
				runat="server" Width="643px" BorderColor="Gray" BorderStyle="None" BorderWidth="1px" BackColor="White" CellPadding="3" AutoGenerateColumns="False" CssClass="dg" >
				<FooterStyle ForeColor="#000066" BackColor="White"></FooterStyle>
                    <EditItemStyle CssClass="dge" Font-Names="Arial" Font-Size="Smaller" />
                    <SelectedItemStyle BackColor="#669999" CssClass="dgs" Font-Bold="True" ForeColor="White" />
                    <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" Mode="NumericPages" />
                    <AlternatingItemStyle CssClass="dga" />
                    <ItemStyle CssClass="dgi" ForeColor="#000066" />
                    <HeaderStyle BackColor="#006699" CssClass="dgh" Font-Bold="True" Font-Names="arial"
                        Font-Size="Smaller" ForeColor="White" />
                    <Columns>
                        <asp:TemplateColumn HeaderText="File Name">
                           <ItemTemplate>
                                   <asp:LinkButton runat="server" Text='<%# Container.DataItem.name %>' CommandName="File" CausesValidation="false" ID="File" > </asp:LinkButton>
                              </ItemTemplate>
                        </asp:TemplateColumn>
                        <asp:BoundColumn DataField="LastWriteTime" HeaderText="Created on" DataFormatString="{0:d}" >
                            <ItemStyle HorizontalAlign="Center" />
                        </asp:BoundColumn>
                    </Columns>
                </asp:DataGrid>
 
 
' Here is the sub to populate datagrid
' You have to import Imports System.IO in your page !
Public Sub GetFiles
' Show some mouse action when user hovers over it
            dgr_public.Attributes.Add("onmouseover", "window.status='Click Here'; return true;")
            dgr_public.Attributes.Add("onmouseout", "window.status=''; return true;")
 
' I sort my files
' Dim compareMethod As CompareByOptions = CompareByOptions.LastWriteTime
' if needed search or ask in comments
 
' I take my path from the web.config
 Dim part_reports_path As String = System.Configuration.ConfigurationManager.AppSettings("part_reports_path")
 For testing you can use : example : reports path = c:\data\projects\grid\files
 
Dim dirInfoPub As New DirectoryInfo(part_reports_path)
            If dirInfoPub.Exists Then
                Dim fileInfoArray() As FileInfo = dirInfoPub.GetFiles("*" & "*.xls")
' This way above I get only excel files 
 
' If you want the files sorted you need to apply a method for this
' Nice but not needed
' I you don't want it remove this next line, keep source and bind
                Array.Sort(fileInfoArray, New CompareFileInfoEntries(compareMethod))
                dgr_public.DataSource = fileInfoArray
                dgr_public.DataBind()
            Else
                'error in filling your Datagrid  
            End If
 
' Now the action when the user clicks on a file : 
Public Sub Public_Click(ByVal sender As System.Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles dgr_public.ItemCommand
            Dim filePath As String = System.Configuration.ConfigurationManager.AppSettings("part_reports_path")& "\"
            Dim filename As String = UCase(CType(e.Item.FindControl("File"), LinkButton).Text)
 
            If e.CommandName = "File" Then
                Try
                    Dim s As Stream = File.OpenRead(filePath & filename)
                    Dim buffer(s.Length) As Byte
 
                    Try
                        s.Read(buffer, 0, CType(s.Length, Int32))
                    Finally
                        s.Close()
                    End Try
 
                    Response.ClearHeaders()
                    Response.ClearContent()
                    Response.ContentType = "application/octet-stream"
                    Response.AddHeader("Content-Disposition", "attachment; filename=" & Replace(CType(e.Item.FindControl("File"), LinkButton).Text, " ", "_"))
                    Response.BinaryWrite(buffer)
                    '   Response.End()
 
                Catch ex As Exception
                    ' you have another error
                End Try
 
            End If
          End Sub

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.