Click here to Skip to main content
15,904,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
previosuly i can download files from gridview and code in GridView1_RowCommand

code is
C#
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "cmd")
        {
            string filename = e.CommandArgument.ToString();
            string path = MapPath("~/Docfiles/" + filename);
            byte[] bts = System.IO.File.ReadAllBytes(path);
            Response.Clear();
            Response.ClearHeaders();
            Response.AddHeader("Content-Type", "Application/octet-stream");
            Response.AddHeader("Content-Length", bts.Length.ToString());
            Response.AddHeader("Content-Disposition", "attachment; filename=" +
            filename);
            Response.BinaryWrite(bts);
            Response.Flush();


but now i create a table and show some data and i want a download link in table here is table code...

ASP.NET
<div class="CSSTableGenerator" >
            <table border="0" width="100%" cellpadding="0" cellspacing="0" 
              id="results">
            <asp:Repeater ID="Repeater1" runat="server">
            <HeaderTemplate>
                <tr>
                    <td>
                       Document ID
                    </td>
                    <td >
                        Document Name
                    </td>
                    <td>
                        File Uploaded
                    </td>
                    <td>
                       Document Type
                    </td>

                </tr>
                </HeaderTemplate>
                <ItemTemplate>
                <tr>
                <%--<td><asp:LinkButton ID="LinkButton1" runat="server"
                CommandArgument='<%# Eval("FileUploaded") %>' 
           CommandName="cmd">Download</asp:LinkButton></td>--%>
                <td><%#DataBinder.Eval(Container.DataItem,"DocumentID") %></td>

                <td><%#DataBinder.Eval(Container.DataItem, "
                     DocumentName")%> </td>
                <td><%#DataBinder.Eval(Container.DataItem,
                                "FileUploaded")%></td>
                <td><%#DataBinder.Eval(Container.DataItem, 
                      "Document")%></td>
                </tr>
                </ItemTemplate>
                </asp:Repeater> 
                <div id="pageNavPosition" >
                </div>              
            </table>
            </div>


now how i add download link in table and what i write instead of GridView1_RowCommand ????

any solution?
Posted
Updated 28-Oct-13 1:53am
v2
Comments
VICK 28-Oct-13 0:39am    
I think you want linkbutton inside the repeater and not table. ???
Diya Ayesa 28-Oct-13 7:33am    
yes
VICK 28-Oct-13 7:52am    
See the Solution Posted by Me.

XML
(1) add itemcommand event to repeater
-----------------------------------------------------
<asp:Repeater ID="Repeater1" OnItemCommand="Repeater1_ItemCommand" runat="server">

(2) use following template in repeate control
----------------------------------------------
<ItemTemplate>
<td>
    <asp:LinkButton ID="LinkButton1" runat="server" CommandArgument='<%# Eval("FileUploaded") %>'
           CommandName="download">Download</asp:LinkButton>
</td>
</ItemTemplate>

(3) use this event for repeater control in codebehind file
--------------------------------------------------------------
protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
{
    if (e.CommandName == "download")
    {
        string filename = e.CommandArgument.ToString();
        string path = MapPath("~/Docfiles/" + filename);
        byte[] bts = System.IO.File.ReadAllBytes(path);
        Response.Clear();
        Response.ClearHeaders();
        Response.AddHeader("Content-Type", "Application/octet-stream");
        Response.AddHeader("Content-Length", bts.Length.ToString());
        Response.AddHeader("Content-Disposition", "attachment; filename=" +
        filename);
        Response.BinaryWrite(bts);
        Response.Flush();
    }
}
 
Share this answer
 
Comments
Diya Ayesa 28-Oct-13 8:41am    
when i done this it shows me error in this line <asp:Repeater ID="Repeater1" OnItemCommand="Repeater1_ItemCommand" runat="server">


ERROR:
Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: CS0123: No overload for 'Repeater1_ItemCommand' matches delegate 'System.Web.UI.WebControls.RepeaterCommandEventHandler'

Source Error:


Line 97: <div class="CSSTableGenerator" >
Line 98: <table border="0" width="100%" cellpadding="0" cellspacing="0" id="results">
Line 99: <asp:Repeater ID="Repeater1" OnItemCommand="Repeater1_ItemCommand" runat="server">
Line 100: <HeaderTemplate>
Line 101: <tr>
samit kaneriya 28-Oct-13 9:03am    
please check repeater event name both side same and Argument type is => RepeaterCommandEventArgs
Diya Ayesa 28-Oct-13 9:23am    
THANKS A LOT =)
Member 13164715 1-May-17 14:30pm    
When I use this, it seems to read the filename as System.Byte[]. Might there be a reason why string filename = e.CommandArgument.ToString(); isn't reading the filename properly?
You can add up the link button in Repeater's ItemTemplate and can get it in Repeater_ItemCommand event.


for detailed solution .Have a look at following URL.

LinkButton inside Repeater Control.

Hope it will help. :)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900