我有这个ASP.NET代码:
<asp:DropDownList
ID="ddlBrokers"
runat="server"
AutoPostBack="true"
DataSourceID="srcBrokers"
DataTextField="broker"
DataValueField="brokerId"
/>
<asp:ObjectDataSource
id="srcBrokers"
TypeName="DatabaseComponent.DBUtil"
SelectMethod="GetBrokers"
runat="server">
</asp:ObjectDataSource>
我的DAL代码:
public DataTable GetBrokers(bool? hasImport=null)
{
SqlCommand cmd = new SqlCommand("usp_GetBrokers");
if (hasImport.HasValue)
cmd.Parameters.AddWithValue("@hasImport", hasImport);
return FillDataTable(cmd, "brokers");
}
当表单加载时,我收到此错误:
ObjectDataSource 'srcBrokers' could not find a non-generic method 'GetBrokers' that has no parameters.
是我的可选参数导致问题吗?我该如何解决这个问题?是否可以使用声明性ASP.NET代码获取可选参数?
答案 0 :(得分:3)
添加方法:
public DataTable GetBrokers() {
return GetBrokers(null);
}
并检查它是否有效?
答案 1 :(得分:0)
您可以像这样使用SelectParameters
:
<asp:ObjectDataSource id="srcBrokers" TypeName="DatabaseComponent.DBUtil" SelectMethod="GetBrokers" runat="server">
<SelectParameters>
<asp:Parameter Name="hasImport" Type="Empty" />
<SelectParameters>
</asp:ObjectDataSource>