GridView中的多个DataKeyNames [英] Multiple DataKeyNames in a GridView

查看:85
本文介绍了GridView中的多个DataKeyNames的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从一个ObjectDataSource中填充了一个GridView,其DataKeyNames字段中有两个项目。一个是主键ID,另一个是类别字段(类别字段用于添加标题行来划分类别)。

I have a GridView populated from an ObjectDataSource with two items in its DataKeyNames field. One is the primary key, ID, the other is a category field (the category field is used to add header rows to delineate categories).

显示工作正常,但我试图创建一个删除操作。对象的删除方法只需要ID字段和ObjectDataSource,即使我将该方法定义为只需要一个ID字段,.net会抱怨,因为它正在寻找一种方法,它具有在DataKeyNames中定义的两个字段。

Displaying works fine, but I'm trying to create a Delete action. The object's delete method only needs the ID field and in the ObjectDataSource even if I define the method as only needing an ID field, .net complains because it is looking for a method which has both the fields defined in DataKeyNames.

它适用于将类别参数添加到delete方法的情况,但令人讨厌的是定义了一个不用于任何内容的参数。

It works if I add a parameter for the category to the delete method, but it's annoying to have a parameter defined that isn't used for anything.

我可以将ObjectDataSource和GridView对象配置为DataKeyNames有两个值,但具体哪个应该传递给哪些方法?

Can I configure the ObjectDataSource and GridView objects to have two values for DataKeyNames but specific which would should be passed to which methods?

这两个对象的定义如下:

The (simplified) definitions for the two objects are:

<asp:ObjectDataSource ID="ObjDS1" runat="server" SelectMethod="getAllItems" 
    TypeName="Items" DeleteMethod="deleteItem">
    <DeleteParameters>
        <asp:Parameter Name="ID" Type="Int32" />
       <!-- This shouldn't be necessary: -->
        <asp:Parameter Name="Category" Type="String" />
    </DeleteParameters>
</asp:ObjectDataSource>

<asp:GridView ID="gvJItems" runat="server" AutoGenerateColumns="False" DataKeyNames="ID,Category" 
    DataSourceID="ObjDS1">
        <Columns>
            <asp:BoundField DataField="ID" Visible="false"  HeaderText="ID" />
            <asp:BoundField DataField="Name" HeaderText="Name" ItemStyle-Width="85%"/>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:LinkButton ID="lbDelete" Runat="server" 
                        OnClientClick="return confirm('Are you sure you want to delete this?');"
                            CommandName="Delete">Delete</asp:LinkButton>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>


推荐答案

所有DataKey值将始终传递给删除方法,因为由 DataKeyNames 旨在成为在GridView中唯一标识该项目的字段或字段。它们通常用于不止这些,以便在ViewState中以每行为基础轻松维护额外的字段,但这样做会带来您在ObjectDataSource方法中看到的副作用。

All of the DataKey values will always be passed to the Delete method because the fields named by DataKeyNames are intended to be the field or fields that uniquely identify that item in the GridView. They're often used for more than that to easily maintain extra fields on a per-row basis in ViewState as you are here, but doing so has the side-effect that you're seeing with the ObjectDataSource methods.

要回答您的原始问题,请不要将类别放在DataKeyNames中,而不要传递给Delete方法。不过,您可以通过将值置于模板列中的隐藏输入中来维护ViewState中的值。不过,除了忽略Delete方法的参数外,获取该值的工作量会更多。

To answer your original question, no, you can't have Category be in DataKeyNames without having it be passed to the Delete method. You could maintain the value in ViewState by putting the value in a hidden input in your Template Column, though. It would be more work to get that value out than to ignore the parameter to the Delete method, though.

这篇关于GridView中的多个DataKeyNames的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆