【ASP】session实现购物车

1.问题提出

            利用session内置对象,设计并实现一个简易的购物车,要求如下:

    1)利用用户名和密码,登录进入购物车首页

    2)购物首页显示登录的用户名以及该用户是第几位访客。(同一用户的刷新应该记录为1次)

    3)购物页面分为两个部分:家用电器和运动系列,选择商品种类进行购物。

    4)在每个具体的购物页中,如果用户已经选择了商品,当再次进入到该页时要显示已选中的商品。

    5)选好商品可以查看购物车,购物车中有继续购物,清空购物车。

 

2.设计实现思路

    1)登录

 

 1 protected void Button1_Click(object sender, EventArgs e)
 2         {
 3             string a = TextBox1.Text;
 4             string b = TextBox2.Text;
 5 
 6             if (a.Equals("yitou") && b.Equals("111"))
 7             {
 8                 Application["name"] = TextBox1.Text;
 9                 Response.Redirect("welcome.aspx");
10             }
11            
12         }

 界面设计

 

             2)web.config中设置session

      在Global.asax中设置初始访问次数为0。利用session_start,保证用户数登录加1.

 1  void Application_Start(object sender, EventArgs e)
 2         {
 3             // 在应用程序启动时运行的代码
 4             RouteConfig.RegisterRoutes(RouteTable.Routes);
 5             BundleConfig.RegisterBundles(BundleTable.Bundles);
 6             Application["count"] = 0;
 7         }
 8         void Session_Start(object sender, EventArgs e)
 9         {
10             Application["count"] = (int)Application["count"] + 1;
11         }

     welcome.asp

 1 protected void Page_Load(object sender, EventArgs e)
 2         {
 3 
 4             string s = Application["name"].ToString();
 5             Response.Write("欢迎" + s + "登录该页面,您是第"+Application["count"].ToString()+"个用户");
 6 
 7         }
 8         protected void Button1_Click(object sender, EventArgs e)
 9         {
10             if (RadioButton1.Checked)
11             {
12                 Server.Transfer("goods.asp");
13             }
14             if (RadioButton2.Checked)
15             {
16                 Server.Transfer("sports.asp");
17             }
18         }

界面设计

    3)如果选择运动界面

 

 

    4)在每个具体的购物页中,如果用户已经选择了商品,当再次进入到该页时要显示已选中的商品。

     

 

    5)选择好商品,可以查看购物车中的内容:

 

 1 protected void Page_Load(object sender, EventArgs e)
 2         {
 3             Label1.Text = "电器:";
 4             Label2.Text = "运动:";
 5             int num=0;
 6             List<string> str = (List<string>)Session["goods"];
 7             if (str != null)
 8             {
 9                 for (int i = 0; i < str.Count; i++)
10                 {
11                     Label1.Text += "  " + str[i];
12                 }
13             }
14             else num++;
15             List<string> sports = (List<string>)Session["sports"];
16             if (sports != null)
17             {
18                 for (int i = 0; i < sports.Count; i++)
19                 {
20                     Label2.Text += "  " + sports[i];
21                 }
22             }
23             else num++;
24             if (num == 2)
25             {
26                 Label3.Text = "购物车是空的,快去购物";
27 
28             }
29             else
30                 Label3.Text = "购物车里面有:";
31         }

    6)查看购物车时,如果没有购物,则会给予提示。

清空购物车:

 protected void Button1_Click(object sender, EventArgs e)
        {
            Label3.Text = "购物车是空的,快去购物";
            Label1.Text = "";
            Label2.Text = ""; 
        }

 

3.总结

    利用session存储对象,后期再修改一下做成数据库的。

 

posted @ 2019-01-14 15:38  一头  阅读(2738)  评论(0编辑  收藏  举报