📄 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 BookShop;
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)
{
Book book = new Book();
SqlDataReader recb = book.GetBookByCategory(nCategoryID);
BookList.DataSource = recb;
BookList.DataBind();
recb.Close();
}
protected void BookList_ItemCommand(object source, DataGridCommandEventArgs e)
{
if (e.CommandName.ToLower() == "add")
{
OrderItemInformation item = null;
OrderFormInformation order = null;
///添加到购物车中,购物车的数据使用Session保存
if (Session[Session.SessionID + ASPNET2System.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 + ASPNET2System.ShoppingCart] = order;
}
else
{
///当购物车中已经存在书籍信息时,从Session中获取以前的书籍信息
order = (OrderFormInformation)Session[Session.SessionID + ASPNET2System.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 + ASPNET2System.ShoppingCart] = order;
}
Response.Write("<script>window.alert('恭喜您,添加该书籍到购物车成功!')</script>");
}
}
private OrderItemInformation GetOrderItemInformation(DataGridCommandEventArgs e)
{
///当购物车没有任何信息时
OrderItemInformation item = new OrderItemInformation();
item.BookID = Int32.Parse(BookList.DataKeys[e.Item.ItemIndex].ToString());
item.ItemDate = DateTime.Now;
item.Number = 1;
///获取书籍价格
Label priceL = (Label)e.Item.FindControl("Price");
if (priceL == null)
{
///显示错误信息
Response.Write("<script>window.alert('数据错误')</script>");
return null;
}
item.Price = decimal.Parse(priceL.Text.Trim());
string sDesnName = e.CommandArgument.ToString();
int index = sDesnName.IndexOf("|");
if (index < -1 || index + 1 >= sDesnName.Length) return item;
item.Desn = sDesnName.Substring(0, index);
item.Name = sDesnName.Substring(index + 1);
return (item);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -