-1

我正在创建一个网站,在 asp.net 中并使用 C# 我的问题是我创建了一个运行良好的 asp 中继器,所以我添加了一个图像按钮来下载中继器包含的文件,但我无法下载该文件,希望有人可以帮助我,我已经有了这个,我尝试了几种方法,但可以让它做我想做的事。这是我的代码。

ASP

<asp:Repeater ID="RepDetailsPost" runat="server" OnItemCommand="Save" >
<HeaderTemplate>
<table style="width:500px" align ="center" cellpadding="0" class="rounded_corners" >
<tr style="background-color:Red; color:White">
<td valign="top" bgcolor="CC0000" >
<b>Post</b>
</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr style="background-color:#333">
<td>
<table style="background-color:#222; width:500px" class="rounded_corners" >
<table>
<tr>
<td style="font-size:large">
Utilizador:
<asp:Label ID="lblSubject0" runat="server" Text='<%#Eval("nome_utilizador") %>' 
        Font-Size="small" Font-Bold="true"/>
</td>
</tr>
</table>
</td>
</tr>
<tr style="background-color:#333;Color:White">
<td align="right" >
Criado em:<asp:Label ID="lblDate0"   runat="server" Font-Bold="true" 
        Text='<%#Eval("data") %>'/>
</td>
</tr>
<tr>
<td>
<table style="background-color:#222; width:500px">
<tr> 
<td>Anexo:
    <asp:Image ImageUrl=<%# string.Format("~/uploads/{0}",Eval("Nome"))%> runat="server"  width=500px/>
</td>
</tr>
</table>
<table style="background-color:#222; width:500px">
<tr> 



<td>Guardar:
    <asp:ImageButton ID="save" ImageUrl=<%# string.Format("~/imagens/icones/save.png")%> runat="server"  width=30px CommandName="save"
   CommandArgument='<%# Eval("Nome") %>' />

        \\\\\\\\\\\\\\\                    this is what I tried   on top     /////////////////

   </tr>
</table>
<table style="background-color:#222; width:500px" align=center>
<tr>
<td>Comentário: <br /><asp:Label ID="lblComment0" Font-Size="small" runat="server" Text='<%#Eval("descricao") %>' /></td>
</tr>
</table>
</td>
</tr>
<tr>
<td  colspan="2"valign="top" bgcolor="CC0000">&nbsp;
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>  

C#

   protected void Save(Object Sender, RepeaterCommandEventArgs e)
{

    if (e.CommandName == "Save")
    {
        int index = Convert.ToInt32(e.CommandArgument);
        string fName = "teste" ;
        Response.ContentType = "application/octet-stream";
        Response.AddHeader("Content-Disposition", "attachment;filename=" + fName);
        Response.TransmitFile(Server.MapPath("~/uploadsadmin/" + fName));
        Response.End();
    }
}
4

1 回答 1

0

您的方法名称OnItemCommand = "Dwn"必须与代码隐藏中的方法名称匹配。将 Save 方法重命名为 Dwn,或更改标记以匹配 Save 方法名称。

像这样重构代码:

if(e.CommandName == "Save")
{
    int index = Convert.ToInt32(e.CommandArgument);
    string fName = row.Cells[2].Text;
    Response.ContentType = "application/octet-stream";
    Response.AddHeader("Content-Disposition", "attachment;filename=" + fName);
    Response.TransmitFile(Server.MapPath("~/uploadsadmin/" + fName));
    Response.End();
}

将断点放在条件(如果...)上,看看是否命中了断点。

于 2013-06-18T15:58:46.393 回答