2012-05-14 26 views
2

我一直在探索如何解決特定問題。我需要創建一個購物車頁面,用戶可以在其中設置其數量或從購物車中移除該特定物品。我決定使用Repeater。和值從數據庫得到使用中繼器購物車

我的代碼如下

<asp:Repeater ID="RepeatStatus" runat="server" OnItemDataBound="RepeatStatus_ItemDataBound" onitemcommand="Button_ItemCommand" EnableViewState="False"> 
        <HeaderTemplate><tr style="color: #FFFFFF; background-color: #3D7169"> 
        <td>Product Name</td> 
        <td>Quantity</td> 
        <td>Price</td> 
        <td>Total</td> 
        </tr></HeaderTemplate> 
        <ItemTemplate> 
         <tr> 
         <td><%#DataBinder.Eval(Container,"DataItem.ProductName")%></td> 
         <td><asp:TextBox ID="txtQty" runat="server" Height="23px" Width="50px" Text=<%#DataBinder.Eval(Container,"DataItem.Quantity")%>></asp:TextBox><br /> 
         <asp:LinkButton runat="server" ID="btnRemove" Text="Remove" CommandName="Remove" CommandArgument='<%# Eval("CDetailsID") %>' style="font-size:12px;"></asp:LinkButton> 
         </td>            
         <td><asp:Label ID="lblPrice" runat="server" Text=<%#DataBinder.Eval(Container,"DataItem.Price")%>></asp:Label></td> 
         <td><asp:Label ID="lblTotal" runat="server"></asp:Label></td> 
         </tr> 
        </ItemTemplate> 
        <FooterTemplate> 
        <br /> 
         <tr style="color: #FFFFFF; background-color: #6C6B66"> 
         <td colspan="4" class="cartTotal">Final Price: </td> 
         </tr> 
         <tr><td colspan="4"> 
        <asp:Label ID="lblEmptyData" Text="No Data Found" runat="server" Visible="false" Font-Bold="True"> 
            </asp:Label></td></tr> 
            <tr><td colspan="4"> 
         <asp:Button ID="btnUpdate" runat="server" Text="Update" /> 
         </td></tr>  
           </FooterTemplate> 
       </asp:Repeater> 

我的代碼背後,是這個

protected void RepeatStatus_ItemDataBound(object sender, RepeaterItemEventArgs e) 
    { 
     int q; 
     decimal p = 0, t = 0; 
     int j = RepeatStatus.Items.Count; 
     if (RepeatStatus.Items.Count > 0) 
     { 
      foreach (RepeaterItem ri in RepeatStatus.Items) 
      { 
       if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) 
       { 
        Label price = (Label)e.Item.FindControl("lblPrice"); 
        TextBox qty = (TextBox)e.Item.FindControl("txtQty"); 
        Label total = (Label)e.Item.FindControl("lblTotal"); 
        q = Convert.ToInt32(qty.Text); 
        p = Convert.ToDecimal(price.Text.Substring(1)); 
        t = (p * q); 
        total.Text = "$" + t.ToString(); 
       } 
      } 
     } 

     if (RepeatStatus.Items.Count < 1) 
     { 
      if (e.Item.ItemType == ListItemType.Footer) 
      { 
       Label lblFooter = (Label)e.Item.FindControl("lblEmptyData"); 
       lblFooter.Visible = true; 
       Button btnU = (Button)e.Item.FindControl("btnUpdate"); 
       btnU.Visible = false; 
      } 
     } 
    } 

出於某種原因,我一直在努力地試圖發現,是該第一個記錄將永遠不會計算產品的子定價。但是第二條記錄和以後將正確顯示值。我做錯了什麼,或者我應該改變它使用Datalist?

+0

您是否嘗試過使用一個GridView呢? – madatanic

+0

...或一個ListView。 – BeemerGuy

+0

他的轉發器沒有什麼問題..我不明白爲什麼他應該使用gridview或listview – Thousand

回答

1

ItemDataBound對於列表中的每個項目都會調用,所以您只需要將計算限制爲只是該項目而不是所有項目,就像您現在要做的那樣。 其次,您似乎從呈現的數據元素中獲取值,而不是數據項本身。如果可以,請在綁定到Repeater(或其他人建議的GridViewListView)之前計算服務器上的總計。

0

disculpa佩羅英里英格爾沒有ES MUY布埃諾佩羅puedes hacer ESTO

昏暗的按鈕作爲按鈕= CTYPE(發件人,巴頓)

'Get the command argument 
    Dim commandArgument As String = button.CommandArgument 

    'Get the Repeater Item reference 
    Dim item As RepeaterItem = CType(button.NamingContainer, RepeaterItem) 

    'Get the repeater item index 
    Dim index As Integer = item.ItemIndex 

    Dim cantidad As TextBox = (CType(MyList.Items(index).FindControl("Quantity"), TextBox)) 
    Dim contador As Integer = 0 

    contador = Convert.ToInt32(cantidad.Text) 
    contador += 1 
    '  'Dim countryName As String = CountryLabel.Text 
    cantidad.Text = contador.ToString() 
+1

請只使用英文 – HaveNoDisplayName