我有一个转发器控件来显示指定文件夹中的一些图片。中继器具有图像和内部按钮以删除该图像。但问题是如何在按钮点击事件中达到该图像名称?这是我的代码:
Aspx文件:
<asp:Repeater ID="rptImage" runat="server">
<ItemTemplate>
<asp:Button id="btnDelete" runat="server" Text="DELETE" OnClick="btnDelete_Click" />
<asp:Image ID="image" runat="server" ImageUrl='<%# Container.DataItem %>' />
</ItemTemplate>
</asp:Repeater>
CS档案:
private void getImages(string serialNo)
{
try
{
string[] filesindirectory = Directory.GetFiles(Server.MapPath("~/Images/" + serialNo + "/"));
List<String> images = new List<string>(filesindirectory.Count());
foreach (string item in filesindirectory)
{
images.Add(String.Format("~/Images/" + serialNo + "/{0}", System.IO.Path.GetFileName(item)));
}
rptImage.DataSource = images;
rptImage.DataBind();
}
catch (Exception)
{
header.Visible = true;
}
}
protected void btnDelete_Click(object sender, EventArgs e)
{
//string imgUrl;
//The url of image is all i need to delete it
}
答案 0 :(得分:1)
使用Button的CommandName,CommandArgument和OnCommand属性。
protected void Btn_Click(object sender, CommandEventArgs e)
{
if (e.CommandName == "BtnClick"){
e.CommandArgument.ToString();
}
}
<asp:Repeater ID="rptImage" runat="server">
<ItemTemplate>
<asp:Button id="btnDelete" runat="server" Text="DELETE" OnCommand="Btn_Click" CommandName="Btn_Click" CommandArgument="<%# Container.DataItem %>" />
<asp:Image ID="image" runat="server" ImageUrl="<%# Container.DataItem %>" />
</ItemTemplate>
</asp:Repeater>