【问题标题】:ASP.NET GridView does not update its rowASP.NET GridView 不更新其行
【发布时间】:2013-12-03 00:19:45
【问题描述】:

我为 ASP.NET GridView 更新行关注了这个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>

【问题讨论】:

    标签: c# asp.net .net gridview


    【解决方案1】:

    您是否使用 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。

    【讨论】:

    • 非常感谢,这就是我的问题的解决方案。
    【解决方案2】:

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

    【讨论】:

    • 感谢您的来信。问题是当我访问 ((TextBox)(row.Cells[2].Controls[0])).Text 我仍然得到旧值而不是输入值。
    猜你喜欢
    • 2010-09-16
    • 1970-01-01
    • 2013-01-16
    • 2011-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多