我想創建一個自定義數據源控件。從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;
}
什麼是DataBrokerDataSource?你的DataSource是CustomDataSource – epitka 2010-02-15 19:05:55
啊! - 我修改了帖子的代碼,試圖清晰地導致意外模糊 – RobD 2010-02-15 19:07:43
原諒我的無知,但我從來沒有看到沒有標籤前綴的控件的標籤。 – epitka 2010-02-15 19:25:49