加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

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

发布时间:2020-12-16 07:20:59 所属栏目:asp.Net 来源:网络整理
导读:我有一个像这样的GridView: asp:GridView ID="gvwStudents" runat="server" AutoGenerateColumns="False" DataKeyNames="ID" ShowHeader="False" onrowdeleting="gvwStudents_RowDeleting" Columns asp:BoundField DataField="FirstName" / asp:BoundField
我有一个像这样的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>

以下是我如何创建GridView绑定的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中没有传递密钥?我需要从ADO.NET DataTable中删除当我触发此事件时保持会话状态的记录.

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

解决方法

这有效:

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

    DataColumn ID = students.Columns.Add("ID",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();   
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读