hashtable的使用,作为数据源绑定数据,session购物车的原理

ContractedBlock.gif ExpandedBlockStart.gif 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();
    }

}
ContractedBlock.gif ExpandedBlockStart.gif 前面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>

很弱智的说~

转载于:https://www.cnblogs.com/daiye/archive/2009/08/26/1554216.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值