Click here to Skip to main content
14,427,367 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hi my delete button which is a LinkButton doesn't work while it took a long time for me to type the code for it to work but I don't know what's wrong with it, please help. Here is some code for it.

using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class fileuploader : System.Web.UI.Page
{

protected void Page_Load(object sender, EventArgs e)
{
show_data();
}

private void show_data()
{
DirectoryInfo d = new DirectoryInfo(MapPath("~/data/"));
FileInfo[] r = d.GetFiles();
DataTable dt = new DataTable();
dt.Columns.Add("path");
for (int i = 0; i < r.Length; i++)
{
DataRow row=dt.NewRow();
row["path"] = "~/data/" + r[i].Name;
dt.Rows.Add(row);
}
DataList1.DataSource = dt;
DataList1.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
string path = "~/data/" + Guid.NewGuid().ToString() + "" + Path.GetExtension(FileUpload1.FileName);
FileUpload1.SaveAs(MapPath(path));
Response.Write("Save Succesfully");
show_data();
}
}
protected void LinkButton1_Command(object sender, CommandEventArgs e)
{
file.Delete(MapPath(e.CommandArgument.ToString()));
Response.Write("File Deleted Succesfully");
show_data();
}
}

What I have tried:

I've been told to add Onclick="LinkButton1_Command" but I'm not where to add it, is it in the source code and please list some steps I could follow as the solution for that
Posted
Updated 10-Jan-20 8:58am
Comments
Vincent Maverick Durano 7-Jan-20 12:30pm
   
When you said not working, you mean it's not hitting the LinkButton event? Also, can you wrap your code in page_load within Not IsPostback block?
Rate this:
Please Sign up or sign in to vote.

Solution 1

Quote:
Onclick="LinkButton1_Command"
Goes in your HTML as part of the button declaration - but you'd need to check that the CommandArgument value is correct as it would have to be a valid path to a file on the Server - you don't have access to delete files on the client at all.

I'd suggest you use the debugger to look at the method and exactly what is happening when you click the button.
   
Rate this:
Please Sign up or sign in to vote.

Solution 2

Add onlcik event in your aspx OnClick="LinkButton1_Command" and AutoPostBack="True"
   
Rate this:
Please Sign up or sign in to vote.

Solution 3

How do I have to exactly do that? I'm not sure exactly where that goes in the code. But when I add it to source code it shows an error with throw new NotImplementedException(); in red and that's in line 10 of the source and in file.cs when I click the LinkButton
   
Comments
BigBoss777 10-Jan-20 14:16pm
   
How do I have to exactly do that? I'm not sure exactly where that goes in the code. But when I add it to the source code it shows an error with throw new NotImplementedException(); in red and that's in line 10 of the source and in file.cs when I click the LinkButton but it's OnClientClick="LinkButton1_Command" that I have added which seems like it might work better because adding OnClick="LinkButton1_Command" with AutoPostBack="True" shows an error too.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100