2012-04-24 78 views
0

我只是在我的GridView中显示我上传的文件的详细信息,所以在我的GridView中只有一行 - 不允许多个文件。Gridview显示已删除的数据

当我删除单行并尝试上载新文件时,它显示2行(新文件​​和删除的文件)。

我已经尝试过使用GridView.DataSource = nullGridView.DataBind()

注意:我删除后重新绑定了GridView,但仍显示已删除的文件。

protected void DeleteLinkButton_Click(object sender, EventArgs e) 
{ 
    if (Session["name"] != null) 
    { 
     string strPath = Session["filepath"].ToString(); 
     System.IO.File.Delete(strPath); 

     GridView2.Rows[0].Visible = false; 
     Label8.Text = ""; 
     Session["filename"] = null; 
     Button3.Enabled = true; 
    } 

    GridView2.DataBind(); 
} 
+0

您必须将其从网格的“DataSource”中移除。 – 2012-04-24 15:25:20

+0

我尝试使用gridview.datasource = null和gridview.databind(),但没有变化 – newuser1555 2012-04-24 15:31:40

+0

你在哪里设置数据源到gridview?我只能看到** DataBind()** – 2012-04-24 15:38:59

回答

0

在某些情况下,我不得不做这样的事情:

//page level variable 
bool refreshRequired = false; 

protected void DeleteLinkButton_Click(object sender, EventArgs e) 
{ 
    if (Session["name"] != null) 
    { 
     string strPath = Session["filepath"].ToString(); 
     System.IO.File.Delete(strPath); 

     GridView2.Rows[0].Visible = false; 
     Label8.Text = ""; 
     Session["filename"] = null; 
     Button3.Enabled = true; 

     refreshRequired = true; 
    } 

} 

protected void Page_PreRender(object sender, EventArgs e) 
{ 
    if(refreshRequired) 
    { 
     //whatever you to to set your grids dataset, do it here 
     //but be sure to get the NEW data 
    } 
} 

截至Delete的时候,你的网格绑定到旧数据。当您更改数据时,您必须将其重新绑定到新数据。