【发布时间】:2015-02-07 23:21:15
【问题描述】:
我想从asp.net后面的代码中绑定datalist
我从列表中获取产品 ID 并根据它们的基础选择所有产品
以下是我的代码:
List<string> r_items_grid = (List<string>)Session["recent_items"];
for(int i=0; i < r_items_grid.Count; i++)
{
OleDbCommand cmd_r_items= new OleDbCommand("SELECT product_id,product_name,product_price,product_image_1 from products where product_id="+ Convert.ToInt32( r_items_grid[i]),con);
r_items_reader=cmd_r_items.ExecuteReader();
DataList3.DataSource = r_items_reader;
DataList3.DataBind();
}
但我只看到数据列表中的最后一条记录
【问题讨论】:
-
这很明显,因为对于每次迭代,您都在更改 DatasSource (DS) 并重新绑定它,而不是这样做,您应该只分配 DS 一次并且只绑定一次,并且应该在 for 循环之外。
-
另外,小心那个 sql 命令检查你的参数。