Gridview行更新按钮不起作用

时间:2017-04-26 14:24:30

标签: c# asp.net gridview datagridview

当我按下编辑然后按下按钮以应用它没有做任何事情的更改时,我的gridview行更新按钮不起作用。我错过了什么吗?

我的代码看起来像这样

C#

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
    string afspraak = ((TextBox)(GridView1.Rows[e.RowIndex].FindControl("EditAfspraak"))).Text;
    string uitleg = ((TextBox)(GridView1.Rows[e.RowIndex].FindControl("EditUitleg"))).Text;
    int id = Convert.ToInt32(((TextBox)(GridView1.Rows[e.RowIndex].FindControl("EditID"))).Text);
    _controller.RecordUpdatenTblAfspraken(afspraak, uitleg, id);
}

protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
    GridView1.EditIndex = e.NewEditIndex;
}

protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
    GridView1.EditIndex = -1;
}

ASP.NET

无法正确粘贴它,所以它在这里:

https://pastebin.com/S3S09nLL

突出显示的按钮 enter image description here

1 个答案:

答案 0 :(得分:1)

更改EditIndex后,您必须重新绑定网格。

protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
    GridView1.EditIndex = e.NewEditIndex;

    GridView1.DataSource = mijnDatabaseBron;
    GridView1.DataBind();
}

同样适用于GridView1_RowCancelingEdit以及GridView1_RowUpdating中的更新成功。这假设您已将OnRowEditingOnRowCancelingEditOnRowUpdating事件添加到GridView。

同样在user7415073注释时,GridView的绑定必须在IsPostBack项检查中。

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        GridView1.DataSource = mijnDatabaseBron;
        GridView1.DataBind();
    }
}

请在此处查看我的答案,了解如何轻松地将EditID传递给后面的代码。

Get old value of label inside GridView during RowUpdating (ASP.NET GridView) C#