简体   繁体   English

删除具有多个主键的Datalist行

[英]Delete Datalist row with multiple primary keys

I have a datalist with a OnDeleteCommand="Delete_Command". 我有一个带有OnDeleteCommand =“Delete_Command”的数据列表。

I want the delete a record with multiple primary Keys but I do not know how to access it from the Delete_Command event. 我希望删除具有多个主键的记录,但我不知道如何从Delete_Command事件访问它。

If I use DataKeyField I'm limited to only one key. 如果我使用DataKeyField,我只能使用一个密钥。 Any workarounds for this? 有没有解决方法呢?

You can access all of the keys: 您可以访问所有密钥:

gridView.DataKeys[rowNum][dataKeyName]

where rowNum is e.RowIndex from the gridView_RowDeleting event handler, and dataKeyName is the key you want to get: 其中rowNum是来自gridView_RowDeleting事件处理程序的e.RowIndex,而dataKeyName是您想要获取的键:

<asp:GridView ID="gridView" runat="server" DataKeyNames="userid, id1, id2, id3" OnRowDeleting="gridView_RowDeleting">

protected void gridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
    gridView.DataKeys[e.RowIndex]["userid"]...
    gridView.DataKeys[e.RowIndex]["id1"]...
    gridView.DataKeys[e.RowIndex]["id2"]...
    gridView.DataKeys[e.RowIndex]["id3"]...
}

Oh, sorry, I missed it. 哦,对不起,我错过了。

AFAIK there is no such a possibility by default. AFAIK默认没有这种可能性。 Maybe you can create a composite key from your primary keys, like 也许你可以用主键创建一个复合键,比如

Key1UnderscoreKey2UnderscoreKey3 Key1UnderscoreKey2UnderscoreKey3

and split it in the event handler. 并将其拆分在事件处理程序中。 So this is a DIY multi-key handler for DataList :-) 所以这是DataList的DIY多键处理程序:-)

Edit: The underscore got lost during format, it replaces with italic text. 编辑:下划线在格式化过程中丢失,它替换为斜体文本。 So instead of "underscore" word use real underscores 因此,而不是“下划线”一词使用真正的下划线

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM