asp.net 4.5 练习~test6-7

webform1.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="test6_7.WebForm1" %>

<!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:Button ID="Button1" runat="server" Text="清空购物车" OnClick="Button1_Click" />&nbsp;
        <asp:Button ID="Button2" runat="server" Text="添加" OnClick="Button2_Click" /><br />
        <asp:Label ID="Label1" runat="server" Text="" ForeColor="Blue"></asp:Label>
    </div>
    </form>
</body>
</html>

webform1.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace test6_7
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Label1.Text = "商品数量:0";
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Session["ItemCount"] = 0;
            Label1.Text = "商品数量:" + Session["ItemCount"];
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            if (Session["ItemCount"] != null)
            {
                int i = int.Parse(Session["ItemCount"].ToString());
                i++;
                Session["ItemCount"] = i;
            }
            else
            {
                Session["ItemCount"] = 1;
            }

            Label1.Text = "商品数量:" + Session["ItemCount"];
        }
    }
}

猜你喜欢

转载自blog.csdn.net/modern358/article/details/114327507