DataList不显示数据

时间:2015-04-12 14:23:50

标签: c# asp.net datalist

所以我有一个DataSource和一个DataList:

 <asp:SqlDataSource ID="SearchDataSource" runat="server" 
        ConnectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\4WheelsDB.mdb;Persist Security Info=True" 
        ProviderName="System.Data.OleDb">
        </asp:SqlDataSource>
  <asp:DataList ID="DataList1" runat="server" DataSourceID="SearchDataSource"></asp:DataList>

当用户点击按钮时,它会执行此代码,根据用户选择的内容修改查询:

query = "SELECT * FROM Cars WHERE "

if(make != 1)
{
   query = query + "make_id = @make";
   SearchDataSource.SelectParameters.Add("make", make.ToString());
}

SearchDataSource.SelectCommand = query;
btn_search.Text = DataList1.Items.Count.ToString();

然而,当datalist显示某些行时,btn_search.Text显示0并且数据列表中没有显示行,是否有人知道我做错了什么?

1 个答案:

答案 0 :(得分:0)

您应该在查询中添加 where 子句。

<强> C#

query = "SELECT * FROM Cars"

if(make != 1)
{
   query = query + " where make_id = @make"; // add here
   SearchDataSource.SelectParameters.Add("make", make.ToString());
}

SearchDataSource.SelectCommand = query;
btn_search.Text = DataList1.Items.Count.ToString();

<强> ASPX

<asp:DataList ID="DataList1" runat="server" DataSourceID="SearchDataSource">
    <ItemTemplate>
        <asp:Label ID="lblmake_id" runat="server" Text='<%# Eval("make_id")%>' />
    </ItemTemplate>
</asp:DataList>