📄 browserbook.aspx.cs
字号:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Linq;
using System.Data.Linq;
using System.Collections.Generic;
public partial class DesktopModules_Book_BrowserBook : System.Web.UI.Page
{
protected int nCategoryID = -1;
protected void Page_Load(object sender, EventArgs e)
{
if(Request.Params["CategoryID"] != null)
{
nCategoryID = Int32.Parse(Request.Params["CategoryID"].ToString());
}
if (!Page.IsPostBack)
{
if (nCategoryID > -1)
{
///按照书籍种类显示所有书籍
BindBookData(nCategoryID);
}
}
}
private void BindBookData(int nCategoryID)
{
BookM book = new BookM();
IList<Book> recb = book.GetBookByCategory(nCategoryID);
if(recb == null) return;
gvBook.DataSource = recb;
gvBook.DataBind();
//Book bok = new Book();
//bok.;
}
private OrderItemInformation GetOrderItemInformation(GridViewCommandEventArgs e)
{
///当购物车没有任何信息时
OrderItemInformation item = new OrderItemInformation();
item.BookID = Int32.Parse(gvBook.DataKeys[Int32.Parse(e.CommandArgument.ToString())]["BookID"].ToString());
item.ItemDate = DateTime.Now;
item.Number = 1;
///获取书籍价格
Label priceL = (Label)gvBook.Rows[Int32.Parse(e.CommandArgument.ToString())].FindControl("Price");
if (priceL == null)
{
///显示错误信息
Response.Write("<script>window.alert('数据错误')</script>");
return null;
}
item.Price = decimal.Parse(priceL.Text.Trim());
item.Name = gvBook.DataKeys[Int32.Parse(e.CommandArgument.ToString())]["Name"].ToString();
item.Desn = gvBook.DataKeys[Int32.Parse(e.CommandArgument.ToString())]["Desn"].ToString();
return (item);
}
protected void gvBook_RowCommand(object sender,GridViewCommandEventArgs e)
{
if(e.CommandName.ToLower() == "add")
{
OrderItemInformation item = null;
OrderFormInformation order = null;
///添加到购物车中,购物车的数据使用Session保存
if(Session[Session.SessionID + ASPNET35System.ShoppingCart] == null)
{
///当购物车没有任何信息时
item = GetOrderItemInformation(e);
if(item == null)
{
///显示错误信息
Response.Write("<script>window.alert('数据错误')</script>");
return;
}
///创建订单信息
order = new OrderFormInformation();
order.OrderItemList.Add(item);
order.TotalMoney = item.Price;
order.TotalNumber = item.Number;
///添加订单信息到Session中
Session[Session.SessionID + ASPNET35System.ShoppingCart] = order;
}
else
{
///当购物车中已经存在书籍信息时,从Session中获取以前的书籍信息
order = (OrderFormInformation)Session[Session.SessionID + ASPNET35System.ShoppingCart];
item = GetOrderItemInformation(e);
if(item == null)
{
///显示错误信息
Response.Write("<script>window.alert('数据错误')</script>");
return;
}
///判断是否已经添加该类型的书籍
int i = 0;
for(i = 0; i < order.OrderItemList.Count; i++)
{
///如果添加了,则数量加一
if(item.BookID == ((OrderItemInformation)order.OrderItemList[i]).BookID)
{
((OrderItemInformation)order.OrderItemList[i]).Number++;
break;
}
}
///否则添加新的书籍
if(i == order.OrderItemList.Count)
{
order.OrderItemList.Add(item);
}
///更新订单信息
order.TotalNumber++;
order.TotalMoney += item.Price;
Session[Session.SessionID + ASPNET35System.ShoppingCart] = order;
}
Response.Write("<script>window.alert('恭喜您,添加该书籍到购物车成功!')</script>");
}
}
protected void gvBook_RowCreated(object sender,GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
Button BuyBook = (Button)e.Row.FindControl("BuyBook");
if(BuyBook != null)
{
BuyBook.CommandArgument = e.Row.RowIndex.ToString();
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -