ASP.NET GridView控件不更新其行 [英] ASP.NET GridView does not update its row

查看:129
本文介绍了ASP.NET GridView控件不更新其行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我跟着这个<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowupdating%28v=vs.110%29.aspx\"相对=nofollow>教程MSDN上了解ASP.NET的GridView更新行,但它不工作。

  updatedItem.DepartureCity =((文本框)(row.Cells [2] .Controls [0]))文本。

仍然给出了从细胞的原始值,而不是将更新之一。

 公共部分类ManagePage:System.Web.UI.Page
{
    BusScheduleModelContainer modelContainer =新BusScheduleModelContainer();
    保护无效的Page_Load(对象发件人,EventArgs的发送)
    {
        //FormsAuthentication.RedirectFromLoginPage()
        //如果(!HttpContext.Current.User.Identity.IsAuthenticated)
        // {
        // Server.Transfer的(LoginPage.aspx);
        //}        resultsGridView.DataSource = modelContainer.BusRoutes.ToList();
        resultsGridView.DataBind();
    }    保护无效RowDeleting(对象发件人,GridViewDeleteEventArgs E)
    {
        变种routeID = int.Parse(e.Values​​ [0]的ToString());
        VAR removedItem = modelContainer.BusRoutes.FirstOrDefault(
            项目= GT; item.RouteID == routeID);        如果(removedItem!= NULL)
        {
            modelContainer.BusRoutes.Remove(removedItem);
            resultsGridView.DataSource = modelContainer.BusRoutes.ToList();
            resultsGridView.DataBind();
            modelContainer.SaveChanges();
        }
    }    保护无效RowUpdating(对象发件人,GridViewUpdateEventArgs E)
    {
        变种routeID = int.Parse(e.NewValues​​ [0]的ToString());
        VAR updatedItem = modelContainer.BusRoutes.FirstOrDefault(
            项目= GT; item.RouteID == routeID);        如果(updatedItem!= NULL)
        {
            GridViewRow行= resultsGridView.Rows [e.RowIndex]
            VAR解析度= row.FindControl(ctl00 $ ContentPlaceHolder1 $ resultsGridView $ ctl02 $ ctl03);
            。updatedItem.DepartureCity =((文本框)(row.Cells [2] .Controls [0]))的文本;
            。updatedItem.ArrivalCity =((文本框)(row.Cells [3] .Controls [0]))的文本;
            updatedItem.DepartureTime = DateTime.Parse(((文本框)(row.Cells [4] .Controls [0]))文本。);
            updatedItem.ArrivalTime = DateTime.Parse(((文本框)(row.Cells [5] .Controls [0]))的文本。);
        }        resultsGridView.EditIndex = -1;        BindData();
    }    保护无效RowEditing(对象发件人,GridViewEditEventArgs E)
    {
        //设置编辑索引。
        resultsGridView.EditIndex = e.NewEditIndex;
        //数据绑定到GridView控件。
        BindData();
    }    保护无效RowCancelingEdit(对象发件人,GridViewCancelEditEventArgs E)
    {
        //重置编辑索引。
        resultsGridView.EditIndex = -1;
        //数据绑定到GridView控件。
        BindData();
    }    私人无效BindData()
    {
        resultsGridView.DataSource = modelContainer.BusRoutes.ToList();
        resultsGridView.DataBind();
    }
}&LT; D​​IV&GT;
                &LT; ASP:GridView控件=服务器ID =resultsGridView
                              的AutoGenerateColumns =真AllowPaging =真
                              AutoGenerateDeleteButton =真OnRowDeleting =RowDeleting
                              AutoGenerateEditButton属性=真OnRowUpdating =RowUpdating
                              OnRowEditing =RowEditingOnRowCancelingEdit =RowCancelingEdit&GT;
                &LT; / ASP:GridView的&GT;
            &LT; / DIV&GT;


解决方案

你使用CommandField中进行更新CONTROLER?
如果是这样,当你点击更新按钮,首先它会做的Page_Load 事件处理程序,以后做 RowUpdating 的实施事件处理程序。

您应该尝试检查后回来的的Page_Load 事件处理程序是这样的:

 保护无效的Page_Load(对象发件人,EventArgs的发送)
{
   如果(!的IsPostBack)
   {
      resultsGridView.DataSource = modelContainer.BusRoutes.ToList();
      resultsGridView.DataBind();
   }
}

通过这种方式,将数据只是第一次打开这个页面绑定到GridView。

有关回发事件,如点击更新按钮,也不会绑定原始数据的GridView一次。

I followed this tutorial on MSDN for ASP.NET GridView Update Row, but it does not work.

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>

解决方案

Do you use CommandField for update controler? If so, when you click update button, first it will do Page_Load event handler, after that do the implementation in RowUpdating event handler.

You should try to check post back in Page_Load event handler like this:

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.

For post back event such as clicking update button, it will not bind the original data to GridView again.

这篇关于ASP.NET GridView控件不更新其行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆