Click here to Skip to main content
15,905,504 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have created code for deleting single image file through button, my button and image is in Datalist control, and when i click on delete button the image is not getting selected so i can't get the particular imageurl of that image.

//this my design page code..

<asp:DataList ID="ImageDataList" runat="server" RepeatColumns="6" OnItemDataBound="ImageDataList_ItemDataBound"
OnItemCommand="ImageDataList_ItemCommand">
<itemtemplate>

class="detailItem">

<asp:ImageButton class="example-image-link" ID="Image1" runat="server" Height="150px" Width="150px"/>


<asp:Button ID="btndelete" runat="server" Text="Delete" OnClick="btndelete_Click"/>





//this my code behind code

protected void ImageDataList_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
((System.Web.UI.WebControls.ImageButton)e.Item.FindControl("Image1")).ImageUrl = e.Item.DataItem.ToString();
}
}

protected void ImageDataList_ItemCommand(object source, DataListCommandEventArgs e)
{
ImageButton lnkval = (ImageButton)e.Item.FindControl("Image1");
string path = lnkval.ImageUrl;// lnkval.Text;
Session.Add("path", path);

}

protected void btndelete_Click(object sender, EventArgs e)
{
string imgpath = ((System.Web.UI.WebControls.Button)ImageDataList.FindControl("btndelete")).Text;
}
protected void lnkmouseclick_Click(object sender, EventArgs e)
{
//DirectoryInfo directoryInfo = new DirectoryInfo(imgpath);
//FileInfo[] fileInfo = directoryInfo.GetFiles();
//string fullpath = fileInfo[0].Name;
//string filename = ImageDataList.FindControl("Image1").ToString();
//if (filename != "")
//{
// string globalpath = Session["globalpath"] as string;
// File.Delete(globalpath + filename);
//}
}

// what should i do, can u help me please.
Posted

There is no need of lnkmouseclick_Click Event here,

Change your btnDelete_click Eventlike this,

C#
protected void btndelete_Click(object sender, EventArgs e)
   {
      ImageButton img = (ImageButton)e.Item.FindControl("Image1");
      string path = img.ImageUrl;

      string globalpath = Session["globalpath"] as string;
      File.Delete(globalpath+"/"path);
   }


try this and inform me...
All the best...
 
Share this answer
 
Comments
sumit kausalye 12-Mar-14 3:41am    
i have done as you said but the problem is i am not getting the path.i guess this is because when i click on delete button the image is not getting selected so i am not getting the path of any particular image. what should i do to select the image?

And when i copied your code it gives me error
on this line
ImageButton img = (ImageButton)e.Item.FindControl("Image1");

because it is giving error to Item.


First, try to get the row index of the clicked row, and then try accessing the image by using the index obtained.
 
Share this answer
 
Comments
sumit kausalye 12-Mar-14 3:51am    
sir i wanted to try this same logic earlier but i don't get e.RowIndex value in button click event. so i am not getting value of particular image.
Then go for client side event, use javascript or jquery to handle button click event and get imageUrl and store it in some Hidden Field...
 
Share this answer
 

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