hashtable的使用,作为数据源绑定数据,session购物车的原理
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();
}
}
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>
<%@ 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>
很弱智的说~