这是我的交易,我得到了一个名为 gridview1 的 datagridviewer 和一个 fileupload1 ,当我上传文件时,它会使用文件名和路径更新数据库中的 gridview1 和表,并将所述文件存储在文件夹“Mag”中......但现在我做什么我想做的是相反的,我知道如何使用 gridview 删除表条目,但从文件夹“Mag”中删除文件不起作用,在 C# 或代码隐藏中使用了以下代码
protected void GridView1_Del(object sender, EventArgs e)
{
string DeleteThis = GridView1.SelectedRow.Cells[0].Text;
string[] Files = Directory.GetFiles(@"i:/Website/WebSite3/Mag/");
foreach (string file in Files)
{
if (file.ToUpper().Contains(DeleteThis.ToUpper()))
{
File.Delete(file);
}
}
}
它给了我错误
“你调用的对象是空的。”
请告诉我我做错了什么是新的,不需要深入了解该平台,因此任何和所有帮助将不胜感激
提前致谢
标记
这是我找到的答案,感谢塔米和其他人的所有答案
好的,这里的处理目标函数从 gridview 和数据库表中删除文件详细信息,并从存储文件的项目文件夹中删除文件
在 gridview 的脚本部分中,您需要包含
OnRowDeleting="FuntionName"
Not
OnSelectedIndexChanged = "FuntionName"
or
OnRowDeleted="FuntionName"
然后在 C# 代码中(代码隐藏)
protected void FuntionName(object sender, GridViewDeleteEventArgs e)
{
// storing value from cell
TableCell cell = GridView1.Rows[e.RowIndex].Cells[0];
// full path required
string fileName = ("i:/Website/WebSite3/Mag/" + cell.Text);
if(fileName != null || fileName != string.Empty)
{
if((System.IO.File.Exists(fileName)))
{
System.IO.File.Delete(fileName);
}
}
}
仅供想学习的人补充参考
OnRowDeleting="FuntionName" 用于在删除行之前,您可以取消删除或像我一样对数据运行函数
OnRowDeleted="FuntionName" 直接删除