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

试图把我存储的内容添加到购物车中,然后将其转移到另一个页面,以便让GridView显示我添加到购物车会话中的所有商品。 将其存储为对象会话。 -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中,这样我就可以在另一个页面的网格视图中显示它。

更正了代码中的问题。

 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; } }