在线购物网站核心设计

更新时间:2023-04-26 07:13:01 阅读量: 初中教育 文档下载

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

1 在线购物网站核心设计

一、 商品浏览页面设计(default.aspx )

同理设定lblPrice DataBindings Text —Eval(“GoodsPrice ”) image1 ImageUrl —

Eval("GoodsPhoto")

同理设定 “购买”链接按扭亏为盈LinkDatabindings —Eval(“GoodsID ”)

“详细信息”的CommandName 设置为 "shopdescrbe"

“购买”的CommandName 设置为 "buy"

另外设置DataList1为水平2列显示。

public partial class_Default : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

string constr = WebConfigurationManager.ConnectionStrings["db_Conn"].ToString();

SqlConnection conn = new SqlConnection(constr);

string Sqlstr = "select * from tb_GoodsInfo";

SqlDataAdapter adp = new SqlDataAdapter(Sqlstr, conn);

conn.Open();

DataSet ds = new DataSet();

adp.Fill(ds);

DataList1.DataSource = ds.Tables[0];

DataList1.DataBind();

conn.Close();

}

protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)

{

if (5744f03167ec102de2bd8918mandName == "shopdescrbe")

{

string str_GoodsID = 5744f03167ec102de2bd8918mandArgument.ToString();

Response.Redirect("shopdes_1.aspx?GoodsId=" + str_GoodsID);

}

//完成登录登录窗口shopbuycart.asp再设计

if (5744f03167ec102de2bd8918mandName == "mybuy")

{

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

{

string str_GoodsId = 5744f03167ec102de2bd8918mandArgument.ToString();

Response.Redirect("shopbuycart.aspx?GoodsID=" + str_GoodsId);

}

else

{

Response.Write("");

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

}

}

}

}

运行结果:

2

二、商品详细页面设计(shopdes.aspx)

Txtintroduce:

using System.Data.SqlClient;

public partial class shopdes_1 : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

string str_GoodsID = Request["GoodsID"];

string constr = "server=.\\sqlexpress;database=db_NetShop;integrated security=true";

SqlConnection conn=new SqlConnection(constr);

string Sqlstr="select * from tb_GoodsInfo where GoodsID='"+str_GoodsID+"'";

SqlDataAdapter adp = new SqlDataAdapter(Sqlstr,conn );

conn.Open();

DataSet ds = new DataSet();

adp.Fill(ds);

3

lblname.Text = ds.Tables[0].Rows[0][1].ToString();

lbltype.Text = ds.Tables[0].Rows[0][2].ToString();

lblprice.Text = ds.Tables[0].Rows[0][4].ToString();

Image1.ImageUrl = ds.Tables[0].Rows[0][3].ToString();

txtintroduce.Text = ds.Tables[0].Rows[0][5].ToString();

conn.Close();

protected void Button1_Click(object sender, EventArgs e)

{

Response.Redirect("Default.aspx");

}

protected void Button2_Click(object sender, EventArgs e)

{

Response.Write("");

}

}

由商品浏览页面运行结按详细信息按钮运行结果:

三、会员登录页面

using System.Data.SqlClient;

public partial class Login : System.Web.UI.Page

{

private string GenerateCheckCode()

{

int number; //定义变量

char code;

string checkCode = String.Empty;

Random random = new Random(); //定义随机变量实例

for (int i = 0; i < 4; i++) //利用For循环语句生成4个随机数或字母

4

{

number = random.Next();

if (number % 2 == 0)

{

code = (char)('0' + (char)(number % 10));

}

else

{

code = (char)('A' + (char)(number % 26));

}

checkCode += code.ToString();

}

Response.Cookies.Add(new HttpCookie("CheckCode", checkCode));

return checkCode;

}

private void CreateCookie()

{

//创建一个Cookie对象

HttpCookie mycookie = new HttpCookie("UserName");

//获取创建的Cookie对象的过期时间

mycookie.Expires = DateTime.MaxValue;

//将创建的Cookie对象添加到内部Cookie集合中

Response.AppendCookie(mycookie);

}

protected void Page_Load(object sender, EventArgs e)

{

lblCode.Text = GenerateCheckCode();

if (!IsPostBack)//判断页面是否首次加载

{

// lblCode.Text = GenerateCheckCode();

if (!Object.Equals(Request.Cookies["UserName"], null))

{

//创建一个Cookie对象,实现记住用户名

HttpCookie readcookie = Request.Cookies["UserName"]; txtName.Text = readcookie.Value;

}

}

}

//登录按钮

protected void btnLogin_Click(object sender, EventArgs e)

{

CreateCookie();

HttpCookie cookie = Request.Cookies["CheckCode"];

if (cookie.Value ==txtCode.Text.Trim())

{

5

//数据库连接字符

string ConStr = "server=.\\sqlexpress;database=db_NetShop;integrated security=true";

SqlConnection con = new SqlConnection(ConStr);

string SqlStr = "select * from tb_User where UserName='" + txtName.Text.Trim() + "' and PassWord='" + txtPwd.Text.Trim() + "'"; //Select查询语句

SqlDataAdapter ada = new SqlDataAdapter(SqlStr, con);

con.Open(); //打开连接

DataSet ds = new DataSet();

ada.Fill(ds);

if (ds.Tables[0].Rows.Count != 0)//判断用户是否通过身份验证

{

Session["UserID"] = ds.Tables[0].Rows[0][0].ToString();

Response.Redirect("Default.aspx");

}

else

{

Response.Write(""); }

Response.Write("");

}

else

{

Response.Write("");//

}

}

protected void btnClear_Click(object sender, EventArgs e)

{

txtName.Text = "";

txtPwd.Text = "";

txtCode.Text = "";

}

protected void btnExit_Click(object sender, EventArgs e)

{

Response.Write("");

}

}

四、购物车页面

6

7

同样设置:lblprice DataBindings 为(Eval(“GoodsPrice ”) txtNum DataBindings 为(Eval(“Num ”). 链接按扭更新和删除的CommandName 属性设为dataupdate 和 datadelete

两个按扭的DataBindings 与下图一样。

设置页眉与页脚

自动套用格式“沙滩与天空”,

using System.Data.SqlClient;

public partial class shopbuycart : System.Web.UI.Page

{

//公共变量购物车中商品的总计金额

public static string M_str_Count;

//动态连接数据库,提取数据数据库信息保存在DataSet对象中

public static DataSet reDs(string P_str_cmdtxt)

{

string constr = "server=.\\sqlexpress;database=db_NetShop;integrated security=true";

SqlConnection conn = new SqlConnection(constr);

conn.Open();

SqlDataAdapter adp = new SqlDataAdapter(P_str_cmdtxt, conn);

DataSet ds = new DataSet();

adp.Fill(ds);

conn.Close();

return ds;

}

//动态连接数据库,执行添加、删除、更新数据表的任务

public static bool exSql(string P_str_cmdtxt)

{

string constr = "server=.\\sqlexpress;database=db_NetShop;integrated security=true";

SqlConnection conn = new SqlConnection(constr);

conn.Open();

SqlCommand cmd = new SqlCommand(P_str_cmdtxt, conn);

try

{

cmd.ExecuteNonQuery();

return true;

}

catch (Exception ex)

{

return false;

}

finally

8

{

conn.Dispose();

conn.Close();

}

}

//把数据数据表中的数据绑定到数据列表中。

public void Bind()

{

//调用reDs方法把数据表的信息保存到DataSet对象中

DataSet ds = reDs("select *,GoodsPrice*Num As Count from tb_Cart where CartID=" + Session["UserID"]);

float P_fl_Count = 0;

//把表中的第7列数据累加保存到变量P_fl_Count中 (即合计金额)

foreach (DataRow dr in ds.Tables[0].Rows)

{

P_fl_Count += Convert.ToSingle(dr[6]);

}

M_str_Count = P_fl_Count.ToString();

DataList1.DataSource = ds;

DataList1.DataBind();

}

protected void Page_Load(object sender, EventArgs e)

{

if (!IsPostBack)

{

string P_str_CartID = Session["UserID"].ToString();

string P_str_GoodsID = Request["GoodsID"];

DataSet myds = reDs("select count(*) from tb_Cart where CartID=" + P_str_CartID + "and GoodsID=" +

P_str_GoodsID);

if (myds.Tables[0].Rows[0][0].ToString() == "0")

{

DataSet ds1 = reDs("select GoodsName,GoodsPrice from tb_GoodsInfo where GoodsID=" + P_str_GoodsID);

string P_str_GoodsName = ds1.Tables[0].Rows[0][0].ToString();

string P_str_GoodsPrice = ds1.Tables[0].Rows[0][1].ToString();

string P_str_Num = "1";

exSql("insert into tb_Cart (CartID,GoodsID,GoodsName,GoodsPrice,Num) values(" + P_str_CartID + "," + P_str_GoodsID + ",'" + P_str_GoodsName + "'," + P_str_GoodsPrice + "," + P_str_Num + ")");

}

else

{

exSql("update tb_Cart set Num=Num+1 Where CartID=" + P_str_CartID + "and GoodsID=" + P_str_GoodsID); }

Bind();

}

9

}

protected void LinkButton1_Click(object sender, EventArgs e)//继续购物按扭 {

Response.Redirect("Default.aspx");

}

}

10

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

Top