1

问候!

我正在创建一个将在 GridView 控件中显示数据的用户控件。我们使用的是 n 层架构,有问题的数据从我们的数据库中检索并作为 ReadOnlyCollection 返回给我们。OurNewObject 是一个包含多个属性和一个不带参数的空构造函数的类 - 它位于以下命名空间中:Acme.ObjectModel。

在用户控件中,我有以下内容:

<asp:GridView ID="ourGrid" runat="server" DataSourceID="ourDataSource">
    <columns>
    <asp:BoundField DataField="Name" HeaderText="Full Name" />
    <asp:BoundField DataField="Gender" HeaderText="Gender" />
    <asp:BoundField DataField="BirthYear" HeaderText="Year of Birth" />
    <asp:BoundField DataField="JoinDate" HeaderText="Date Joined" />
  </columns>
</asp:GridView>
<asp:ObjectDataSource ID="ourDataSource" runat="server" SelectMethod="GetTopUsers" TypeName="Acme.Model.OurNewObject">
</asp:ObjectDataSource>

在后面的用户控件代码中,我有以下公共方法:

public ReadOnlyCollection<OurNewObject> GetTopUsers()
{
    return (OurDataProxy.GetJustTheTopUsers());
}

当我将用户控件放在 Web 窗体上并运行它时,我收到以下消息:

ObjectDataSource 'ourDataSource' 找不到没有参数的非泛型方法 'GetTopUsers'。

所以我的问题是:

  1. 我是否错误地使用了 ObjectDataSource?
  2. 在这种情况下是否有更合适的方法来使用 ObjectDataSource?

谢谢。

4

3 回答 3

2

通常,您将创建一个包含数据访问方法的单独对象,而不是将这些方法放在代码隐藏中。单独的对象可以是实例或静态的,但对象本身必须有一个无参数的构造函数(或根本没有构造函数)。

此外,ObjectDataSource 上的 TypeName 属性应该引用上述单独对象的类型名。例子:

public class SampleDataObject
{
  public ICollection<OurNewObject> GetTopUsers()
  {
    //[...]
  }
}

The attributes mentioned above: [System.ComponentModel.DataObject(true)] at the class level, and [System.ComponentModel.DataObjectMethod(DataObjectMethodType.Select)] at the getter method are not required, but will aid in design-time support by filtering out other types when looking for classes to hook yer ObjectDataSource up to.

于 2008-12-03T02:15:02.650 回答
0

我认为这个问题缺少两个属性。

首先在您的 GetTopUsers() 方法上添加此属性

[System.ComponentModel.DataObjectMethodAttribute
    (System.ComponentModel.DataObjectMethodType.Select, true)]

然后在实际的 OurNewObject 类上添加这个属性

[System.ComponentModel.DataObject]
于 2008-11-19T21:51:18.020 回答
0

尝试将DataKeyNames(添加主键)属性添加到GridView,看看是否有效?

于 2008-12-01T06:09:26.267 回答