简体   繁体   English

ASP.NET GridView不会更新其行

[英]ASP.NET GridView does not update its row

I followed this tutorial on MSDN for ASP.NET GridView Update Row, but it does not work. 在MSDN上针对ASP.NET GridView更新行遵循了本教程 ,但是它不起作用。

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

Still gives the original value from the cell and not the updated one. 仍然会提供单元格的原始值,而不是更新的值。

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>

In RowUpdating method you need to add modelContainer.SaveChanges(); 在RowUpdating方法中,您需要添加modelContainer.SaveChanges(); like below: 如下所示:

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();
}

Do you use CommandField for update controler? 您是否将CommandField用于更新控制程序? If so, when you click update button, first it will do Page_Load event handler, after that do the implementation in RowUpdating event handler. 如果是这样,当您单击更新按钮时,首先它将执行Page_Load事件处理程序,然后在RowUpdating事件处理程序中执行实现。

You should try to check post back in Page_Load event handler like this: 您应该尝试像这样在Page_Load事件处理程序中检查回发:

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

By this way, it will bind data to the GridView only first time you open this page. 通过这种方式,它将仅在您首次打开此页面时将数据绑定到GridView。

For post back event such as clicking update button, it will not bind the original data to GridView again. 对于诸如单击更新按钮之类的回发事件,它将不会再次将原始数据绑定到GridView。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM