gpt4 book ai didi

c# - ASP.Net GridView RowUpdate 未触发且 RowUpdating 没有 NewValues

转载 作者:行者123 更新时间:2023-12-02 22:14:28 25 4
gpt4 key购买 nike

我有以下 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 绑定(bind)到 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 进行数据绑定(bind)吗?你应该只这样做 if(!IsPostBack):

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

否则您将加载旧值并阻止 RowUpdated 事件。

更新

I'm probably doing a lot of things wrong, can you show me how I should do sorting and updating now?

这是一个完整的工作示例:

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>

关于c# - ASP.Net GridView RowUpdate 未触发且 RowUpdating 没有 NewValues,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14724295/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com