ASP.Net GridView RowUpdate未触发,RowUpdating没有NewValues

时间:2013-02-06 08:09:26

标签: c# asp.net events gridview datatable

我有以下GridView:

<asp:GridView ID="gridCar" runat="server" RowStyle-ForeColor="Black" AllowSorting="true" OnSorting="gridCar_Sorting" AutoGenerateEditButton="true" OnRowEditing="gridCar_RowEditing" OnRowCancelingEdit="gridCar_RowCancelingEdit" OnRowUpdating="gridCar_RowUpdating" OnRowUpdated="gridCar_RowUpdated"></asp:GridView>

我想更新编辑按钮选择的行。问题是,如果我尝试在gridCar_RowUpdating中执行此操作,则e.NewValues不包含已编辑的值,而是包含旧值。

我四处搜索,发现我可能不得不使用onRowUpdated事件,但后来又面临另一个问题;这个事件根本没有被解雇。我尝试在e.Cancel = false中设置gridCar_RowUpdating,但这不会解决问题。

GridView绑定到DataTable。

编辑:这是我的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;

public partial class _Default : Page
{
    DataTable carsTable = new DataTable("cars");

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            carsTable.Columns.Add("CarID");
            carsTable.Columns.Add("CarRegNum");
            carsTable.Columns.Add("CarModel");
            carsTable.Columns.Add("CarType");
            carsTable.Columns.Add("CarOwner");

            carsTable.Rows.Add(1, "AAA-111", "Toyota", "Hatchback", "Matti");
            carsTable.Rows.Add(2, "BBB-222", "Mercedes-Benz", "Van", "Keijo");
            carsTable.Rows.Add(3, "CCC-333", "Renault", "Regular", "Matilda");

            carsTable.AcceptChanges();

            gridCar.DataSource = carsTable;
            gridCar.DataBind();
        }
    }

    protected string dataViewSortDirection(SortDirection direction)
    {
        switch (direction)
        {
            case SortDirection.Ascending:
                return "ASC";
            case SortDirection.Descending:
                return "DESC";
            default:
                throw new ArgumentOutOfRangeException();
        }
    }

    protected void gridCar_Sorting(object sender, GridViewSortEventArgs e)
    {
        gridCar.Sort(e.SortExpression, e.SortDirection);

        // update GridView
        gridCar.DataBind();
    }

    protected void gridCar_RowEditing(object sender, GridViewEditEventArgs e)
    {
        gridCar.EditIndex = e.NewEditIndex;
        gridCar.DataBind();
    }

    protected void gridCar_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        gridCar.EditIndex = -1;
        gridCar.DataBind();
    }

    protected void gridCar_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        labelDebug.Text += "b";
        //gridCar.EditIndex = -1;
        //gridCar.DataBind();
    }

    protected void gridCar_RowUpdated(object sender, GridViewUpdatedEventArgs e)
    {
        labelDebug.Text += "a";
        gridCar.EditIndex = -1;
        gridCar.DataBind();
    }
}

2 个答案:

答案 0 :(得分:5)

你确定你没有在回发上数据化GridView吗?你应该只做if(!IsPostBack)

protected void Page_Load(Object sender, EventArgs e)
{
    if(!Page.IsPostBack)
    {
        BindGridView();
    }
}

否则,您正在加载旧值并阻止RowUpdated事件。

<强>更新

  

我可能做了很多错事,你能告诉我我是怎么回事吗?   现在应该进行排序和更新吗?

这是一个完整的工作样本:

public partial class GridTest : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindGrid();
        }
    }

    private void BindGrid()
    {
        var source = GetCarTable().Select("", this.SortExpression).CopyToDataTable();
        GridCar.DataSource = source;
        GridCar.DataBind();
    }

    private string SortExpression
    {
        get
        {
            if (ViewState["SortExpression"] == null || string.IsNullOrEmpty((String)ViewState["SortExpression"]))
            {
                ViewState["SortExpression"] = "CarModel ASC";
            }
            return ViewState["SortExpression"].ToString();
        }
        set { ViewState["SortExpression"] = value; }
    }

    private static DataTable GetCarTable()
    {
        DataTable carsTable = new DataTable("cars");
        carsTable.Columns.Add("CarID");
        carsTable.Columns.Add("CarRegNum");
        carsTable.Columns.Add("CarModel");
        carsTable.Columns.Add("CarType");
        carsTable.Columns.Add("CarOwner");

        carsTable.Rows.Add(1, "AAA-111", "Toyota", "Hatchback", "Matti");
        carsTable.Rows.Add(2, "BBB-222", "Mercedes-Benz", "Van", "Keijo");
        carsTable.Rows.Add(3, "CCC-333", "Renault", "Regular", "Matilda");

        return carsTable;
    }

    protected void gridCar_Sorting(object sender, System.Web.UI.WebControls.GridViewSortEventArgs e)
    {
        string currentSortColumn = null;
        string currentSortDirection = null;
        currentSortColumn = this.SortExpression.Split(' ')[0];
        currentSortDirection = this.SortExpression.Split(' ')[1];

        if (e.SortExpression.Equals(currentSortColumn))
        {
            //switch sort direction
            switch (currentSortDirection.ToUpper())
            {
                case "ASC":
                    this.SortExpression = currentSortColumn + " DESC";
                    break;
                case "DESC":
                    this.SortExpression = currentSortColumn + " ASC";
                    break;
            }
        }
        else
        {
            this.SortExpression = e.SortExpression + " ASC";
        }

        //load the data with this SortExpression and DataBind the Grid
        BindGrid();
    }

    protected void GridCar_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridCar.EditIndex = e.NewEditIndex;
        BindGrid();
    }

    protected void GridCar_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridCar.EditIndex = -1;
        BindGrid();
    }

    protected void GridCar_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        var oldValues = e.OldValues;
        var newValues = e.NewValues;
        // BindGrid();
    }

    protected void GridCar_RowUpdated(object sender, GridViewUpdatedEventArgs e)
    {
        //BindGrid();
    }
}

aspx几乎保持不变:

<asp:GridView   ID="GridCar" 
            runat="server" 
            RowStyle-ForeColor="Black" 
            AllowSorting="true" 
            OnSorting="gridCar_Sorting" 
            AutoGenerateEditButton="true" 
            OnRowEditing="GridCar_RowEditing"
            OnRowCancelingEdit="GridCar_RowCancelingEdit"
            OnRowUpdating="GridCar_RowUpdating" 
            OnRowUpdated="GridCar_RowUpdated"
            AutoGenerateColumns="true">
</asp:GridView>

答案 1 :(得分:0)

您的代码没有DataSourceID。我假设数据绑定在Code Behind。

如果您只想编辑/更新GridView的行,您需要的是 ItemUpdating 事件(例如 gridcar_ItemUpdating )和而不是RowUpdating < / em>的

我希望它可以帮到你。