📄 sto.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;
public partial class sto : System.Web.UI.Page
{
public DataSet ds ;
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = Session["user"].ToString() + "欢迎您" + "(如果您不是" + Session["user"].ToString() + "请退出登录)";
if (!Page.IsPostBack)
{
cart();
}
}
private void ShowMassage(string msg)
{
Response.Write("<script>alert('" + msg + "')</script>");
}
void cart()
{
string str = Session["buy"].ToString();
if (str == "")
{
ShowMassage("你还没有向购物车中添加任何商品!");
}
else
{
string[] Array = System.Text.RegularExpressions.Regex.Split(str, ",", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
DataTable tab = new DataTable();
DataColumn col1 = new DataColumn("userid", System.Type.GetType("System.Int32"));
DataColumn col2 = new DataColumn("id", System.Type.GetType("System.Int32"));
DataColumn col3 = new DataColumn("petname", System.Type.GetType("System.String"));
DataColumn col4 = new DataColumn("petmoney", System.Type.GetType("System.String"));
DataColumn col5 = new DataColumn("count", System.Type.GetType("System.Int32"));
DataColumn col6 = new DataColumn("TotalPrice", System.Type.GetType("System.Int32"));
DataColumn col7 = new DataColumn("ordertime", System.Type.GetType("System.String"));
tab.Columns.Add(col1);
tab.Columns.Add(col2);
tab.Columns.Add(col3);
tab.Columns.Add(col4);
tab.Columns.Add(col5);
tab.Columns.Add(col6);
tab.Columns.Add(col7);
foreach (string i in Array)
{
DataRow dr = tab.NewRow();
if (i != "")
{
int pro_id = Int32.Parse(i);
SqlConnection con = new SqlConnection("data source=localhost;uid=sa;pwd=;database=Ebusiness");
con.Open();
SqlCommand comm1 = new SqlCommand("select id,petname,petmoney from product_info where id=@id", con);
SqlParameter pid = new SqlParameter("@id", SqlDbType.Int);
pid.Value = pro_id.ToString();
comm1.Parameters.Add(pid);
SqlDataReader reader;
reader = comm1.ExecuteReader();
if (reader.Read())
{
dr["userid"] = Convert.ToInt32(Session["userid"].ToString());
dr["id"] = reader.GetInt32(0);
dr["petname"] = reader.GetString(1);
dr["petmoney"] = reader.GetSqlMoney(2).ToString();
dr["count"] = "1";
Double subtotal_here = Double.Parse(dr["petmoney"].ToString()) * Int32.Parse(dr["count"].ToString());
dr["TotalPrice"] = subtotal_here.ToString();
dr["ordertime"] = DateTime.Now.ToString();
reader.Close();
}
con.Close();
for (int j = 0; j < tab.Rows.Count; j++)
{
int product_id_fromsession = Int32.Parse(tab.Rows[j]["id"].ToString());
if (pro_id == product_id_fromsession)
{
//Response.Write("这是测试语句!看是否每次都会运行");
tab.Rows[j].Delete();
}
}
}
else
break;
tab.Rows.Add(dr);
}
Session["cart"] = tab;
ShoppingCartDlt.DataSource = (DataTable)Session["cart"];
ShoppingCartDlt.DataBind();
}
}
protected void butt_Click(object sender, EventArgs e)
{
Response.Redirect("index.aspx");
}
protected void update_Click(object sender, EventArgs e)
{
int i;
DataTable newtable = (DataTable)Session["cart"];
for (i=0;i<=this.ShoppingCartDlt.Items.Count-1;i++)
{
TextBox CountText=(TextBox)this.ShoppingCartDlt.Items[i].Cells[4].FindControl("CountTb");
CheckBox ProductIDCheck = (CheckBox)this.ShoppingCartDlt.Items[i].Cells[0].FindControl("chkProductID");
newtable.Rows[i][4] = Int32.Parse(CountText.Text.ToString());
newtable.Rows[i][5] = Int32.Parse(newtable.Rows[i][4].ToString()) * Double.Parse(newtable.Rows[i][3].ToString());
SqlConnection con = new SqlConnection("data source=localhost;uid=sa;pwd=;database=Ebusiness");
con.Open();
SqlCommand comm = new SqlCommand("select num from product_info where id=@id",con);
comm.Parameters.Add("@id", SqlDbType.Int).Value = Int32.Parse(newtable.Rows[i][1].ToString());
SqlDataReader reader = comm.ExecuteReader();
if (reader.Read())
{
if (Convert.ToInt32(newtable.Rows[i][4].ToString() )>reader.GetInt32(0))
{
ShowMassage("该产品库存不足你只能购买" + reader.GetInt32(0) .ToString()+ "件");
newtable.Rows[i][4] = reader.GetInt32(0);
}
reader.Close();
}
con.Close();
if (ProductIDCheck.Checked)
{
newtable.Rows[i][0] = 1;//添加删除标记1
}
}
for (int m = 0; m < newtable.Rows.Count; m++)
{
if (newtable.Rows[m][0].ToString()== "1")
{
newtable.Rows[m].Delete();
}
}
Session["cart"] = newtable;
ShoppingCartDlt.DataSource = (DataTable)Session["cart"];
ShoppingCartDlt.DataBind();
}
protected void CheckOut_Click(object sender, EventArgs e)
{
if (Session["Cart"] != null) //购物车是否为空
{
int h;
Double TotalPri;
TotalPri = 0;
DataTable nowTable3 = new DataTable("Cart");
nowTable3 = (DataTable)Session["Cart"];
if (nowTable3.Rows.Count > 0) //返回购物车中是否有货物
{
for (h = 0; h <= nowTable3.Rows.Count - 1; h++)
{
TotalPri = TotalPri + Int32.Parse(nowTable3.Rows[h][5].ToString());//Double.Parse((string)TotalText.Text);
}
label.Text = "总计: " + TotalPri.ToString() + " 元";
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
Session["buy"] = "";
Session["cart"] = "";
ShoppingCartDlt.Dispose();
ShoppingCartDlt.Visible = false;
Label7.Text = "您的购物车为空";
}
protected void Button2_Click(object sender, EventArgs e)
{
Response.Redirect("orderview.aspx");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -