一个简单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>();
    }
}
相关推荐
开开心心就好1 小时前
微软官方出品:免费数据恢复工具推荐
网络·笔记·microsoft·pdf·word·音视频·symfony
alphaTao7 小时前
LeetCode 每日一题 2025/11/3-2025/11/9
windows·leetcode
习惯就好zz13 小时前
WSL2 安装Ubuntu卡在安装进度0%无响应问题解决
linux·windows·ubuntu·wsl·wsl2
仰望—星空17 小时前
MiniEngine学习笔记 : CommandListManager
c++·windows·笔记·学习·cg·direct3d
ue星空19 小时前
Windows内核函数使用
windows
业余幻想家21 小时前
Windows10/Windows11家庭版系统关闭自动更新
运维·windows
阿猿收手吧!1 天前
windows本机vscode通过ssh免密登录远程linux服务器 && git push/pull 免密
服务器·windows·vscode
zxm85131 天前
如何在Windows系统中加入程序自启动
windows
~~李木子~~1 天前
Windows软件自动扫描与分类工具 - 技术文档
windows·分类·数据挖掘
-指短琴长-1 天前
Qt的下载和安装【Windows】
开发语言·windows·qt