在购物车的会话状态中存储数据

时间:2015-05-02 23:26:09

标签: c# asp.net entity-framework code-first shopping-cart

尝试将我存储的内容添加到购物车中 然后将其转移到另一个页面,以使GridView显示我已添加到Cart会话中的所有项目。将其存储为对象会话。 -AddToCart在A Session中获取行详细信息和存储,然后获取该会话对象并将其显示在另一页的Grid视图中。

从中获取此代码:

 protected void GridViewDisplay_RowCommand(object sender,
 GridViewCommandEventArgs e)
 {
 if (e.CommandName == "AddToCart")
 {
 object[] values;
        DataTable orderTable;
        // Retrieve the row index stored in the 
        // CommandArgument property.
        int index = Convert.ToInt32(e.CommandArgument);

        // Retrieve the row that contains the button 
        // from the Rows collection.
        GridViewRow row = GridViewDisplay.Rows[index];
        values = new Object[GridViewDisplay.Rows[0].Cells.Count];
        for (int i = 0; i < GridViewDisplay.Rows[0].Cells.Count; i++)
        {
            values[i] = GridViewDisplay.Rows[0].Cells[i].Text;
        }

       orderTable = (DataTable)Session["OrderLine"];
       orderTable.Rows.Add(values);
       Session["OrderLine"] = orderTable;


    }

}

然后现在我尝试将其存储在一个Session中,这样我就可以在另一个页面的网格视图中显示它。

1 个答案:

答案 0 :(得分:0)

更正了代码中的问题。

protected void GridViewDisplay_RowCommand(object sender,
 GridViewCommandEventArgs e)
 {
 if (e.CommandName == "AddToCart")
 {
 object[] values;
        DataTable orderTable;
        // Retrieve the row index stored in the 
        // CommandArgument property.
        int index = Convert.ToInt32(e.CommandArgument);

        // Retrieve the row that contains the button 
        // from the Rows collection.
        GridViewRow row = GridViewDisplay.Rows[index];
        values = new Object[GridViewDisplay.Rows[0].Cells.Count];
        for (int i = 0; i < GridViewDisplay.Rows[0].Cells.Count; i++)
        {
            values[i] = row.Cells[i].Text;
        }

       orderTable = (DataTable)Session["OrderLine"];
       orderTable.Rows.Add(values);
       Session["OrderLine"] = orderTable;


    }

}