DataList 不显示数据
DataList not displaying data
所以我有一个数据源和一个数据列表:
<asp:SqlDataSource ID="SearchDataSource" runat="server"
ConnectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|WheelsDB.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();
然而,当数据列表应该显示一些行时,btn_search.Text 显示 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>
所以我有一个数据源和一个数据列表:
<asp:SqlDataSource ID="SearchDataSource" runat="server"
ConnectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|WheelsDB.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();
然而,当数据列表应该显示一些行时,btn_search.Text 显示 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>