一个简单ASP.NET购物车设计

思路:

  1. 创建一个多选列表

  2. 在cs文件里初始化购物车会话变量,同,创建一个新的 List<string> 并将其赋值给会话状态中的 "Cart" 键--(利用Session)

    markdown 复制代码
    Session 是一种用于存储用户特定信息的对象,这些信息可以在多个请求之间保持(注意:初始化 Session["Cart"],可以确保在任何时候使用它时都不会遇到空引用问题)
  3. 检查并处理会话状态中购物车为空的情况

  4. 添加商品按键(注:当再次点击时,重复的商品将不再添加)

  5. 清空购物车

  6. 若想拓展(可以使用 CheckBoxListRepeater 控件来显示带有图片的商品列表)

Goods1.aspx代码

html 复制代码
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Goods1.aspx.cs" Inherits="Goods1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:CheckBoxList ID="cblGood" runat="server">
                <asp:ListItem>苹果</asp:ListItem>
                <asp:ListItem>梨</asp:ListItem>
                <asp:ListItem>香蕉</asp:ListItem>
                <asp:ListItem>猕猴桃</asp:ListItem>
                <asp:ListItem>黄瓜</asp:ListItem>
                <asp:ListItem>白菜</asp:ListItem>
            </asp:CheckBoxList>
            <asp:Button ID="btnAdd" runat="server" Text="加入购物车" OnClick="btnAdd_Click"/>
            <asp:Button ID="btnView" runat="server" Text="查看购物车" OnClick="btnView_Click" />
            <asp:Button ID="btnClear" runat="server" Text="清空购物车"  OnClick="btnClear_Click"/><br/>
            <asp:Label ID="lblShow01" runat="server" ></asp:Label>
        </div>
    </form>
</body>
</html>

Goods1.aspx.cs代码

c# 复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Goods1 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            // 初始化购物车会话变量
            if (Session["Cart"] == null)
            {
                Session["Cart"] = new List<string>();
            }
        }
    }
    

    

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        List<string> cart = (List<string>)Session["Cart"];
        foreach (ListItem item in cblGood.Items)
        {
            if (item.Selected && !cart.Contains(item.Value))
            {
                cart.Add(item.Value);
            }
        }
        Session["Cart"] = cart;
        lblShow01.Text = "已加入购物车";
    }

    protected void btnView_Click(object sender, EventArgs e)
    {
        List<string> cart = (List<string>)Session["Cart"];
        if (cart.Count > 0)
        {
            lblShow01.Text = "购物车中的商品:" + string.Join(", ", cart);
        }
        else
        {
            lblShow01.Text = "购物车为空";
        }
    }



    protected void btnClear_Click(object sender, EventArgs e)
    {
        Session["Cart"] = new List<string>();
    }
}
相关推荐
LZQqqqqo1 小时前
C# _列表(List<T>)_ 字典(Dictionary<TKey, TValue>)
windows
Channing Lewis3 小时前
zoho crm为什么xx是deal的关联对象但是调用函数时报错说不是关联对象
运维·服务器·windows
李小咖3 小时前
第2章 cmd命令基础:常用基础命令(1)
windows·网络安全·cmd·cmd命令·李小咖
Digitally4 小时前
6种将iPhone照片传输到Windows 10电脑的方法
windows·电脑·iphone
sean9084 小时前
Chrome 提示 “此扩展程序不再受支持”(MacOS/Windows)
chrome·windows·macos·扩展·支持
SBFE6 小时前
出现错误,Microsoft store初始化失败。请尝试刷新或稍后返回。
microsoft
小乖兽技术8 小时前
C#开发基础之深入理解“集合遍历时不可修改”的异常背后的设计
开发语言·windows·c#
gb42152879 小时前
负载均衡算法中的加权随机算法
windows·算法·负载均衡
未出道的小弟10 小时前
图形界面应用程序技术栈大全
windows
java叶新东老师1 天前
解决windows系统下 idea、CLion 控制台中文乱码问题
java·windows·intellij-idea