62,046
社区成员
发帖
与我相关
我的任务
分享
<%@ Page Title="" Language="C#" MasterPageFile="~/MemberPortal/MainSite.master" AutoEventWireup="true" CodeFile="Shopping.aspx.cs" Inherits="MemberPortal_Shopping" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<div style=" text-align:left;">
<h1><img src="image/shopping.gif" width="121" height="21" /></h1>
<p style=" font-size:18px; font:幼圆;" >您选好的商品:<asp:Label ID="Label1" runat="server"
Text="Label" Visible="False"></asp:Label>
</p>
</div>
<br /><div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
Width="100%" BackColor="White" BorderColor="#999999" BorderStyle="None"
BorderWidth="1px" CellPadding="3" GridLines="Vertical" ShowFooter="True"
onrowdatabound="GridView1_RowDataBound"
onrowcommand="GridView1_RowCommand" onrowdeleting="GridView1_RowDeleting">
<AlternatingRowStyle BackColor="#DCDCDC" />
<Columns>
<asp:TemplateField HeaderText="图书编号">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("BookId") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="图书名称" FooterText="合计">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("BookName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="单价">
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Eval("UnitPrice") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="商品个数">
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Bind("Number") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Number") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="小计">
<ItemTemplate>
<asp:Label ID="Label5" Text="" runat="server"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="操作" ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Delete" Text="删除"></asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Edit" Text="编辑"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
<HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
<RowStyle BackColor="#EEEEEE" ForeColor="Black" />
<SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#0000A9" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#000065" />
</asp:GridView></div>
<div style=" text-align:right; margin-top:10px;">
<asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="~/MemberPortal/image/balance.gif" /></div>
</asp:Content>
public partial class MemberPortal_Shopping : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if(Request.QueryString["Op"]=="BuyBooks"){
int bookId = Convert.ToInt32(Request.QueryString["bookId"]);
BuyBooks(bookId);
}
if (Session["Cart"] != null)
{
BindCart();
}
else {
Label1.Text = "目前还没有购买任何图书";
}
}
}
private void BindCart() {
GridView1.DataSource = Session["Cart"];
GridView1.DataBind();
}
private void BuildCart() {
DataTable cart = new DataTable();
cart.Columns.Add("BookId",typeof(Int32));
cart.Columns.Add("BookName",typeof(string));
cart.Columns.Add("UnitPrice", typeof(double));
cart.Columns.Add("Number", typeof(Int32));
Session["Cart"] = cart;
}
private void AppendBooks(int bookId,string bookName,double unitPrice,int number) {
DataTable cart =Session["Cart"] as DataTable;
DataRow dr = cart.NewRow();
dr["BookId"]=bookId;
dr["BookName"]=bookName;
dr["UnitPrice"] = unitPrice;
dr["Number"] = number;
cart.Rows.Add(dr);
Session["Cart"] = cart;
}
private bool ExistBook(int bookId) {
DataTable cart = Session["Cart"] as DataTable;
foreach (DataRow dr in cart.Rows) {
if (Convert.ToInt32(dr["BookId"]) == bookId) {
return true;
}
}
return false;
}
private void UpdateBooks(int bookId)
{
DataTable cart = Session["Cart"] as DataTable;
foreach (DataRow dr in cart.Rows)
{
if (Convert.ToInt32(dr["BookId"]) == bookId)
{
dr["Number"] = Convert.ToInt32(dr["Number"])+1;
}
}
Session["Cart"] = cart;
}
private void BuyBooks(int bookId) {
string strConn = ConfigurationManager.ConnectionStrings["conn"].ConnectionString;
SqlConnection conn = new SqlConnection(strConn);
conn.Open();
string sql = "select * from Books where Id=@Id";
SqlDataAdapter dr = new SqlDataAdapter(sql, conn);
DataSet ds = new DataSet();
SqlParameter[] paras = new SqlParameter[]{
new SqlParameter("@Id",bookId),
};
dr.SelectCommand.Parameters.AddRange(paras);
dr.Fill(ds, "CategoryTable");
string bookName = ds.Tables["CategoryTable"].Rows[0][1].ToString();
double unitPrice = Convert.ToDouble(ds.Tables["CategoryTable"].Rows[0][7].ToString());
if (Session["Cart"] == null)
{
this.BuildCart();
this.AppendBooks(bookId,bookName, unitPrice ,1);
}
else {
if (ExistBook(bookId))
{
this.UpdateBooks(bookId);
}
else {
this.AppendBooks(bookId, bookName, unitPrice ,1);
}
}
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#D3D7F5'");
e.Row.Attributes.Add("onmouseout", "currentcolor=this.style.backgroundColor=currentcolor");
Label lblPrice = e.Row.FindControl("Label3") as Label;
Label lblNumber = e.Row.FindControl("Label4") as Label;
Label lblTotal = e.Row.FindControl("Label5") as Label;
double UnitPrice = Convert.ToDouble(lblPrice.Text);
int Number = Convert.ToInt32(lblNumber.Text);
double Total = UnitPrice * Number;
lblTotal.Text = Total.ToString();
}
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
}
}