• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 784
  • Last Modified:

Download buttons on gridview problem

Hi, I'm having trouble getting my Download column on my gridview to work.  I have an upload function in my program that saves all the files to the server, along with inserting the filepath, filename, and other things into the database.  On my user page (see the attached screenshot), I've got a gridview with columns with a download button.  One column is the filename.  I figure this should be straight forward, setting the download up to download the name of the file in the same row, but I just can't figure out programmatically what I would do to get the value of each filename in the row and then do some sort of Response.Transferfile(file_name_in_the_row).  Hopefully I'm clear, please feel free to ask if I left something out.  
<asp:HiddenField ID="userhiddenfield" runat="server" />
         <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
        DataSourceID="SqlDataSource1" CellPadding="4" ForeColor="#333333" 
        GridLines="None" style="margin-right: 0px" Width = "1000px">
        <RowStyle BackColor="#EFF3FB" />
        <Columns>
            <asp:ButtonField ButtonType="Button" Text="Download" />
            <asp:BoundField DataField="fileid" HeaderText="ID" 
                SortExpression="fileid" />
            <asp:BoundField DataField="title" HeaderText="Title" SortExpression="title" />
            <asp:BoundField DataField="subtitle" HeaderText="Subtitle" 
                SortExpression="subtitle" />
            <asp:BoundField DataField="nameoffile" HeaderText="File Name" 
                SortExpression="nameoffile" />
            <asp:BoundField DataField="date" HeaderText="Date" SortExpression="Date" />
            <asp:BoundField DataField="category" HeaderText="Category" 
                SortExpression="category" />
            <asp:BoundField DataField="FileUser" HeaderText="User" 
                SortExpression="FileUser" />
        </Columns>
        <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
        <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
        <HeaderStyle BackColor="#507CD1" BorderColor="#0033CC" Font-Bold="True" 
            ForeColor="White" />
        <EditRowStyle BackColor="#2461BF" />
        <AlternatingRowStyle BackColor="White" />
    </asp:GridView>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:connstr %>" 
        SelectCommand="SELECT * FROM [UserofFile] WHERE ([FileUser] = @FileUser)" 
            CacheExpirationPolicy="Sliding">
        <SelectParameters>
            <asp:ControlParameter ControlID="userhiddenfield" Name="FileUser" 
                PropertyName="Value" Type="String" />
        </SelectParameters>
    </asp:SqlDataSource>

Open in new window

screenshot.jpg
0
JeffEae
Asked:
JeffEae
  • 5
  • 2
  • 2
  • +1
1 Solution
 
nmarunCommented:
Why can't you just add a hyperlink to this download column and provide the file path there?

So instead of:
<asp:ButtonField ButtonType="Button" Text="Download" />

you'll have:

<asp:Hyperlink ID="FileDownloadLink" runat="server" NavigateUrl="provide the file path here" Text="Download" />
0
 
JeffEaeAuthor Commented:
Alright, so if I uploaded the documents using Server.MapPath(".\\"), what would the navigateUrl be? And then I still need to get the filename, which is in its own column in the row of the download link.  
0
 
nmarunCommented:
The NavigateUrl will be a relative path on the server (or you could hardcode the path from a different server). Try something like this:

<asp:HyperLink ID="downloadLink" runat="server" NavigateUrl='<%# GetFilePath(DataBinder.Eval(Container, "DataItem.Download").ToString())%>' />
 
and in your code-behind file:
 
public string GetFilePath(string Download)
{
    string path = Server.MapPath("//");
    string filePath = string.Format("{0}//{1}", path, Download);
    return filePath;
}

Open in new window

0
Restore individual SQL databases with ease

Veeam Explorer for Microsoft SQL Server delivers an easy-to-use, wizard-driven interface for restoring your databases from a backup. No expert SQL background required. Web interface provides a complete view of all available SQL databases to simplify the recovery of lost database

 
JeffEaeAuthor Commented:
I tried to insert that Hyperlink instead of the Button and got

Error      1      System.Web.UI.WebControls.DataControlFieldCollection must have items of type 'System.Web.UI.WebControls.DataControlField'. 'asp:HyperLink' is of type 'System.Web.UI.WebControls.HyperLink'.      C:\Documents and Settings\Datalis\My Documents\Visual Studio 2008\WebSites\USCorrugatedtool\User.aspx      21      

And where am I returning the file name from the column named 'filename' on the corresponding row?
0
 
bedanandCommented:
You can use template of gridview. 

Instead of this
<
asp:ButtonField
 
ButtonType
=
"Button"
 
Text
=
"Download"
 
/><asp:ButtonField ButtonType="Button" Text="Download" />

Use as below.

Open in new window


<asp:TemplateField>
                    <ItemTemplate>
                        <asp:HyperLink runat="Server" ID="hlDownload" NavigateUrl='<%# Eval("RelativeFilePath") %>'></asp:HyperLink>
                    </ItemTemplate>
                </asp:TemplateField>

Open in new window

0
 
madhevan_pillaiCommented:
Hi,

Try This

aspx

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="DownLoad.aspx.vb" Inherits="FileDownload_DownLoad" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
   
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
            <Columns>
                <asp:BoundField DataField="FileName" HeaderText="FileName" ReadOnly="True" SortExpression="FileName" />
             
               
                <asp:TemplateField HeaderText="Download" >
                <ItemTemplate>
                  <asp:HyperLink       runat="server" ID="aaa"      />  
                </ItemTemplate>
             
                </asp:TemplateField>
                 
            </Columns>
        </asp:GridView>
         
    </form>
</body>
</html>


codebehind

Imports System.Data
Partial Class FileDownload_DownLoad
    Inherits System.Web.UI.Page
    Private Function GetData() As DataTable
        Dim dt As New DataTable
        dt.Columns.Add("FileName")
        dt.Columns.Add("Path")
        dt.Rows.Add(New Object() {"TextFile.txt", "TextFile.txt"})
        dt.Rows.Add(New Object() {"TextFile1.txt", "TextFile1.txt"})
        Return dt
    End Function

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Me.IsPostBack Then
            Me.GridView1.DataSource = GetData()
            Me.GridView1.DataBind()
        End If
    End Sub

 
   

    Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
        Dim ctrl As HyperLink
        If e.Row.RowType = DataControlRowType.DataRow Then
            ctrl = CType(e.Row.Controls(1).Controls(1), HyperLink)
            ctrl.NavigateUrl = e.Row.Cells(0).Text
            ctrl.Text = e.Row.Cells(0).Text

        End If
    End Sub
End Class
0
 
JeffEaeAuthor Commented:
The problem of which template to use is fine, I'll go with what bedanand:said and use a link instead of a button.  Taking a look at my screenshot, the column 'filename' is where I'm stuck.  I know there has to be a way to get the hyperlink to actually read that filename in the row that it is in and then download.  
0
 
JeffEaeAuthor Commented:
Anybody? This project is almost done, this is my last piece! =P
0
 
bedanandCommented:
If you have already saved the relative filepath on the database table on single columns just put the column name

<asp:TemplateField>
                    <ItemTemplate>
                        <asp:HyperLink runat="Server" ID="hlDownload" NavigateUrl='<%# Eval("FilePath") %>'></asp:HyperLink>
                    </ItemTemplate>
                </asp:TemplateField>

If you have put on two or more columns you can concate the fields

like this


<asp:TemplateField>
                    <ItemTemplate>
                        <asp:HyperLink runat="Server" ID="hlDownload" NavigateUrl='<%# Eval("FilePath") + Eval("FileName") %>'></asp:HyperLink>
                    </ItemTemplate>
                </asp:TemplateField>

Open in new window

0
 
JeffEaeAuthor Commented:
The last person had the right answer, after I opened the delete request
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 5
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now