| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 654 人关注过本帖
标题:ASP.NET 2.0 购物车编写中的困惑
只看楼主 加入收藏
飞鱼
Rank: 1
等 级:新手上路
帖 子:38
专家分:0
注 册:2006-2-27
收藏
 问题点数:0 回复次数:1 
ASP.NET 2.0 购物车编写中的困惑

刚学习ASP.NET不久,这是我写的一个简单的购物车程序,上面只有两个GridView控件和一个LinkButton按钮,显示图书的GridView1控件中有一个模板列增加一个购买按钮,单击购买按钮可以购买一次,单击查看购物车也可以看到购买的信息,不过再次点击购买第二次的时候,或者购买同一商品第二次,再查看购物车时GridView2中没有了任何购物信息,若有高手对此有兴趣,可以帮忙一起研究一下,给些实质性的修改意见,最好能附加上代码,让大家共同学习,不盛感激!

//前台代码
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ProductList.aspx.cs" Inherits="_Default" %>

<%@ Register Assembly="Microsoft.Web.Atlas" Namespace="Microsoft.Web.UI" TagPrefix="cc1" %>

<!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>简单的购物车</title>
</head>
<body>
<form id="form1" runat="server">
<div style="text-align: left">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Height="108px"
ShowHeader="False" Width="728px" OnRowCommand="GridView1_RowCommand" DataKeyNames="BookId">
<Columns>
<asp:ImageField DataImageUrlField="BookImg" >
<ItemStyle HorizontalAlign="Center" />
</asp:ImageField>
<asp:BoundField DataField="BookId" Visible="False" />
<asp:BoundField DataField="BookName" />
<asp:BoundField DataField="BookAuthor" />
<asp:BoundField DataField="BookPrice" />
<asp:TemplateField>
<ItemStyle HorizontalAlign="Center" />
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="buy" CommandArgument='<%# Eval("BookId") %>'>购 买</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
&nbsp; &nbsp;
<br />

<asp:LinkButton ID="LinkButton2" runat="server" OnClick="LinkButton2_Click" Width="81px">查看购物车</asp:LinkButton>
<br />
<br />
<asp:GridView ID="GridView2" runat="server" Height="153px" Width="744px">
</asp:GridView>
</div>
</form>
</body>
</html>
==========================================
//后台程序代码
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
SqlConnection myConn = new SqlConnection("server=.;uid=sa;pwd=;database=book");
myConn.Open();
SqlCommand selCmd = new SqlCommand("select * from shop_book", myConn);
SqlDataReader sdr = selCmd.ExecuteReader();
this.GridView1.DataSource = sdr;
this.GridView1.DataBind();
myConn.Close();
}
}

private DataTable createProductDT()//建立购物车
{
DataTable dtProducts = new DataTable();
DataColumn productColumn = new DataColumn();
productColumn.DataType = System.Type.GetType("System.Int32");
productColumn.ColumnName = "id";
productColumn.Unique = true;
dtProducts.Columns.Add(productColumn);

productColumn = new DataColumn();
productColumn.DataType = System.Type.GetType("System.String");
productColumn.ColumnName = "name";
dtProducts.Columns.Add(productColumn);
productColumn = new DataColumn();
productColumn.DataType = System.Type.GetType("System.Decimal");
productColumn.ColumnName = "price";
dtProducts.Columns.Add(productColumn);
productColumn = new DataColumn();
productColumn.DataType = System.Type.GetType("System.Int32");
productColumn.ColumnName = "quantity";
dtProducts.Columns.Add(productColumn);

//使"id"成为主键
DataColumn[] pkColumns = new DataColumn[1];
pkColumns[0] = dtProducts.Columns["id"];
dtProducts.PrimaryKey = pkColumns;
return dtProducts;
}

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "buy")//GridView1中有一个购买的按钮
{
int BookId = Convert.ToInt32(e.CommandArgument);
AddProduct(BookId);
}
}

private void AddProduct(int BookId)//添加信息到购物车之中
{
DataTable dtProducts = createProductDT();
DataRow newRow = dtProducts.NewRow();
if (Session["dtProducts"] != null)
{
for (int i = 0; i < dtProducts.Rows.Count; i++)
{
if (Convert.ToInt32(dtProducts.Rows[i]["id"]) == BookId)
{
dtProducts.Rows[i]["quantity"] = Convert.ToInt32(dtProducts.Rows[i]["quantity"]) + 1;
break;
}
}
}
else
{
DataRow drProduct = dtProducts.Rows.Find(BookId);
DataRow tempTr = getBookDetails(BookId);
newRow[0] = BookId;
newRow[1] = tempTr["BookName"];
newRow[2] = tempTr["BookPrice"];
newRow[3] = 1;

dtProducts.Rows.Add(newRow);
}
//把产品存储到Session
Session["dtProducts"] = dtProducts;
}

public DataRow getBookDetails(int BookId)//获取数据库中图书的信息填充到DataTable中
{
SqlConnection myConn = new SqlConnection("server=.;uid=sa;pwd=;database=book");
myConn.Open();
string selCmd="select BookName,BookPrice from shop_book where BookId="+BookId;
SqlDataAdapter sda = new SqlDataAdapter(selCmd,myConn);
DataSet ds = new DataSet();
sda.Fill(ds);
return ds.Tables[0].Rows[0];
}
protected void LinkButton2_Click(object sender, EventArgs e)//单击这个按钮可以查看购物车中的信息
{
this.GridView1.Visible = false;
this.GridView2.DataSource = (DataTable)Session["dtProducts"];//用来显示购物车中的内容
this.GridView2.DataBind();
}

搜索更多相关主题的帖子: 购物车 NET ASP 困惑 编写 
2007-05-23 15:32
zidu
Rank: 1
等 级:新手上路
帖 子:23
专家分:0
注 册:2007-5-23
收藏
得分:0 

private void AddProduct(int BookId)//添加信息到购物车之中
{
DataTable dtProducts = createProductDT();
...
Session["dtProducts"] = dtProducts;
错误在这两行,你每次都将dtProducts重新初始化了,
应改为对DataTable dtProducts = (DataTable) Session["dtProducts"]的引用,知道思路就改代码吧,很简单的


2007-05-23 15:54
快速回复:ASP.NET 2.0 购物车编写中的困惑
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.077162 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved