ASP.NET GridView不会更新其行

时间:2013-12-03 00:19:46

标签: c# asp.net .net gridview

我对ASP.NET GridView Update Row遵循了此tutorial on MSDN,但它不起作用。

updatedItem.DepartureCity = ((TextBox)(row.Cells[2].Controls[0])).Text;

仍然从单元格中提供原始值,而不是更新的值。

public partial class ManagePage : System.Web.UI.Page
{
    BusScheduleModelContainer modelContainer = new BusScheduleModelContainer();
    protected void Page_Load(object sender, EventArgs e)
    {
        //FormsAuthentication.RedirectFromLoginPage()
        //if (!HttpContext.Current.User.Identity.IsAuthenticated)
        //{
        //    Server.Transfer("LoginPage.aspx");
        //}

        resultsGridView.DataSource = modelContainer.BusRoutes.ToList();
        resultsGridView.DataBind();
    }

    protected void RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        var routeID = int.Parse(e.Values[0].ToString());
        var removedItem = modelContainer.BusRoutes.FirstOrDefault(
            item => item.RouteID == routeID);

        if (removedItem != null)
        {
            modelContainer.BusRoutes.Remove(removedItem);
            resultsGridView.DataSource = modelContainer.BusRoutes.ToList();
            resultsGridView.DataBind();
            modelContainer.SaveChanges();
        }
    }

    protected void RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        var routeID = int.Parse(e.NewValues[0].ToString());
        var updatedItem = modelContainer.BusRoutes.FirstOrDefault(
            item => item.RouteID == routeID);

        if (updatedItem != null)
        {
            GridViewRow row = resultsGridView.Rows[e.RowIndex];
            var res = row.FindControl("ctl00$ContentPlaceHolder1$resultsGridView$ctl02$ctl03");
            updatedItem.DepartureCity = ((TextBox)(row.Cells[2].Controls[0])).Text;
            updatedItem.ArrivalCity = ((TextBox)(row.Cells[3].Controls[0])).Text;
            updatedItem.DepartureTime = DateTime.Parse(((TextBox)(row.Cells[4].Controls[0])).Text);
            updatedItem.ArrivalTime = DateTime.Parse(((TextBox)(row.Cells[5].Controls[0])).Text);
        }

        resultsGridView.EditIndex = -1;

        BindData();
    }

    protected void RowEditing(object sender, GridViewEditEventArgs e)
    {
        //Set the edit index.
        resultsGridView.EditIndex = e.NewEditIndex;
        //Bind data to the GridView control.
        BindData();
    }

    protected void RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        //Reset the edit index.
        resultsGridView.EditIndex = -1;
        //Bind data to the GridView control.
        BindData();
    }

    private void BindData()
    {
        resultsGridView.DataSource = modelContainer.BusRoutes.ToList();
        resultsGridView.DataBind();
    }
}

<div>
                <asp:GridView runat="server" ID="resultsGridView" 
                              AutoGenerateColumns="true" AllowPaging="true"
                              AutoGenerateDeleteButton="true" OnRowDeleting="RowDeleting"
                              AutoGenerateEditButton="true" OnRowUpdating="RowUpdating"
                              OnRowEditing="RowEditing" OnRowCancelingEdit="RowCancelingEdit">
                </asp:GridView>
            </div>

2 个答案:

答案 0 :(得分:0)

在RowUpdating方法中,您需要添加modelContainer.SaveChanges();,如下所示:

if (updatedItem != null)
{
    GridViewRow row = resultsGridView.Rows[e.RowIndex];
    var res = row.FindControl("ctl00$ContentPlaceHolder1$resultsGridView$ctl02$ctl03");
    updatedItem.DepartureCity = ((TextBox)(row.Cells[2].Controls[0])).Text;
    updatedItem.ArrivalCity = ((TextBox)(row.Cells[3].Controls[0])).Text;
    updatedItem.DepartureTime = DateTime.Parse(((TextBox)(row.Cells[4].Controls[0])).Text);
    updatedItem.ArrivalTime = DateTime.Parse(((TextBox)(row.Cells[5].Controls[0])).Text);
    modelContainer.SaveChanges();
}

答案 1 :(得分:0)

您是否使用CommandField进行更新控制? 如果是这样,当您单击更新按钮时,首先它将执行Page_Load事件处理程序,之后执行RowUpdating事件处理程序中的实现。

您应该尝试在 Page_Load 事件处理程序中检查回帖,如下所示:

protected void Page_Load(object sender, EventArgs e)
{  
   if(!IsPostBack)
   {
      resultsGridView.DataSource = modelContainer.BusRoutes.ToList();
      resultsGridView.DataBind();
   }
}

通过这种方式,它只会在您第一次打开此页面时将数据绑定到GridView。

对于回发事件,例如点击更新按钮,它不会再次将原始数据绑定到GridView。