ASP.Net GridView RowUpdate未启动,RowUpdating未';t没有新值
本文关键字:新值 RowUpdating GridView Net RowUpdate 启动 ASP | 更新日期: 2023-09-27 17:57:47
我有以下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
事件,但随后我面临另一个问题;这个活动根本没有被启动。我尝试在gridCar_RowUpdating
中设置e.Cancel = false
,但这并不能解决问题。
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();
}
}
您确定不在回发时对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>
您的代码没有DataSourceID。我假设数据绑定在代码背后。
如果您只想编辑/更新GridView的行,则需要ItemUpdating事件(例如gridcar_ItemUpdating)和而不是RowUpdating。
我希望它能帮助你。