asp.net之購物車
阿新 • • 發佈:2018-11-13
buy1.aspx
<body> <form id="form1" runat="server"> <asp:CheckBox ID="C1" runat="server" Text="豬肉" /> <br /> <asp:CheckBox ID="C2" runat="server" Text="牛肉" /> <br /> <asp:CheckBox ID="C3" runat="server" Text="羊肉" /> <br /> <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="提交" /> <br /> <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="~/buy2.aspx">買點別的 </asp:HyperLink> <asp:HyperLink ID="HyperLink2" runat="server" NavigateUrl="~/cart.aspx">檢視購物車 </asp:HyperLink></form> </body>
buy1.aspx.cs
protected void Button1_Click(object sender, EventArgs e) { if (C1.Checked) Session["b1"] = C1.Text; else Session["b1"] = null; if (C2.Checked) Session["b2"] = C2.Text; else Session["b2"] = null; if (C3.Checked) Session["b3"] = C3.Text; else Session["b3"] = null; }
buy2.aspx
<body> <form id="form1" runat="server"> <asp:CheckBox ID="C4" runat="server" Text="籃球" /><br /> <asp:CheckBox ID="C5" runat="server" Text="足球" /><br /> <asp:CheckBox ID="C6" runat="server" Text="排球" /><br /> <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="提交" /> <br /> <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="~/buy1.aspx">買點別的 </asp:HyperLink> <asp:HyperLink ID="HyperLink2" runat="server" NavigateUrl="~/cart.aspx">檢視購物車 </asp:HyperLink></form> </body>
buy2.aspx.cs
protected void Button1_Click(object sender, EventArgs e)
{
if (C4.Checked) Session["b4"] = C4.Text; else Session["b4"] = null;
if (C5.Checked) Session["b5"] = C5.Text; else Session["b5"] = null;
if (C6.Checked) Session["b6"] = C6.Text; else Session["b6"] = null;
}
cart.aspx
<body>
<form id="form1" runat="server">
您的購物車:<br />
<%for(int i=0;i<Session.Count;i++)
if(Session[i]!=null) Response.Write(Session[i].ToString() + "<br>");
%>
<br />
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="~/buy1.aspx">返回
</asp:HyperLink>
</form>
</body>