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

思路:

  1. 创建一个多选列表

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

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

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

  5. 清空购物车

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

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 小时前
Effective Python 条款43:自定义容器类
开发语言·windows·python·pandas·scikit-learn·pyqt
wdfk_prog6 小时前
Wi-Fi Direct 真机实践:JOIN 建链耗时优化
服务器·asp.net·php
解道Jdon9 小时前
Rust重写OpenCode Rune:性能提升最高100倍
ide·windows·git·svn·eclipse·github·visual studio
Frag0ut1 天前
开源下载管理器MDM下载获取与实操指南:IDM的上位替代
windows·macos·mdm·下载管理器·断点续传·开源下载工具·idm替代
the3clipse1 天前
Windows 11 26H2 任务栏与开始菜单深度自定义指南:位置、图标、布局全掌握
windows·使用技巧·开始菜单·win11 26h2·任务栏设置·任务栏自定义
SeanQhl1 天前
中文 Windows 上跑 Python 定时任务——编码、路径、控制台三个坑
windows·python·编码·效率工具·定时任务
Vex2une1 天前
系统出问题不想重装?系统还原点创建与重置此电脑完整指南
windows·系统还原点·重置此电脑·系统还原怎么用·创建还原点·保留文件重置·系统保护开启
保卫大狮兄2 天前
2026中小制造企业轻量化ERP选型观察:产供销协同路线、上线周期与ROI分析
windows·microsoft·erp·生产管理
陈工大模型2 天前
MacOS 跨平台压缩包处理:从编码痛点到工程化落地的完整方案
windows·macos·缓存·电脑·策略模式·c盘清理
凌镜icecraft3 天前
我改文件扩展名的时候那个警告弹窗再也不会弹出来了
windows·c#·wpf·explorer