这是交易我得到了一个名为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);
}
}
}
它给了我错误
“你调用的对象是空的。”
请告诉我我做错了什么是新的,不必深入了解平台,因此任何和所有帮助都将不胜感激,提前感谢马克
这是我找到的答案感谢 Tammy 和其他所有人的所有答案
好的,这里的交易目标功能从gridview和数据库表中删除文件详细信息,并从存储文件的项目文件夹中删除文件
在 gridview 的脚本部分中,您希望包括
OnRowDeleting="FuntionName"
不是
OnSelectedIndexChanged = "FuntionName"
或者
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" 它直接删除