【问题标题】:Inheriting from DataSourceControl does not produce an IDataSource从 DataSourceControl 继承不会产生 IDataSource
【发布时间】:2011-01-17 02:46:57
【问题描述】:

我正在尝试创建自定义数据源控件。

我一直在关注this article 的信(我认为...)。

我有我的数据源的骨架/基本实现,但是当我在标记中声明它并尝试将其静态绑定到 gridview 时,我收到以下错误:

“grdVw”的 DataSourceID 必须是 IDataSource 类型的控件的 ID

这对我来说似乎非常奇怪,因为我的数据源继承自 DataSourceControl,而后者又实现了 IDataSource。即使我在自定义数据源中显式实现 IDataSource,也没有什么区别。

我的标记是:

<DataBrokerDataSource  ID="objSrcDBroker" runat="server" />

<div>
    <asp:GridView ID="grdVw" DataSourceID="objSrcDBroker" DataMember="Table0" runat="server">
    </asp:GridView>
</div>

<div>
    <asp:GridView id="grdVw2" DataSourceID="objSrcDBroker" DataMember="Table1" runat="server">
    </asp:GridView>
</div>

而我的控制是:

Public Class DataBrokerDataSource
    Inherits DataSourceControl
    Implements IDataSource  'Have tried with this statement included AND excluded = same result

    Protected Overrides Function GetView(ByVal viewName As String) As System.Web.UI.DataSourceView Implements IDataSource.GetView
        'Code here
    End Function

    Protected Overrides Function GetViewNames() As System.Collections.ICollection Implements IDataSource.GetViewNames
        'Code here
    End Function

End Class

任何帮助或建议将不胜感激。

继续...

查看堆栈跟踪表明错误源自: System.Web.UI.WebControls.DataBoundControl.GetDataSource()。

我已经在反射器中检查了这个方法(见下文),看着这个(基于我得到的错误消息)在我看来,好像 FindControl 部分正在成功,但是source = control as IDataSource;source 保留为空值,即转换失败 - 但为什么?

protected virtual IDataSource GetDataSource()
{
    if ((!base.DesignMode && this._currentDataSourceValid) && (this._currentDataSource != null))
    {
        return this._currentDataSource;
    }
    IDataSource source = null;
    string dataSourceID = this.DataSourceID;
    if (dataSourceID.Length != 0)
    {
        Control control = DataBoundControlHelper.FindControl(this, dataSourceID);
        if (control == null)
        {
            throw new HttpException(SR.GetString("DataControl_DataSourceDoesntExist", new object[] { this.ID, dataSourceID }));
        }
        source = control as IDataSource;
        if (source == null)
        {
            throw new HttpException(SR.GetString("DataControl_DataSourceIDMustBeDataControl", new object[] { this.ID, dataSourceID }));
        }
    }
    return source;
}

【问题讨论】:

  • 什么是 DataBrokerDataSource?您的数据源是 CustomDataSource
  • 啊! - 我修改了帖子的代码以试图清晰导致意外混淆
  • 原谅我的无知,但我从未见过没有标签前缀的控件标签。
  • 太棒了!非常感谢 Epitka :-) 我将数据源代码移动到不同的程序集并注册了程序集,这解决了问题。请发表您的评论作为答案,以便我将其标记为正确答案。再次感谢

标签: asp.net vb.net inheritance datasource .net-datasourcecontrol


【解决方案1】:

谢谢epitka,问题是页面上没有正确注册控件引起的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-04-02
    • 1970-01-01
    • 1970-01-01
    • 2018-09-28
    • 1970-01-01
    • 1970-01-01
    • 2014-01-31
    • 2023-03-18
    相关资源
    最近更新 更多