hashtable的使用,作為資料來源綁定資料,session購物車的原理

來源:互聯網
上載者:User
Code
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections;
public partial class order : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void ok_Click(object sender, EventArgs e)
    {
        addCart();
    }
    protected void addCart()
    {
        if (Session["name"] == null)
        {
         Session["name"] = name.Text;
         Session["price"] = price.Text;
        }
        else
        {
            string str1 = Session["name"].ToString();
            str1 = str1 +"|"+ name.Text;
            Session["name"] = str1;
            str1 = Session["price"].ToString();
            str1 = str1 + "|" + price.Text;
            Session["price"] = str1;
        }
        bind();
    }
    protected void bind()
    {
        Hashtable ht = new Hashtable();
        string str = Session["name"].ToString();
        string str2 = Session["price"].ToString();
        string[] listname = str.Split('|');
        string[] listprice=str2.Split('|');
        for (int i = 0; i < listname.Length; i++)
        { Response.Write(listname[i]); 
            Response.Write(listprice[i]);
            try
            {
                ht.Add(listname[i], listprice[i]);
            }
            catch (Exception ex)
            {
                Response.Write("exist");
            }
        }
        Repeater1.DataSource = ht;
        Repeater1.DataBind();
    }

}前面aspx代碼
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="order.aspx.cs" Inherits="order" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:TextBox runat="server" ID="name"></asp:TextBox>
    <asp:TextBox runat="server" ID="price"></asp:TextBox>
    <asp:Button ID="ok" Text="submit" runat="server" OnClick="ok_Click" />
    <ul>
        <asp:Repeater ID="Repeater1" runat="server">
        <ItemTemplate>
        <li><%#Eval("key")%><%#Eval("value")%></li>
        </ItemTemplate>
        </asp:Repeater>
        </ul>
    </div>
    </form>
</body>
</html>

很弱智的說~

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.