如何使用asp.net删除gridview行时执行物理文件删除操作

时间:2016-10-04 07:49:39

标签: asp.net gridview aspxgridview

我是Grid View的新手, 我在网格视图中获得了一些文件名形式Sql Data Source,如果我删除该行,还应该删除一些其他文件夹中存在的物理文件。 这是我的代码我试过..

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if(e.Row.RowType==DataControlRowType.DataRow)
            {
               Control control=e.Row.Cells[0].Controls[0];

               String Filename = e.Row.Cells[2].ToString();

                if (control is LinkButton)
                {
                    ((LinkButton)control).OnClientClick = "return confirm('Are you Sure you want to delete? this Cannot be undone')";

                 if((File.Exists(Server.MapPath("~/Content/MainContent/BulkDataContent/" + Filename))))
                     {
                    File.Delete(Server.MapPath("~/Content/MainContent/BulkDataContent/" + Filename));
                     }
                }

但问题是,当我从网格视图中删除一个文件时,从网格视图删除一行并删除所有物理文件从文件夹中删除...它循环结束直到结束.. 帮我解决这个问题...... 在此先感谢。

1 个答案:

答案 0 :(得分:1)

更新了Ans

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if(e.Row.RowType==DataControlRowType.DataRow)
            {
               Control control=e.Row.Cells[0].Controls[0];

               String Filename = e.Row.Cells[2].ToString();

                if (control is LinkButton)
                {
                    ((LinkButton)control).OnClientClick = "return confirm('Are you Sure you want to delete? this Cannot be undone')";
    //write here your Delete query/Procedure


    if((File.Exists(Server.MapPath("~/Content/MainContent/BulkDataContent/" + Filename))))
                 {
                File.Delete(Server.MapPath("~/Content/MainContent/BulkDataContent/" + Filename));
                 }
            }