ASP.Net Gridview不会在按钮单击时重新绑定数据

时间:2014-06-17 14:10:54

标签: c# asp.net sql-server linq

我是asp.net中的新手我使用LINQ与asp.net按钮点击事件我的gridview没有重新绑定数据,是gridview进入更新面板

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                “>                                                                                                                                                                                                                                                                                         “>                                                                                                                                                           

protected void btnSave_Click(object sender, EventArgs e)
{
    foreach (GridViewRow gvr in gvClientData.Rows)
    {

        if (((CheckBox)gvr.FindControl("chkdisplay")).Checked == true)
        {
            string Index = ((Label)gvr.FindControl("lblIndex")).Text;
            int GIIndex = Convert.ToInt32(Index);
            GI_InsureMaster insertclientinfo = vjdb.GI_InsureMasters.Single(upd => upd.GIMastIndex == GIIndex);
            insertclientinfo.SendToCompany = true;
            vjdb.SubmitChanges();
        }

    }
    BindAgencyData();
    Response.Redirect(Request.RawUrl);
}

1 个答案:

答案 0 :(得分:0)

您似乎正在尝试修改对象,然后将其保存回数据库,但是您做错了。

您正在使用其他数据上下文vjdb查询该对象,并且您正在SubmitChanges上调用linqobject。您应该在SubmitChanges

上致电vjdb
protected void btnSave_Click(object sender, EventArgs e)
{
    foreach (GridViewRow gvr in gvClientData.Rows)
    {

        if (((CheckBox)gvr.FindControl("chkdisplay")).Checked == true)
        {
            string Index = ((Label)gvr.FindControl("lblIndex")).Text;
            int GIIndex = Convert.ToInt32(Index);
            GI_InsureMaster insertclientinfo = vjdb.GI_InsureMasters.Single(upd => upd.GIMastIndex == GIIndex);
            insertclientinfo.SendToCompany = true;
            vjdb.SubmitChanges(); //HERE
        }

    }
    BindAgencyData();
    Response.Redirect(Request.RawUrl);
}

假设BindAgencyData正在查询数据库以获取最新/更新的记录,然后将数据绑定到网格。