gpt4 book ai didi

ASP.NET - Gridview RowDeleting 事件上没有 Datakey!

转载 作者:行者123 更新时间:2023-12-02 21:10:15 25 4
gpt4 key购买 nike

我有一个像这样的 GridView:

<asp:GridView ID="gvwStudents" runat="server" 
AutoGenerateColumns="False" DataKeyNames="ID"
ShowHeader="False" onrowdeleting="gvwStudents_RowDeleting">
<Columns>
<asp:BoundField DataField="FirstName" />
<asp:BoundField DataField="LastName" />
<asp:BoundField DataField="Email" />
<asp:CommandField ShowDeleteButton="True" DeleteText="Remove" />
</Columns>
</asp:GridView>

以下是我创建 DataTable 的方法,GridView 绑定(bind)到该 DataTable,以便您知道我正在处理哪些数据:

private DataTable MakeStudentsTable()
{
DataTable students = new DataTable();

DataColumn ID = students.Columns.Add("ID", typeof(int));
ID.AutoIncrement = true;

DataColumn firstName = students.Columns.Add("FirstName", typeof(string));
DataColumn lastName = students.Columns.Add("LastName", typeof(string));
DataColumn email = students.Columns.Add("Email", typeof(string));


return students;
}

为什么,哦,为什么RowDeleting事件的EventArgs中没有传递键?我需要从触发此事件时保持 session 状态的 ADO.NET DataTable 中删除记录。

为什么这不起作用? DataKeys 仅在使用 DataSource 控件时才起作用吗?

最佳答案

这有效:

private DataTable MakeStudentsTable()
{
DataTable students = new DataTable();

DataColumn ID = students.Columns.Add("ID", typeof(int));
ID.AutoIncrement = true;

DataColumn firstName = students.Columns.Add("FirstName", typeof(string));
DataColumn lastName = students.Columns.Add("LastName", typeof(string));
DataColumn email = students.Columns.Add("Email", typeof(string));

DataRow student = students.NewRow();
student["FirstName"] = "foo";
student["LastName"] = "bar";
student["Email"] = "foo@bar.com";
students.Rows.Add(student);

return students;
}

protected void gvwStudents_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string id = this.gvwStudents.DataKeys[e.RowIndex].Value.ToString();
}

关于ASP.NET - Gridview RowDeleting 事件上没有 Datakey!,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/583796/

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