这是我的“加入购物车”系统,当多次加入同一个商品时,(数量和总价计算都正常),但如果加入了另一个商品时,就出问题了。DataTable 中总会多出不需要的行。
请大家帮我看看。急用!protected void BtnAddToCar_Click(object sender, EventArgs e)
{
int goodsId = Int32.Parse(this.DetailsView1.Rows[0].Cells[1].Text);
string theGoodsName = (this.DetailsView1.Rows[1].Cells[1].Text).ToString();
decimal thePrice = Convert.ToDecimal(this.DetailsView1.Rows[5].Cells[1].Text);//DataTable dtb = Session["myCar"] as DataTable;if (Session["myCar"] == null)
{
DataTable dtb = new DataTable();
dtb.Columns.Add("goodsId", typeof(int));
dtb.Columns.Add("thtGoodsName");
dtb.Columns.Add("thePrice");
dtb.Columns.Add("TheCount");
dtb.Columns.Add("TotalPrice");
DataRow dRow = dtb.NewRow();
dRow["goodsId"] = goodsId;
dRow["thtGoodsName"] = theGoodsName;
dRow["thePrice"] = thePrice;
dRow["TheCount"] = 1;
dRow["TotalPrice"] = thePrice;
dtb.Rows.Add(dRow);
Session["myCar"] = dtb;}else
{
DataTable dtb = Session["myCar"] as DataTable;
for (int i = 0; i < dtb.Rows.Count; i++)
{
if (Convert.ToInt32(dtb.Rows[i][0]) == goodsId)
{
DataRow oldDR;
oldDR = dtb.Rows[i];
oldDR["TheCount"] = Int32.Parse(oldDR["TheCount"].ToString()) + 1;
oldDR["thePrice"] = thePrice;oldDR["TotalPrice"] = thePrice * Int32.Parse(oldDR["TheCount"].ToString());
}
else
{
DataRow dRow = dtb.NewRow();
dRow["goodsId"] = goodsId;
dRow["thtGoodsName"] = theGoodsName;
dRow["thePrice"] = thePrice;
dRow["TheCount"] = 1;
dRow["TotalPrice"] = thePrice;
dtb.Rows.Add(dRow);
}
Session["myCar"] = dtb;
}
}
Response.Redirect("myCar.aspx");
}

解决方案 »

  1.   

    好像逻辑上出现了问题。你使用for循环来比较原来DataTable里面的每一个ID,如果相同,则增加,但是,如果这个ID是一个新的ID,原来有3种产品,循环就会运行3次,每次比较都不同,就会变成每次都自动增加……要去开会了,晚上再回来看看。如有不对楼下请纠正。
      

  2.   

    这里改为
    else if ((i+1)==dtb.Rows.Count)
    {
    DataRow dRow = dtb.NewRow();
    dRow["goodsId"] = goodsId;
    dRow["thtGoodsName"] = theGoodsName;
    dRow["thePrice"] = thePrice;
    dRow["TheCount"] = 1;
    dRow["TotalPrice"] = thePrice;
    dtb.Rows.Add(dRow);
    }
    }
    Session["myCar"] = dtb;
    }
      

  3.   


    非常感谢 ezhuyin(碧海蓝天) 现在问题解决了! DataTable dtb = Session["myCar"] as DataTable;
                bool isInCart = false;
                for (int i = 0; i < dtb.Rows.Count; i++)
                {
                    if (Convert.ToInt32(dtb.Rows[i][0]) == goodsId)
                    {
                        DataRow oldDR;
                        oldDR = dtb.Rows[i];
                        oldDR["TheCount"] = Int32.Parse(oldDR["TheCount"].ToString()) + 1;
                        //oldDR["thePrice"] = thePrice;
                        oldDR["TotalPrice"] = thePrice * Int32.Parse(oldDR["TheCount"].ToString());                    isInCart = true;
                        break;
                    }
                }
                if (!isInCart)
                {
                    DataRow dRow = dtb.NewRow();
                    dRow["goodsId"] = goodsId;
                    dRow["thtGoodsName"] = theGoodsName;
                    dRow["thePrice"] = thePrice;
                    dRow["TheCount"] = 1;
                    dRow["TotalPrice"] = thePrice;
                    dtb.Rows.Add(dRow);
                }
                Session["myCar"] = dtb;这个问题现在解决了。我还要用myCart.ASPX 页面将数据显示出来,并再GRVIDVIEW 中显示修改 数量和 删除按钮。还需要您的帮忙。谢谢!
      

  4.   

    这个问题,可以参考孟子E章的几篇文章。http://dotnet.aspx.cc/article/a933b187-06c3-4263-9eec-414a54d9c815/read.aspxhttp://dotnet.aspx.cc/article/2cb1c6d7-2b22-4655-8922-2a8a053a66fa/read.aspx
      

  5.   

    接着上面的问题:现在我要更新购物车资料,可怎么也得不到 textbox 中的数量。不管能解决否,我都会尽快给大家结分!谢谢大家。
    protected void btnUpdateCart_Click(object sender, EventArgs e)
    {
    DataTable dtb = (DataTable)Session["myCar"];
    foreach (GridViewRow grw in this.GridView2.Rows)
    {int i = grw.RowIndex;
    TextBox TheGoodsCount = (TextBox)grw.FindControl("txtCount");int theCount = Int32.Parse(TheGoodsCount.Text);DataRow UpdateDR;
    UpdateDR = dtb.Rows[i];
    Decimal thePrice = Convert.ToDecimal(dtb.Rows[i]["thePrice"]);
    UpdateDR["TheCount"] = theCount;
    UpdateDR["TotalPrice"] = thePrice * theCount;Session["myCar"] = dtb;
      

  6.   

    前面GridView2定义的代码呢?断点在DataRow UpdateDR;这句,然后看TheGoodsCount和theCount这两个量是否正确。
      

  7.   

    GridView2 没有什么定义的代码,就只是在页面中拖进了一个GridView 控件,名为GridView2。利用模板定义好并绑定数据后,能正常显示各行数据。包括 TEXTBOX 中显示各项商品的数目。
      

  8.   

    你里面一定有类似这样的代码
    <Columns>                  
      <asp:BoundField DataField="EmployeeID" HeaderText="Employee ID" ReadOnly="true"/>                    
      <asp:BoundField DataField="FirstName"  HeaderText="First Name"/>
      <asp:BoundField DataField="LastName"   HeaderText="Last Name"/>                    
      <asp:TemplateField HeaderText="Birth Date">
        <ItemTemplate> 
          <asp:Label ID="BirthDateLabel" Runat="Server" 
                     Text='<%# Eval("BirthDate", "{0:d}") %>' />
        </ItemTemplate>
        <EditItemTemplate>
          <asp:Calendar ID="EditBirthDateCalendar" Runat="Server"
                        VisibleDate='<%# Eval("BirthDate") %>'
                        SelectedDate='<%# Bind("BirthDate") %>' />
        </EditItemTemplate>
      </asp:TemplateField>                    
    </Columns>贴出来看看才知道。
      

  9.   

    <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" Height="205px"
                Width="540px">
                <Columns>
                    <asp:BoundField DataField="Id" HeaderText="序号" />
                    <asp:BoundField DataField="goodsId" HeaderText="商品ID" />
                    <asp:BoundField DataField="thtGoodsName" HeaderText="商品名称" />
                    <asp:BoundField DataField="thePrice" HeaderText="单价" />
                    <asp:TemplateField HeaderText="数量">
                        <ItemTemplate>
                            <asp:TextBox ID="txtCount" runat="server" Text='<%# Eval("TheCount") %>' Width="91px" EnableViewState="False"></asp:TextBox>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:BoundField DataField="TotalPrice" HeaderText="单项总价" />
                    <asp:TemplateField HeaderText="删除">
                        <ItemTemplate>
                            <asp:Button ID="btnDelTheGoods" runat="server" CommandArgument='<%# Eval("goodsId") %>'
                                Text="删除" Width="59px" />
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
            <asp:Button ID="btnUpdateCart" runat="server" OnClick="btnUpdateCart_Click" Text="更新购物车" />
      

  10.   

    这是实现商品数量修改的代码,不知哪儿有问题,总取不出 TextBox 中输入的值:protected void btnUpdateCart_Click(object sender, EventArgs e)
    {
    DataTable dtb = (DataTable)Session["myCar"];foreach (GridViewRow grw in this.GridView2.Rows)
    {int i = grw.RowIndex;int theCount = Convert.ToInt32(((TextBox)this.GridView2.Rows[i].Cells[4].FindControl("txtCount")).Text.ToString());DataRow UpdateDR;
    UpdateDR = dtb.Rows[i];
    Decimal thePrice = Convert.ToDecimal(dtb.Rows[i]["thePrice"]);
    UpdateDR["TheCount"] = theCount;
    //oldDR["thePrice"] = thePrice;
    UpdateDR["TotalPrice"] = thePrice * theCount;
    Session["myCar"] = dtb;
    Context.Server.Transfer(Request.CurrentExecutionFilePath);}
    }以下是: aspx 页面代码:<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" Height="205px"
    Width="540px">
    <Columns>
    <asp:BoundField DataField="Id" HeaderText="序号" />
    <asp:BoundField DataField="goodsId" HeaderText="商品ID" />
    <asp:BoundField DataField="thtGoodsName" HeaderText="商品名称" />
    <asp:BoundField DataField="thePrice" HeaderText="单价" />
    <asp:TemplateField HeaderText="数量">
    <ItemTemplate>
    <asp:TextBox ID="txtCount" runat="server" Text='<%# Eval("TheCount") %>' Width="91px" EnableViewState="False"></asp:TextBox>
    </ItemTemplate>
    </asp:TemplateField>
    <asp:BoundField DataField="TotalPrice" HeaderText="单项总价" />
    <asp:TemplateField HeaderText="删除">
    <ItemTemplate>
    <asp:Button ID="btnDelTheGoods" runat="server" CommandArgument='<%# Eval("goodsId") %>'
    Text="删除" Width="59px" />
    </ItemTemplate>
    </asp:TemplateField>
    </Columns>
    </asp:GridView>
    <asp:Button ID="btnUpdateCart" runat="server" OnClick="btnUpdateCart_Click" Text="更新购物车" />
    购物车的结构如下:dtb.Columns.Add("Id", typeof(Int32));
    dtb.Columns[0].AutoIncrement = true;
    dtb.Columns[0].AutoIncrementSeed = 1;
    dtb.Columns.Add("goodsId", typeof(Int32));
    dtb.Columns.Add("thtGoodsName");
    dtb.Columns.Add("thePrice");
    dtb.Columns.Add("TheCount");
    dtb.Columns.Add("TotalPrice");
      

  11.   

    <asp:TemplateField HeaderText="数量">
    <ItemTemplate>
    <asp:TextBox ID="txtCount" runat="server" Text='<%# Eval("TheCount") %>' Width="91px" EnableViewState="False"></asp:TextBox>
    </ItemTemplate>
    </asp:TemplateField>
    <asp:BoundField DataField="TotalPrice" HeaderText="单项总价" />
    <asp:TemplateField HeaderText="删除">
    <ItemTemplate>
    问题就在这里,ItemTemplate字段里面的似乎是无法被读到的,它只是作为一个"template"(模版)而存在。
      

  12.   

    试试看这样拿
    int theCount = Convert.ToInt32(((TextBox)this.GridView2.Rows[i].Cells[4].FindControl("txtCount")).Text.ToString());
    改为
    int theCount = int.Parse(grw.Cells[4].Text);
      

  13.   

    如果不可以就用
    int theCount = int.Parse(((TextBox)grw.FindControl("txtCount")).Text);
      

  14.   

    感谢碧海蓝天,问题解决了。谢谢!其它都没问题,只 是应该键GV 的在 PAGELOAD 中的绑定 放在IF(!ISPOSTBACK) 中。