⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 carttoorder.aspx.cs

📁 这是一个编好的网上书店系统
💻 CS
字号:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace BookStore.Payment
{
	/// <summary>
	/// CartToOrder 的摘要说明。
	/// </summary>
	public class CartToOrder : System.Web.UI.Page
	{
		protected System.Web.UI.WebControls.DataList dlCart;
		protected System.Web.UI.WebControls.Label lbAllPrice;
		protected System.Web.UI.WebControls.Label lbAllDiscountPrice;
		protected System.Web.UI.WebControls.Label lbDiscount;
		protected System.Web.UI.WebControls.Label lbInfo;
		protected System.Web.UI.WebControls.Button btToOrder;
		protected System.Web.UI.HtmlControls.HtmlForm QuickSearchFrom;
		protected System.Web.UI.WebControls.Button btLater;
		protected System.Web.UI.WebControls.TextBox tbContactName;
		protected System.Web.UI.WebControls.TextBox tbCall;
		protected System.Web.UI.WebControls.TextBox tbAddress;
		protected System.Web.UI.WebControls.TextBox tbMobile;
		protected System.Web.UI.WebControls.TextBox tbPostCode;
		protected System.Web.UI.WebControls.TextBox tbEmail;
		protected System.Web.UI.WebControls.DropDownList ddPayment;
		protected System.Web.UI.WebControls.DropDownList ddSending;
		protected System.Web.UI.WebControls.TextBox tbMemo;
		protected System.Web.UI.WebControls.DropDownList ddIsNeedInvoice;
		protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator1;
		protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator3;
		protected System.Web.UI.WebControls.RegularExpressionValidator RegularExpressionValidator3;
		protected System.Web.UI.WebControls.RegularExpressionValidator RegularExpressionValidator1;
		protected System.Web.UI.WebControls.RegularExpressionValidator RegularExpressionValidator2;

		static int nUserID, nCountBookNumber;
		protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator2;

		private void Page_Load(object sender, System.EventArgs e)
		{
			if(!IsPostBack)
			{
				if(Object.Equals(Session["UserName"],null))
				{
					Session["ReqestedURL"] = Request.RawUrl;
					Response.Redirect("../Error.aspx");
				}
				else
				{
					nUserID = int.Parse(Session["UserID"].ToString());
					BindData();
					OtherDataBind();
				}
			}
		}
		private void OtherDataBind()
		{
			// Bind User infomation
			string strSql;
			strSql = "SELECT RealName, Address, PostCode, PhoneNumber, MobilePhone, EMail FROM [User] where ID=" + nUserID;
			DataSet currentDS;
			currentDS = RobertSoft.BookStore.DBClass.DBBaseClass.ExecuteSQLForDS(strSql);
			DataRow currentDR;
			currentDR = currentDS.Tables[0].Rows[0];
			this.tbContactName.Text = currentDR["RealName"].ToString();
			this.tbAddress.Text = currentDR["Address"].ToString();
			this.tbPostCode.Text = currentDR["PostCode"].ToString();
			this.tbEmail.Text = currentDR["EMail"].ToString();
			this.tbMobile.Text = currentDR["MobilePhone"].ToString();
			this.tbCall.Text = currentDR["PhoneNumber"].ToString();
		}
		private void BindData()
		{
			float fAllPrice, fAllDiscountPrice;
			fAllPrice = 0;
			fAllDiscountPrice = 0;
			// Now, query the shoppingcart detail view, bind data to datalist
			DataTable currentDT;

			RobertSoft.BookStore.OrderBook currentOrder = new RobertSoft.BookStore.OrderBook();

			currentDT = currentOrder.OrderDetail(nUserID);
			DataRow currentDR;

			nCountBookNumber = currentDT.Rows.Count;
			
			for(int i=0; i < currentDT.Rows.Count; i++)
			{
				currentDR = currentDT.Rows[i];
				fAllPrice += float.Parse(currentDR[4].ToString());
				fAllDiscountPrice += float.Parse(currentDR[5].ToString());
			}
			this.dlCart.DataSource = currentDT.DefaultView;
			this.dlCart.DataBind();

			this.lbAllPrice.Text = fAllPrice.ToString();
			this.lbAllDiscountPrice.Text = fAllDiscountPrice.ToString();
			float fDiscount = fAllPrice - fAllDiscountPrice;
			int nDiscount = (int) (fDiscount * 10);
			fDiscount = (float)nDiscount / 10;
			this.lbDiscount.Text = fDiscount.ToString();
		}

		#region Web 窗体设计器生成的代码
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{    
			this.dlCart.DeleteCommand += new System.Web.UI.WebControls.DataListCommandEventHandler(this.dlCart_DeleteCommand_1);
			this.btToOrder.Click += new System.EventHandler(this.btToOrder_Click);
			this.btLater.Click += new System.EventHandler(this.btLater_Click);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		// Delete current line
		private void dlCart_DeleteCommand_1(object source, System.Web.UI.WebControls.DataListCommandEventArgs e)
		{
			int intBookID = int.Parse(e.CommandArgument.ToString());
			DeleteData(intBookID);
			BindData();
			
		}

		private void DeleteData(int intBookID)
		{
			string strSql = "DELETE FROM [ShoppingCart] where BookID=" + intBookID + " and UserID=" + nUserID;
			try
			{
				RobertSoft.BookStore.DBClass.DBBaseClass.ExecuteSQLCmd(strSql);			
				this.lbInfo.Text = "删除成功";
			}
			catch
			{
				this.lbInfo.Text = "删除失败!";
				return;
			}			
		}

		// When click to order button, then ...
		private void btToOrder_Click(object sender, System.EventArgs e)
		{
			
			if(nCountBookNumber !=0)
			{// First, insert into OrderUserDetail, save user sending information;
				
					this.btToOrder.Enabled = false;
					string strSql;
					//strSql = "SELECT RealName, Address, PostCode, PhoneNumber, MobilePhone, EMail FROM [User] where ID=" + nUserID;
			
					string strContactName =	this.tbContactName.Text.Trim();
					string strAddress = this.tbAddress.Text.Trim();
					string strPostcode = this.tbPostCode.Text.Trim();
					string strEmail = this.tbEmail.Text.Trim();
					string strMobile = this.tbMobile.Text.Trim();
					string strCall = this.tbCall.Text.Trim();
					string strDate = DateTime.Now.ToString();
					string strMemo = this.tbMemo.Text.ToString();
					string strTotalDiscountPrice = this.lbAllDiscountPrice.Text.Trim();
					int nPayment =  int.Parse(this.ddPayment.SelectedItem.Value);
					int nSending = int.Parse(this.ddSending.SelectedItem.Value);
					int nNeedInvoice = int.Parse(this.ddIsNeedInvoice.SelectedItem.Value);
			
					strSql = "INSERT INTO [OrderUserDetail] (UserID, ContactName, Address, Email, PostCode, PhoneNumber, MobilePhone, SendMemo, SendMethod, PayMethod, OrderDate, OrderStatus, OrderFinishDate, NeedInvoice, TotalDiscountPrice)VALUES("
						+ nUserID + ","
						+ "'" + strContactName + "',"
						+ "'" + strAddress + "',"
						+ "'" + strEmail + "',"
						+ "'" + strPostcode + "',"
						+ "'" + strCall + "',"
						+ "'" + strMobile + "',"
						+ "'" + strMemo + "',"
						+ nSending + ","
						+ nPayment + ","
						+ "'" + strDate + "',"
						+ 1 + ","
						+ "''," + nNeedInvoice + ",'" + strTotalDiscountPrice + "')";
					try
					{
						RobertSoft.BookStore.DBClass.DBBaseClass.ExecuteSQLCmd(strSql);			
					}
					catch
					{
					
					}		

					// When insert succeed, get orderId
					strSql = "Select Max(ID) From OrderUserDetail";
					int nOrderID = RobertSoft.BookStore.DBClass.DBBaseClass.ExecuteSQLForValue(strSql);
	
					// copy all shopping cart table stuff to OrderBookDetail table
					strSql = "select * from ShoppingCartToBookOrderDetail where UserID=" + nUserID;
					DataSet currentDV;
					currentDV = RobertSoft.BookStore.DBClass.DBBaseClass.ExecuteSQLForDS(strSql);
					DataRow currentDR;

					for(int i=0; i < currentDV.Tables[0].Rows.Count; i++)
					{
						float fPrice;
						currentDR = currentDV.Tables[0].Rows[i];
						int nBookID = int.Parse(currentDR["BookID"].ToString());
						int nQuantity = int.Parse(currentDR["Quantity"].ToString());
						int nSpecial = int.Parse(currentDR["SpecialPrice"].ToString());
						if( nSpecial == 1)
						{
							int nDiscount;
							nDiscount = int.Parse(currentDR["Discount"].ToString());
							fPrice = float.Parse(currentDR["Price"].ToString());
							fPrice = (float)nDiscount*fPrice/100;
							int n = (int) (fPrice * 10);
							fPrice = (float)n / 10;							
						}
							// otherwise, the book sale price will consider user's level
							// 1 star * 0.9
							// 2 star * 0.88
							// 3 star * 0.85
							// 4 star * 0.80
							// 5 star * 0.78
						else
						{
							int n = int.Parse(currentDR["UserLevel"].ToString());
							if( n == 1)
							{
								fPrice = float.Parse(currentDR["Price"].ToString()) * (float)0.9;
								int nCount = (int) (fPrice * 10);
								fPrice = (float)nCount / 10;						
							}
							else if( n == 2)
							{
								fPrice = float.Parse(currentDR["Price"].ToString()) * (float)0.88;
								int nCount = (int) (fPrice * 10);
								fPrice = (float)nCount / 10;						
							}
							else if(n == 3)
							{
								fPrice = float.Parse(currentDR["Price"].ToString()) * (float)0.85;
								int nCount = (int) (fPrice * 10);
								fPrice = (float)nCount / 10;					
							}
							else if(n == 4)
							{
								fPrice = float.Parse(currentDR["Price"].ToString()) * (float)0.80;
								int nCount = (int) (fPrice * 10);
								fPrice = (float)nCount / 10;					
							}
							else if(n == 5)
							{
								fPrice = float.Parse(currentDR["Price"].ToString()) * (float)0.78;
								int nCount = (int) (fPrice * 10);
								fPrice = (float)nCount / 10;						
							}
							else
							{
								fPrice = float.Parse(currentDR["Price"].ToString()) * (float)0.9;
								int nCount = (int) (fPrice * 10);
								fPrice = (float)nCount / 10;					
							}
						}
						strSql = "INSERT INTO [OrderBookDetail](UserID, OrderID, BookID, Quantity, DiscountPrice) VALUES("
							+ nUserID + ","
							+ nOrderID + ","
							+ nBookID + ","
							+ nQuantity + ",'" + fPrice.ToString() +"')";
						try
						{
							RobertSoft.BookStore.DBClass.DBBaseClass.ExecuteSQLCmd(strSql);			
						}
						catch
						{
						
						}			
					}
					// Delete shopping cart coresponde contents
					strSql = "DELETE FROM [ShoppingCart] where UserID=" + nUserID;
					try
					{
						RobertSoft.BookStore.DBClass.DBBaseClass.ExecuteSQLCmd(strSql);			
					}
					catch
					{
					
					}			
			
					// Get the orderID and send it to next page
					string strUrl;
					strUrl = "OrderOK.aspx?OrderID=" + nOrderID + "&Payment=" + nPayment + "&Sending=" + nSending;
					this.btToOrder.Enabled = true;
					Response.Redirect(strUrl);
				
			}
			else
			{
				this.lbInfo.Text = "购物车不能为空!";
				this.lbInfo.ForeColor = Color.Red;
			}
		}

		private void btLater_Click(object sender, System.EventArgs e)
		{
			Response.Redirect("../Default.aspx");
		}

		
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -