0

以前我可以从 gridview 下载文件和 GridView1_RowCommand 中的代码

代码是

 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();

但现在我创建了一个表格并显示了一些数据,我想要一个表格中的下载链接,这里是表格代码......

<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>

现在如何在表中添加下载链接以及我写的内容而不是 GridView1_RowCommand ????

任何解决方案?

4

1 回答 1

1

我了解,您将文件存储在某个名为“Docfiles”的文件夹中。您只需在此文件的 ItemTemplate 中添加超链接

<td><a href="/Docfiles/<%#DataBinder.Eval(Container.DataItem, "DocumentName")%>">Download</a></td>

在这种情况下不需要 GridView1_RowCommand

于 2013-10-27T19:42:00.443 回答