C 经典购物车流程全代码

更新时间:2023-05-18 21:36:01 阅读量: 实用文档 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

C# 经典购物车流程全代码 asp.net 经典毕业设计

C# 购物车 及后台代码

C# 程序语言

一.防止SQL注入

public static bool SqlFilter2(string InText)

{ string word = "and|exec|insert|select|delete|update|chr|mid|master|or|truncate|char|declare|join|'"; if (InText == null)

return false;

foreach (string str_t in word.Split('|'))

{if ((InText.ToLower().IndexOf(str_t + " ") > -1) || (InText.ToLower().IndexOf(" " + str_t) > -1) || (InText.ToLower().IndexOf(str_t) > -1))

{ return true;//返回有}

}

}

二.MD5加密

using system.web.security

string pwd = FormsAuthentication.HashPasswordForStoringInConfigFile(TextBox2.Text,"MD5");

四..配置文件的加密与解密

数据库连接字符串

<appSettings>

<add key=”ConnectionString” value=”server=(Local);database = test; pwd=sa;uid=sa;”/> </appSettings

加密

Configurationconfig=WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);

ConfigurationSection section config.GetSection(”appSettings”);

if(section !=null && !section.SectionInformation.IsProtected)

{ Section.SectionInformation.ProtectSection(”RsaProtectedConfigurationProvider”); Config.Save();

}

解密

Configuration

config=WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath); ConfigurationSection section =config.GetSection(“appSettings”);

If(section !=null && secion.SectionInformation.IsProtected)

{Section SectionInformation.UnprotectSection();

Config.Save();

}

五.邮件的发送和接收

Encoding encoding = Encoding.GetEncoding("GB2312");

string address = TextBox1.Text.Trim();

string biaoti = "购物网用户激活";

string content = "status.aspx?id=" + TextBox2.Text + "";

C# 经典购物车流程全代码 asp.net 经典毕业设计

MailAddress from = new MailAddress("clear_rm@", "Fei_L", encoding);

MailAddress to = new MailAddress(address);

MailMessage mail = new MailMessage(from, to);

mail.Subject = biaoti;

mail.Body = content;

mail.SubjectEncoding = encoding;

mail.BodyEncoding = encoding;

SmtpClient smtp = new SmtpClient("");

smtp.DeliveryMethod = work;

eDefaultCredentials = true;

smtp.Credentials = new workCredential("clear_rm@", "061110"); smtp.Send(mail);

dbcon.message("会员注册成功,请登录邮件激活会员!");

六.产品添加

protected void Button1_Click(object sender, EventArgs e)

{SqlConnection conn = dbcon.conn();

conn.Open();

SqlCommand cmd_rz = new SqlCommand("select count(*) from shop where sortid='" + TextBox6.Text + "'", conn);

int num_rz = (int)cmd_rz.ExecuteScalar();

conn.Close();

if (num_rz > 0)

{dbcon.message("此产品编号已登记!");

}

else{

if (DropDownList1.SelectedItem.Text == "产品分类")

{dbcon.message("请选择分类!");

}

else

{conn.Open();

SqlCommand cmd=new SqlCommand ("insert into shop(sortid, sortname, product, ,buy, inventory, discount, price, path, parentpath) values ('" + TextBox6.Text + "','" + TextBox1.Text+ "','" + FCKeditor1.Value+ "','" + TextBox3.Text + "','" + TextBox3.Text+ "','" + TextBox4.Text+ "','" +(int.Parse(TextBox5.Text) * int.Parse(TextBox4.Text)).ToString()+ "','" +Session["picid"].ToString()+ "','" + DropDownList1.SelectedItem.Text + "')", conn);

int num = cmd.ExecuteNonQuery();

conn.Close();

if (num > 0) dbcon.message("添加产品成功!");

}

}

}

七.用户登录状态的保存

if (Session["uid"] != null)

{string uid = "";

C# 经典购物车流程全代码 asp.net 经典毕业设计

||if (Request["id"].ToString() != "")

{uid = Request["id"].ToString();

}

if (!Page.IsPostBack)

{uid = username.Text;

SqlDataReader rd = select(uid);

}

}

八.购物车

a = mandArgument.ToString();

//if (Session["ID"] != null)//如果用户没有登录

//{ if (Session["Cart"] == null)//如果购物篮不存在

{// Response.Write("<script language='javascript'>alert('1')</script>");

this.BuildCart();//创建购物篮并将商品存入

}

else//购物篮存在

{ DataTable cart = Session["Cart"] as DataTable;

if (this.ExistBook(cart))//如果购物篮已存在该商品

{this.BuildSession(cart); //修改购物篮中的商品

}

}

//Response.Redirect("buy car.aspx");//跳转到购物车界面

//}

//else

//{//如果没有登录,跳转到登录界面

// Response.Redirect("denglu.aspx");

//}

}

public bool ExistBook(DataTable cart)

{ foreach (DataRow dr in cart.Rows)

{

if (dr["QID"].ToString()==a)

{ dr["NUM"] = Convert.ToInt32(dr["NUM"]) + 1;

Session["Cart"] = cart;

//Response.Redirect("Cart.aspx");

}

}

return true;

}

public void BuildCart()

{//创建购物车

DataTable cart = new DataTable();//已经创建了表,但是没有字段

cart.Columns.Add("QID");

cart.Columns.Add("ID");

C# 经典购物车流程全代码 asp.net 经典毕业设计

cart.Columns.Add("NUM");

cart.Columns.Add("IID");

cart.Columns.Add("name");

cart.Columns.Add("jiage");

//Response.Write("<script language='javascript'>alert('创建了表')</script>");

//cart表中已有5个字段

//将点击的商品加入购物车中

this.BuildSession(cart);//往cart中添加一条记录

}

///// <summary>

///// 添加新书

///// </summary>

///// <param name="cart"></param>

public void BuildSession(DataTable cart)

{

//新建一个数据行的记录

DataRow dr = cart.NewRow();

SqlDataReader rd=select(a);

if (rd.Read())

{ dr["QID"] = a;//存商品编号

dr["ID"] = rd[1];//存商品名称

dr["NUM"] = "1";//存商品数量

dr["iid"] = rd[2];

dr["name"] = rd[3];//存入单价

dr["jiage"] = rd[4];//存入商品图片地址

cart.Rows.Add(dr);//将数据行加入到cart表中

//Response.Write("<script language='javascript'>alert('添加了记录')</script>"); //Response.Write(rd[1]);

//Response.Write(rd[2]);

//Response.Write(rd[3]);

//Response.Write(rd[4]);

}

Session["Cart"] = cart;//将数据表cart的值存入session保存

//Response.Write("<script language='javascript'>alert('购买成功')</script>");

RegisterStartupScript("", "<script language='javascript'>alert('购买成功')</script>"); }

九..计算总价

if (Session["cart"] == null)

{

Response.Write("<script>alert('购物车为空,请挑选商');window.location.href='shengri.aspx?id=1';</script>"); }

DataTable cart = (DataTable)Session["cart"];

GridView1.DataSource = cart; 品

C# 经典购物车流程全代码 asp.net 经典毕业设计

GridView1.DataBind();

int NUM = 0;

foreach (DataRow dr in cart.Rows)

{NUM += int.Parse(dr["NUM"].ToString()) * int.Parse(dr["jiage"].ToString());

}

Label3.Text = NUM.ToString();

十.删除时跳出对话框

双击GridView的OnRowDataBound事件;

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) {

if (e.Row.RowType == DataControlRowType.DataRow)

{

if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)

{

((LinkButton)e.Row.Cells[6].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('你确认要删除:"" + e.Row.Cells[1].Text + ""吗?')");

}

}

}

C# 经典购物车流程全代码 asp.net 经典毕业设计

本文来源:https://www.bwwdw.com/article/9ln4.html

Top