0

我正在尝试使用带有格式化的产品 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();

            }

        }

    }
}
4

1 回答 1

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();

        }
于 2012-12-06T10:22:58.797 回答