感谢您对此代码的帮助,我只想使用 C# 添加一个删除按钮以从 dataList 中删除图像,我是新手,我不知道该去哪里,这就是代码。
<asp:DataList ID="imgList" repeatColumns="3" runat="server" CssClass="auto-style1" Width="681px">
<ItemTemplate>
<asp:Image ID="img1" runat="server" ImageUrl='<%# Bind("Name", "~/images/{0}") %>' width="200px" Height="200px" />
<asp:Button Text="Delete" id="deleteButton" runat="server" OnClick="Delete_Command" />
</ItemTemplate>
</asp:DataList>
--------------------------------
protected void Delete_Command (object sender, EventArgs e)
{
string fileName = e.CommandArgument.ToString();
File.Delete(Server.MapPath(fileName));
FileInfo fInfo;
fInfo = new FileInfo(fileName);
fInfo.Delete();
imgList.DataBind();
}
这个问题在约翰的帮助下得到了解决,现在我有另一个问题,我也感谢任何帮助
<asp:DataList ID="imgList" repeatColumns="3" runat="server" CssClass="auto-style1" Width="681px">
<ItemTemplate >
<asp:Image ID="img1" runat="server" ImageUrl='<%# Bind("Name", "~/images/{0}") %>' width="200px" Height="200px" />
<asp:Button Text="Delete" ID="deleteButton" OnCommand="Unnamed1_Command" commandArgument='<%# Bind("Name", "~/images/{0}") %>' runat="server" />
</ItemTemplate>
</asp:DataList>
protected void Unnamed1_Command(object sender, CommandEventArgs e)
{
string fileName = e.CommandArgument.ToString();
File.Delete(Server.MapPath(fileName));
FileInfo fInfo;
fInfo = new FileInfo(fileName);
fInfo.Delete();
imgList.DataBind();
}
我面临错误 system.io.directorynotfoundexception 找不到" fInfo.Delete((;" 的路径的一部分我不知道为什么,我在网上尝试了一些解决方案,但到目前为止没有任何效果。
经过多次检查,我找到了答案,代码终于可以工作了,我会分享,如果有人要添加一些东西来让它变得更好,请写下来:
protected void Unnamed1_Command(object sender, CommandEventArgs e)
{
FileInfo fInfo = new FileInfo (Server.MapPath(e.CommandArgument.ToString()));
fInfo.Delete();
imgList.DataBind();
}
<asp:DataList ID="imgList" repeatColumns="3" runat="server" CssClass="auto-style1" Width="681px">
<ItemTemplate >
<asp:Image ID="img1" runat="server" ImageUrl='<%# Bind("Name", "~/images/{0}") %>' width="200px" Height="200px" />
<asp:Button Text="Delete" ID="deleteButton" OnCommand="Unnamed1_Command" commandArgument='<%# Bind("Name", "~/images/{0}") %>' runat="server" />
</ItemTemplate>
</asp:DataList>