我正在尝试使用带有格式化的产品ID和数量字符串的Cookie。我设置了我进行查询并获取cookie中每个项目的表数据。现在我需要获取数据,将其连接到列表视图。 cookie看起来像:1x4:23x9 ......
public partial class checkout : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
productTableAdapter ad = new productTableAdapter();
HttpCookie c = Request.Cookies["cart"];
if (c == null)
{
}
else
{
string items = c.Value;
new prod(items);
if (items.Length > 0)
{
char[] delimiterChars = {':'};
string[] orders = items.Split(delimiterChars);
char[] del = {'x'};
ObjectDataSource ods = new ObjectDataSource();
foreach( string s in orders){
string[] proc = s.Split(del);
int id = int.Parse(proc[0]);
int quant = int.Parse(proc[1]);
DataSet1.productDataTable td = ad.GetDataByPID(id);
// something..?
}
ListView1.DataSource = ods;
ListView1.DataBind();
}
}
}
}
答案 0 :(得分:0)
为什么不绑定IList
?
public class LVItemModel
{
public int Id { get; set; }
public int Quantity { get; set; }
}
...
string items = c.Value;
if (items.Length > 0)
{
List<LVItemModel> list = new List<LVItemModel>();
string[] orders = items.Split( new char[] { ':' } );
foreach( string s in orders){
string[] proc = s.Split(new char { 'x' });
int id = int.Parse(proc[0]);
int quant = int.Parse(proc[1]);
list.Add( new LVItemModel() { Id = id, Quantity = quant } );
}
ListView1.DataSource = list;
ListView1.DataBind();
}