检索绑定到gridview控件的复杂对象的值

时间:2011-07-24 17:09:32

标签: asp.net gridview nested-class datakey

gridview控件使复杂对象的数据绑定变得相当容易。在我的场景中,gridview控件绑定到 Customer 对象,该对象具有一些'flat'属性和一个带有 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>

谢谢!

1 个答案:

答案 0 :(得分: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. 派生您的自定义gridview(覆盖CreateChildControls()),它的datakeynames支持多层表达式。您可以使用Reflector来引用默认实现。 (很多工作)
  2. 在RowDataBound事件期间将复杂的属性值存储到viewstate中(首选,我也使用它):))
  3. 示例:

    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