【问题标题】:Gridview usercontrol with delete row functionality doesn't update my gridview after delete具有删除行功能的 Gridview 用户控件在删除后不会更新我的 gridview
【发布时间】:2013-11-22 16:09:51
【问题描述】:

我有一个带有 CRUD 功能的 gridview 用户控件。我有一个 OnRowDeleting 事件和一个 RowCommand 事件来检查我的“删除”按钮是否被点击。

OnRowDeleting:

protected void grd_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        grd.EditIndex = -1;
        pnlSelected.Visible = false;
        btnSave.Visible = false;
        btnInsert.Visible = false;
        grd.DataBind();
    }

行命令:

protected void grd_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Delete")
        {
            LinkButton lnkBtn = (LinkButton)e.CommandSource;    // the button
            GridViewRow myRow = (GridViewRow)lnkBtn.Parent.Parent;  // the row
            GridView myGrid = (GridView)sender; // the gridview

            string item = (string)myGrid.DataKeys[myRow.RowIndex].Value.ToString();
            DataController.DeleteByPrimKey(item);
        }
    }

在我的网络表单中,我填写了用户控件的数据源。现在的问题是当我想删除一条记录时。

ucShowHouseList.DataSource = myDataSource;

如果我在 gridview 中删除记录,gridview 不会显示更改。如果我刷新页面,记录将被删除。

可能是什么问题?

编辑:

我认为我的问题是用户控件中的数据源在删除后没有更新。所以在我的网络表单的 Page_Load 中,我填写了我的用户控件的数据源:

ucShowHouseList.DataSource = myDataSource;

我现在在我的用户控件中删除行:

grd.DataSource = DataSource;
grd.DataBind();

DataSource 是我的用户控件中的一个 IENumerable 属性,我在我的 web 表单中的 page_load 上填写它。

public IEnumerable DataSource;

这会返回一个项目集合,并且在我删除一行时不会更新,因为我的页面没有重新加载,因此无法更新集合。

如何解决?

【问题讨论】:

  • 旁注:使用lnkBtn.NamingContainer 代替lnkBtn.Parent.Parent 来获取GridViewRow。根据你的问题,DataBindGridView是你改了DataSource之后吗?
  • 好吧,没关系,我通过重新填充数据源然后重新绑定它解决了我的问题。我的问题现在解决了

标签: asp.net gridview crud


【解决方案1】:

您需要在grd_RowDeleting 而不是grd.DataBind(); 之后调用Gridview 绑定函数 Gridview.DataBind 需要绑定数据源。 Here

【讨论】:

    猜你喜欢
    • 2015-11-16
    • 2012-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多