Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I Want to delete image from data list control as well as folder , by clicking to link button. i struggling for that and did not succeed please guide me.

my aspx code is

<asp:DataList ID="DataList1" runat="server" RepeatColumns = "4" CellPadding = "4" Width="100%" OnDeleteCommand="DataList1_DeleteCommand" >
<itemtemplate>

<asp:Image ID="Image1" ImageUrl='<%# String.Format("~/DistributorShopImages/{0}", Eval("ShopImagesExt")) %>' onclick = "LoadDiv(this.src, this)" style ="cursor:pointer" runat="server" Height="200"
Width="200" />




<asp:LinkButton ID="Delete" Height="22px" CommandName="Delete" Width="100px" runat="server" CommandArgument ='<%# Container.DataItem %>' ForeColor="Red">Delete









my CS code is

C#
protected void DataList1_DeleteCommand(object source, DataListCommandEventArgs e)
      {

          
         
          string file = e.CommandArgument.ToString();


          File.Delete(Server.MapPath(file));

          FileInfo fInfo;

          fInfo = new FileInfo(file);

          fInfo.Delete();
          DataList1.DataBind();
}
Posted
Updated 26-Nov-15 22:24pm
v3

1 solution

Add a Label On your Datalist...That Label Would be bind by the path.. like ~/Upload/image.jpg or ..Upload/image.jpg


<asp:datalist id="DataList1" runat="server" repeatcolumns="4" cellpadding="4" width="100%" ondeletecommand="DataList1_DeleteCommand" xmlns:asp="#unknown">


<asp:image id="Image1" imageurl="<%# String.Format("~/DistributorShopImages/{0}", Eval("ShopImagesExt")) %>" onclick="LoadDiv(this.src, this)" style="cursor:pointer" runat="server" height="200">
Width="200" />

<asp:label id="lbl_del_image" runat="server" text="<%#Eval("ShopImagesExt") %>" visible="false">

<asp:linkbutton id="Delete" height="22px" width="100px" runat="server" forecolor="Red" text="Delete" onclick="Delete_Click" />




C#
protected void Delete_Click(object sender, EventArgs e)

    {   

      DataListItem item = ((LinkButton)sender).NamingContainer as DataListItem;

       Label del_path = (Label)item.FindControl("bl_del_image");   //  Label Id

         string str =del_path.Text;
        String strPath = Server.MapPath(str);
     System.IO.File.Delete(strPath);

}
 
Share this answer
 
v2
Comments
Noman Suleman 27-Nov-15 5:17am    
i did as you define but its not deleting image from folder
Member 13238549 16-Aug-17 7:42am    
ghfhfg
Arasappan 27-Nov-15 5:18am    
make the break point and run..what did u getting in del_path
Noman Suleman 27-Nov-15 5:23am    
sir i got the path image.jpg although the complete path is ~/DistributorShopImages/image.jpg folder name is missing in label
Arasappan 27-Nov-15 5:26am    
what path u used to show the image as a url..Bind simailar to the label...

For Image It will be Image Url
For Label It will be Text.. Bind Similar to the label which is to display the image

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