2010-02-15 42 views
0

我想创建一个自定义数据源控件。从DataSourceControl继承不产生IDataSource

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

我有我的数据源的骨架/基本实现,但是当我宣布它的标记,并尝试静态绑定到一个gridview,我收到以下错误:

The DataSourceID of 'grdVw' must be the ID of a control of type IDataSource

这看起来非常奇怪对我来说,因为我的数据源从DataSourceControl继承,而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部分成功,但该源=控制为IDataSource;作为空值,即转换失败 - 但为什么?

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; 
} 
+0

什么是DataBrokerDataSource?你的DataSource是CustomDataSource – epitka 2010-02-15 19:05:55

+0

啊! - 我修改了帖子的代码,试图清晰地导致意外模糊 – RobD 2010-02-15 19:07:43

+0

原谅我的无知,但我从来没有看到没有标签前缀的控件的标签。 – epitka 2010-02-15 19:25:49

回答

0

谢谢epitka,问题是由控件未正确注册在页面上造成的。