【问题标题】:ObjectDataSource -> SelectMethod is not specified未指定 ObjectDataSource -> SelectMethod
【发布时间】:2010-02-16 09:14:21
【问题描述】:

我用 ObjectDataSource + ASPxGridView 创建了一个 UserControl。 ObjectDataSource 的 SelectMethod 我根据 UserControl 的公共参数动态设置:

private int _companyID = -1;
public int CompanyID
{
    get { return _companyID; }
    set
    {
        _companyID = value;

        dsPersons.SelectMethod = "GetPersonsByCompany";
        dsPersons.SortParameterName = "sort";
        dsPersons.SelectParameters.Clear();
        dsPersons.SelectParameters.Add("companyID", DbType.Int32, value.ToString());
    }
}

当我像这样在 Page 上使用我的控件时:

<uc:PersonsManager ID="personsManager" runat="server" CompanyID="2" />

或者像这样放在另一个 ASPxGridView 中:

<dxwgv:ASPxGridView ID="gridViewCompany" runat="server" DataSourceID="dsCompany" KeyFieldName="ID" Width="100%"
    AutoGenerateColumns="false">
    <Columns>
        <dxwgv:GridViewDataColumn>
            <DataItemTemplate>
                <uc:PersonsManager ID="personsManager" runat="server" CompanyID='<%# Bind("ID") %>' />
            </DataItemTemplate>
        </dxwgv:GridViewDataColumn>
    </Columns>
</dxwgv:ASPxGridView>

一切正常,但我想从代码隐藏中声明 Control 参数

<uc:PersonsManager ID="personsManager" runat="server" />

然后在 Page_Load 上:

personsManager.CompanyID = 2;

如果我使用之前的声明,那么我只能看到 UserControl 的 ASPxGridView 第一次加载一些数据,因为任何操作都会导致 UserControl 的 ASPxGridView 的 HtmlRowCreated 事件发生错误:

不支持选择操作 通过 ObjectDataSource 'dsPersons' 除非 指定了 SelectMethod。

为什么会这样?

谢谢。

【问题讨论】:

    标签: asp.net aspxgridview


    【解决方案1】:

    当您添加对象数据源时,您会看到一个向导,让您可以选择对象上支持数据库典型 CRUD 方法的方法。

    点击你的objectDataSource的smarttag并选择'configure datasource',你会看到。

    那么看看http://msdn.microsoft.com/en-us/library/9a4kyhcx.aspx 了解如何实现这些方法..

    祝你好运。

    【讨论】:

    • 您没有正确理解这种情况:当我将 Control 参数设置为内联或在其他 gridView (Bind) 中时,我的方法工作正常,但当我从代码隐藏 ( C#)。我认为 ASPxGridView 的问题和它的回调,为什么 Page_Load 被忽略?
    • 我有点困惑,绑定是在页面加载之后发生的。参数仍然在标记中设置,对吧?你想覆盖它们吗?您可能必须处理 onselect 或 selection 或其他任何方法并自己构建它以解决该问题。再一次,我模糊地记得,但我知道我去过那里不止一次......
    • 嗯...我在页面上有一个自定义控件(没有别的)。 UserControl 有一个 gridView 和 dataSource 以及 select 方法,这取决于控件的公共参数。如果我在标记中设置此参数,则一切正常,但如果我想从代码隐藏中动态设置此参数,则在对用户控件的 gridView 中的数据进行任何操作时会出错。
    • 我很茫然。有点累。明天会重新访问,看看我是否对它有不同的看法。祝你好运。
    【解决方案2】:

    请查看 Eran Nachum 的这篇博文 "Manually Set ObjectDataSource.SelectMethod Property BUG"。他描述了您必须如何在 OnInit 事件期间设置网格的数据源,然后在 OnPreRender 期间进行数据绑定。

    (上面引用的网站已经失效了,回程机上有一篇博文:Eran Nachum's Blog from December 2007 in the wayback machine

    【讨论】:

    • 提供的网址已失效:(
    • 是的,不幸的是。幸运的是,它看起来像回程机器复制了一份。我将使用以博客文章开头的页面的 URL 更新答案。
    猜你喜欢
    • 2014-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多