【问题标题】:ASP.NET GridView Custom Binding ErrorASP.NET GridView 自定义绑定错误
【发布时间】:2011-09-09 02:27:06
【问题描述】:

我的任务是为我们的产品设计一个 ORM + 对象模型。它主要用于 Windows + WinForms 开发,但现在也需要与我们的 ASP.NET 产品一起使用。

但是,将 Collection 对象绑定到 GridView 似乎存在问题(我怀疑其他 ASP.NET 数据源控件)。

在线开发团队收到以下错误

[HttpException (0x80004005): The IListSource does not contain a data source named ''. Check your DataMember value.]

我们使用的集合对象使用“视图”集合来处理排序/过滤(类似于 DataView 与 DataTable 的关系),这是 IListSource 实现返回的“列表”。但由于某种原因,ASP.NET 控件对此不满意并引发异常。

如果我在集合上使用 Linq 扩展方法“ToList”,它会成功绑定 - 但将列表转换为列表似乎毫无意义,因为 GridView 绑定到也实现 IListSource 的 DataTables,它一定是我做过的事情对象模型错误。

但我有点不知道在哪里看,似乎大多数谷歌示例都显示绑定到数据集,所以不是特别有用。

一如既往,欢迎任何见解。

编辑


AlumniShopItems 的类型为:

DataObjectCollection<AlumniShopItem>

它是以下的子类:

DataObjectCollection<T> : BindingList<T>, IListSource, IList<T>, IDataObjectCollection, IFillableList where T : DataObject

IListSource 返回以下类型列表:ObjectListView<T>

哪个实习生实现了以下接口:

IBindingListView, IList<T>, ITypedList, IRaiseItemChangedEvents, ICancelAddNew, IDeserializationCallback


谢谢

马龙

下面是他们使用的代码:

 <asp:GridView ID="ShopGrid" runat="server" AutoGenerateColumns="False" AutoGenerateEditButton="True"
                        OnRowEditing="ShopGrid_RowEditing" 
              OnRowUpdating="ShopGrid_RowUpdating" OnRowCancelingEdit="CancelEdit" 
              OnRowDataBound="RowBinding" >
                        <Columns>
                            <asp:TemplateField HeaderText="ID" Visible="false">
                                <EditItemTemplate>
                                     <asp:Label ID="Label1" runat="server" Text='<%#Bind("Ident")%>'></asp:Label>
                                </EditItemTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="Label1" runat="server" Text='<%#Bind("Ident")%>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                              <asp:TemplateField HeaderText="Item Name" >
                                <EditItemTemplate>
                                     <asp:TextBox ID="NameText" runat="server" Text='<%#Bind("Name")%>'></asp:TextBox>
                                </EditItemTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="NameLabel" runat="server" Text='<%#Bind("Name")%>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Pay Pal ID" >
                                <EditItemTemplate>
                                     <asp:TextBox ID="PayPalText" runat="server" Text='<%#Bind("PayPalID")%>'></asp:TextBox>
                                </EditItemTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="PayPalLabel" runat="server" Text='<%#Bind("PayPalID")%>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Price" >
                                <EditItemTemplate>
                                     <asp:TextBox ID="PhotoPath" runat="server" Text="" > </asp:TextBox>
                                     &nbsp;&nbsp;&nbsp;
                                     <asp:Button ID="Upload" runat="server" Text="Upload" />
                                </EditItemTemplate>
                                <ItemTemplate>
                                    <asp:Image ID="itemPhoto" runat="server" />
                                </ItemTemplate>
                            </asp:TemplateField>
                              <asp:TemplateField HeaderText="Price" >
                                <EditItemTemplate>
                                      <asp:CheckBox ID="CheckBox1" runat="server" Checked='<%#Bind("Current")%>' Enabled="true" />
                                </EditItemTemplate>
                                <ItemTemplate>
                                    <asp:CheckBox ID="CheckBox1" runat="server" Checked='<%#Bind("Current")%>' Enabled="false" />
                                </ItemTemplate>
                            </asp:TemplateField>
                        </Columns>
                    </asp:GridView>

页面加载中的某处:

Line 57:                 alumniShopItems.FillMe();
Line 58:                 ShopGrid.DataSource = alumniShopItems;
Line 59:                 ShopGrid.DataBind();

【问题讨论】:

  • “FillMe()”内部发生了什么?
  • 您的数据源是否将 Null 值作为空字符串返回?
  • alumniShopItems 的类型是什么?及其基类和接口(如果有)
  • "FillMe()" 进入数据库,对于它找到的每一行,它都会填充一个对象并将其添加到集合中。至于返回空值,我不完全确定你的意思,但不,它们会为空。
  • alumniShopItems 的类型信息现在包含在编辑中。

标签: .net asp.net data-binding gridview


【解决方案1】:

我已经对此进行了排序,所以我将把答案放在这里,以防其他人遇到这个问题。

这是因为 IListSource.ContainsListCollection 属性返回 true (我的错,我误解了它试图做什么),改变了它所以它返回 false 排序它。

奇怪的是,WinForms 绑定行为像这样工作得很好,这就是为什么它以前不是问题的原因,更改它使其返回 false 也不会影响它在 Windows 上的工作。

我感到困惑的原因是最初我认为 ContainsListCollection 属性通知控件该列表有另一个它希望它使用的列表。现在我可以看到这个功能是通过实现接口来实现的(我同意这很有意义),并且属性指定返回的 IList 中是否有一个子集合并使用它。

我只能说感谢上帝对 .NET 源代码的支持!尽管因为我安装了 Windows 7 SP1 而不得不在 VM 中进行操作...

【讨论】:

    猜你喜欢
    • 2018-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-22
    • 2012-06-07
    • 1970-01-01
    • 2019-05-10
    • 1970-01-01
    相关资源
    最近更新 更多