0

我只是在我的 GridView 中显示我上传的文件详细信息,因此我的 GridView 中只有一行 - 不允许多个文件。

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

我已经尝试过使用GridView.DataSource = nulland GridView.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();
}
4

1 回答 1

0

In some cases I've had to do something like this:

//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
    }
}

At the time of Delete, your grid is bound to the old data. When you change the data, you must rebind it to the new.

于 2012-04-24T16:31:16.503 回答