c# - GridView RowUpdating 没有获取新数据

标签 c# asp.net gridview

当尝试更新 GridView 数据时,它成功运行,但它获取的是旧数据而不是您在文本框中键入的数据。

这是我的:

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        string name = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text;
        string phone = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text;
        string email = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text;
        int contactId = Convert.ToInt32(((TextBox)(GridView1.Rows[e.RowIndex].Cells[4].Controls[0])).Text);
        objLogic.UpdateContact(name, phone, email, contactId);  //passes values to SQL to update database
        GridView1.EditIndex = -1;
        GridView1.DataBind();
    }

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

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DropDownList1.DataSource = objLogic.LoadClient();
            DropDownList1.DataTextField = "name";
            DropDownList1.DataValueField = "clientId";
            DropDownList1.DataBind();
        }

        GridView1.DataSource = objLogic.LoadContacts(Convert.ToInt16(DropDownList1.SelectedValue));
        GridView1.DataBind();

比如当前数据是:

姓名:布莱克,电话:123-234-3456,电子邮件:test@test.com,联系方式:22

我输入新数据:

姓名:约翰,电话:555-555-5555,电子邮件:test2@test2.com,联系人 ID:22

最终进入数据库的数据:

姓名:布莱克,电话:123-234-3456,电子邮件:test@test.com,联系方式:22

最佳答案

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        DropDownList1.DataSource = objLogic.LoadClient();
        DropDownList1.DataTextField = "name";
        DropDownList1.DataValueField = "clientId";
        DropDownList1.DataBind();
        BindGrid();
    }
 }

protected void BindGrid()
{
    GridView1.DataSource = objLogic.LoadContacts(Convert.ToInt16(DropDownList1.SelectedValue));
    GridView1.DataBind();
}

每次页面回发时,您都在重新绑定(bind)网格。将网格的绑定(bind)移动到一个单独的函数中。完成行更新后,重新绑定(bind)网格。

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
    string name = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text;
    string phone = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text;
    string email = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text;
    int contactId = Convert.ToInt32(((TextBox)(GridView1.Rows[e.RowIndex].Cells[4].Controls[0])).Text);
    objLogic.UpdateContact(name, phone, email, contactId);  //passes values to SQL to update database
    GridView1.EditIndex = -1;
    BindGrid();
}

当您编辑时,您也必须调用 BindGrid。

protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
    GridView1.EditIndex = e.NewEditIndex;
    //GridView1.DataBind(); this is meaningless, you have not set a DataSource
    BindGrid();
}

关于c# - GridView RowUpdating 没有获取新数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25121636/

相关文章:

c# - EWS 托管 API 拉取通知以查看新电子邮件不起作用

c# - JumpList.GetJumpList 不包含最近的项目

c# - ASP.NET Web API 中的自定义方法名称

asp.net - 清除 ASP.NET 中的 session

c# - 当前上下文中不存在名称 'ClientScript'

android - 如何在 Android 中访问网格中选定图像的 View 资源 ID?

android - 选项卡 fragment 内的 gridview

c# - RichTextBox (WPF) 没有字符串属性 "Text"

ASP.NET Core Azure AD OpenID - 生产服务器上的 token 签名验证失败

c# - 将数据更新到asp.net中的gridview中