流程:当点击购物车页面中的进入收银台==》进入此页面,在此页面中填写表单,此时表单内容还不存储到数据库中,而是把表单内容传递给订单确认页面==》把order_modify.aspx页面的表单值传递给==》order_confirm.aspx页面,在此页面中点击完成的时候在把order_modify.aspx页面表单内容和商品信息存储到数据库中。
【一】: order_modify.aspx页面
确认订单按钮
知识点:页面之间的值传递:利用Session进行页面的多值传递:
在此处,Session中存放订单的实体类Model.Order,这样,Session中就存放了多个值,然后利用Session进行页面值传递。
//确认订单
protected void btnOrder_Click(object sender, EventArgs e)
{
string name = txtname.Text.Trim();
string sex = ddlsex.SelectedValue;
string address = txtaddress.Text.Trim();
string email = txtemail.Text.Trim();
string postcode = txtpostcode.Text.Trim();
string phone = txtphone.Text.Trim();
string sendtype = radsendtype.SelectedValue;
string paytype = radpaytype.SelectedValue;
if (name.Length==0||sex.Length==0||address.Length==0||email.Length==0||postcode.Length==0||phone.Length==0||sendtype.Length==0||paytype.Length==0)
{
Page.ClientScript.RegisterStartupScript(Page.GetType(), "MsgBox", "<script>alert('请把信息填写完整!')</script>");
return;
}
//把表单中的值放在order中,然后把order放在Session中,页面间值传递,存取方便
MyShop.Model.Order order = new MyShop.Model.Order() {
recname=name,
sex=sex,
address=address,
email=email,
postcode=postcode,
phone=phone,
sendtype=sendtype,
paytype=paytype
};
Session["order"] = order;//利用Session存储order
Response.Redirect("order_confirm.aspx");
}
【二】:order_confirm.aspx页面
(1):当进入此页面时候,在此页面中取出上个页面中的Session内容,并显示出来
//显示传入的Session内容
if (Session["order"]!=null)
{
MyShop.Model.Order order = Session["order"] as MyShop.Model.Order;//把Session转换为Model类型
litname.Text = order.recname + "["+order.sex+"]";
litpostcode.Text = order.postcode;
litaddress.Text = order.address;
litphone.Text = order.phone;
litemail.Text = order.email;
litsendtype.Text = order.sendtype;
litpaytype.Text = order.paytype;
litsendmoney.Text = GetSendMoney(order.sendtype).ToString("c2");
litAllmoney.Text = (GetSendMoney(order.sendtype) + sc.GetTotlePrice()).ToString("c2");
}
else
{
Response.Write("订单信息为空,请重新购买!");
Response.End();
return;
}
//计算送货费用
private decimal GetSendMoney(string p)
{
int sendmoney = 0;
if (p == "特快专递(EMS)")
{
sendmoney = 150;
}
else if (p=="普通平邮")
{
sendmoney = 80;
}
return sendmoney;
}
(2):订单完成:把上个页面传递过来的Session内容插入到数据库中:
//订单完成
protected void btnOk_Click(object sender, EventArgs e)
{
//购物车为空
if (Session["shopcart"] == null)//如果Session["shopcart"]不存在
{
Session["shopcart"] = new Model.ShopCart();
}
Model.ShopCart sc = Session["shopcart"] as Model.ShopCart;
if (sc.GetItemCount()==0)
{
Page.ClientScript.RegisterStartupScript(Page.GetType(), "MsgBox", "<script>alert('购物车为空!')</script>");
return;
}
//订单信息为空
if (Session["order"] == null)
{
Page.ClientScript.RegisterStartupScript(Page.GetType(), "MsgBox", "<script>alert('订单信息为空!')</script>");
return;
}
MyShop.Model.Order order = Session["order"] as MyShop.Model.Order;//把Session转换为Model类型
order.fp = chkfp.Checked ? 1 : 0;
order.detailsmoney = sc.GetTotlePrice();
order.sendmoney = GetSendMoney(order.sendtype);
order.username = User.Identity.Name;
string bh = DateTime.Now.ToString("yyyyMMdd") + sc.GetTotlePrice();
order.orderbh=bh;//订单编号:当前日前+商品总价格
order.remark = txtremark.Text.Trim();
int orderid = new MyShop.DAL.OrderDAO().Add(order);
if (orderid > 0)
{
//d订单插入成功,可以插入订单明细orderdetails
MyShop.DAL.OrderdetailsDAO orderdetail = new MyShop.DAL.OrderdetailsDAO();
foreach (Model.ShopItem item in sc.GetItemList())
{
orderdetail.Add(new MyShop.Model.Orderdetails() {
createDate=DateTime.Now,
orderid=orderid,
price=item.Price,
proid=item.Proid,
quantity=item.Quantity
});
}
Page.ClientScript.RegisterStartupScript(Page.GetType(), "MsgBox", "<script>alert('下单成功!');location.href='order_ok.aspx?orderbh="+bh+"'</script>");
Session["order"] = null;//订单清空
Session["shopcart"] =null;//购物车清空
}
else
{
Page.ClientScript.RegisterStartupScript(Page.GetType(), "MsgBox", "<script>alert('订单添加失败,请联系管理员!')</script>");
return;
}
}
【三】:order_ok.aspx页面:把上一级页面order_confirm.aspx页面传入的订单编号显示出来
if (!IsPostBack)
{
string orderbh=Request.QueryString["orderbh"];
if (!string.IsNullOrEmpty(orderbh))
{
litorderbh.Text = orderbh;//显示订单编号
}
}