1

gridview 控件使复杂对象的数据绑定相当容易。在我的场景中,gridview 控件绑定到Customer对象,该对象具有一些“平面”属性和一个复杂属性,该属性采用Address类型的对象。网格按预期显示数据。问题是我发现无法在后面的代码中访问 Address 属性的值。例如,将 DataKeyNames 集合设置为DataKeyNames="Id, Address.Id "会导致错误:

DataBinding:System.Data.Entity.DynamicProxies.Customer_95531162E60920A5C3C02043F6564873913B91785C856624301E8B6E89906BF6 不包含名称为 Address.Id 的属性。

在后面的代码中访问 Address.Id 字段值的正确方法是什么?理想情况下,我想做类似的事情:

    protected void CustomerDetailsObjectDataSource_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
{
    if (CustomersGridView.SelectedIndex < 0) return;        

    // Retrieving the Customer's id works:
    e.InputParameters["id"] = Convert.ToString(CustomersGridView.DataKeys[CustomersGridView.SelectedIndex].Value);        

    // Retrieving the Address id doesn work:
    e.InputParameters["id"] = Convert.ToString(CustomersGridView.DataKeys[CustomersGridView.SelectedIndex].Values["Address.Id"].ToString());        
}

这是asp代码:

<ContentTemplate>

        <asp:GridView ID="CustomersGridView" runat="server" AutoGenerateColumns="False" DataSourceID="CustomersObjectDataSource"
                      onselectedindexchanged="CustomersGridView_SelectedIndexChanged" DataKeyNames="Id,Address.Id" ondatabound="CustomersGridView_DataBound">
            <Columns>
                <asp:TemplateField HeaderText="Aktion">
                    <ItemTemplate>
                        <asp:LinkButton runat="server" ID="SelectCustomerButton" Text="Auswählen" CommandName="Select" /> <br/>                         
                    </ItemTemplate>
                </asp:TemplateField>

                <asp:TemplateField HeaderText="Kunde" SortExpression="LastName" ItemStyle-VerticalAlign="Top" >
                    <ItemTemplate>
                        <asp:Label ID="NumberLabel" runat="server" Text='<%#"GpNr: " + Eval("Number")%>'></asp:Label><br/>
                        <asp:Label ID="SalutationLabel" runat="server" Text='<%#Eval("Salutation")%>'></asp:Label>
                        <asp:Label ID="TitleLabel" runat="server" Text='<%#Eval("Title")%>'></asp:Label>
                        <asp:Label ID="FirstNameLabel" runat="server" Text='<%#Eval("FirstName")%>'></asp:Label>
                        <asp:Label ID="LastNameLabel" runat="server" Text='<%#Eval("LastName")%>'></asp:Label><br/>
                        <asp:Label ID="NameContactPersonLabel" runat="server" Text='<%#"Kontakt: " + Eval("NameContactPerson")%>'></asp:Label>
                    </ItemTemplate>
                    <ItemStyle VerticalAlign="Top" />
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Adresse" SortExpression="Address.PostalCode" ItemStyle-VerticalAlign="Top" >
                    <ItemTemplate>                            
                        <asp:Label ID="AddressIdLabel" runat="server" Text = '<%#Eval("Address.Id") %>'></asp:Label>
                        <asp:Label ID="AddressStreetLabel" runat="server" Text='<%#Eval("Address.Street")%>'></asp:Label>
                        <asp:Label ID="AddressHouseNumberLabel" runat="server" Text='<%#Eval("Address.HouseNumber")%>'></asp:Label>
                        <asp:Label ID="AddressHouseNumberExtensionLabel" runat="server" Text='<%#Eval("Address.HouseNumberExtension")%>'></asp:Label>
                        <asp:Label ID="AddressDoorNumberLabel" runat="server" Text='<%#Eval("Address.DoorNumber")%>'></asp:Label><br/>
                        <asp:Label ID="AddressPostalCodeLabel" runat="server" Text='<%#Eval("Address.PostalCode")%>'></asp:Label>
                        <asp:Label ID="AddressCityLabel" runat="server" Text='<%#Eval("Address.City")%>'></asp:Label><br/>
                        <asp:Label ID="AddressCountryLabel" runat="server" Text='<%#Eval("Address.Country")%>'></asp:Label>
                    </ItemTemplate>

谢谢!!

4

1 回答 1

0

原因是 Gridview 将 datakeynames 视为属性名称,它在 CreateChildControls() 期间使用 DataBinder.GetPropertyValue() 来检索数据键值,而 DataBinder.Eval 支持多级属性(只要用点字符分隔即可)

例子:

object item = ...
DataBinder.Eval(item, "Address.Id"); //no problem
DataBinder.GetPropertyValue(item, "Address.Id"); //will throw exception

有几种解决方案可以解决您的问题:

  1. 派生您的自定义网格视图(覆盖 CreateChildControls()),它的数据键名支持多层表达式。您可以使用 Reflector 来引用默认实现。(很多工作)
  2. 在 RowDataBound 事件期间将复杂的属性值存储到视图状态中(首选,我也使用 :))

例子:

private void CustomersGridView_RowDataBound(object sender, EventArgs args)
{
    if(e.Row.RowType = DataControlRowType.DataRow)
    {
        object id = DataBinder.Eval(e.Row.DataItem, "Id");

        //DictionaryInViewState is a variable that will be stored into viewstate later
        DictionaryInViewState[id] = DataBinder.Eval(e.Row.DataItem, "Address.Id");
    }
}

参考:

http://www.telerik.com/forums/problem-with-a-complex-datakeynames-value

于 2014-02-05T09:31:06.840 回答