首页 新闻 赞助 找找看

如何将用session取出来并存在一张临时表中的数据取出来

0
悬赏园豆:10 [待解决问题]

public partial class ShopCar : System.Web.UI.Page
{ private Int32 Total = 0;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindCartList();
}
}
private void BindCartList()
{
DataTable dt = new DataTable();
if (Session["Cart"] != null)
{
dt = (DataTable)Session["Cart"];
}
else
{
dt.Columns.Add(new DataColumn("Goods_Id", typeof(Int32)));
dt.Columns.Add(new DataColumn("Type_Id", typeof(String)));
dt.Columns.Add(new DataColumn("Goods_Name", typeof(String)));
dt.Columns.Add(new DataColumn("Goods_Price", typeof(float)));
dt.Columns.Add(new DataColumn("Cart_Num", typeof(Int32)));
}
if (Goods_Id!= null)
{
Boolean IsExist = false;
foreach (DataRow dr in dt.Rows)
{
if (dr["Type_Id"].ToString() == Type_Id)
{
IsExist = true;
break;
}
}
if (IsExist)
{
ScriptManager.RegisterStartupScript(
this, typeof(Page), "alertExist", "alert('您选择的商品(编号:" + Type_Id + ")已在购物车!')", true);
}
else
{
SqlHelper helper = new SqlHelper();
DataTable dtRow = helper.FillDataTable(
String.Format("Select * From tb_Goods Where Type_Id={0}", Type_Id.ToString()));
dt.Rows.Add(new object[]{
Convert.ToInt32(Goods_Id.ToString()),
dtRow.Rows[0]["Type_Id"].ToString(),
dtRow.Rows[0]["Goods_Name"].ToString(),
Convert.ToInt32(dtRow.Rows[0]["Goods_Price"].ToString()),
1});
}
}
gvCart.DataSource = dt;
gvCart.DataBind();
Session["Cart"] = dt;
}
protected void gvCart_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onmouseover", "b=this.style.backgroundColor;this.style.backgroundColor='#E1ECEE'");
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=b");
TextBox tb = (TextBox)e.Row.FindControl("text");
((HtmlImage)e.Row.FindControl("imgjian")).Attributes.Add("onclick", "jian(" + tb.ClientID + ")");
((HtmlImage)e.Row.FindControl("imgjia")).Attributes.Add("onclick", "jia(" + tb.ClientID + ")");
DataRowView drv = (DataRowView)e.Row.DataItem;
Total += Int32.Parse(drv["Goods_Price"].ToString()) * Int32.Parse(tb.Text);
}
if (e.Row.RowType == DataControlRowType.Footer)
{
e.Row.Cells[1].Text = "金额总计:";
e.Row.Cells[1].HorizontalAlign = HorizontalAlign.Right;
e.Row.Cells[2].Text = Total.ToString("c2");
e.Row.Cells[2].ForeColor = Color.Red;
}
}
protected void gvCart_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
if (Session["Cart"] != null)
{
DataTable dt = (DataTable)Session["Cart"];
dt.Rows.RemoveAt(e.RowIndex);
dt.AcceptChanges();
Session["Cart"] = dt;
Response.Redirect("ShopCar.aspx");
}
}
protected void imgbtnTotal_Click(object sender, ImageClickEventArgs e)
{
if (Session["Cart"] != null)
{
DataTable dt = (DataTable)Session["Cart"];
for (int i = 0; i < gvCart.Rows.Count; i++)
{
dt.Rows[i]["Cart_Num"] = ((TextBox)gvCart.Rows[i].FindControl("Text")).Text;
}
dt.AcceptChanges();
Session["Cart"] = dt;
Response.Redirect("ShopCar.aspx");
}
}
private Int32? Goods_Id

{
get
{
try
{
return Int32.Parse(Request.QueryString["Goods_Id"]);
}
catch
{
return null;
}
}
}
private String Type_Id
{
get
{
return Request.QueryString["Type_Id"];
}
}
我在做购物车时用session取值,但是做订单那块的时候我不会把虚拟表中的数据取出来,该怎么办呢?求大神指点我一下。上面的代码是购物车后台代码
}

< >
分享
所有回答(1)
0

你这张虚拟表,是存储在数据库的实体表吗?

ludi | 园豆:5 (初学一级) | 2015-10-28 16:37

不是实体表,没有插入到数据库中,所以导致后面的部分不会做。不会取值

支持(0) 反对(0) 杨杨昵 | 园豆:93 (初学一级) | 2015-10-28 20:22

@杨杨昵: 那这逻辑就有问题了,你要嘛数据库建一个购物车临时表,要嘛把购物车中信息存储在cookie中,只存储在session中生命周期是很短的

支持(0) 反对(0) ludi | 园豆:5 (初学一级) | 2015-10-28 20:29

@ludi你看上面的代码嘛!我可能是表达错误,但是我想要把值给取出来,做订单部分,可以怎么做呢

支持(0) 反对(0) 杨杨昵 | 园豆:93 (初学一级) | 2015-10-28 20:33
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册