2011-08-05 34 views
0

值哎,所以我有一個gridview如下ASP.NET填充的GridView文本框與會話的購物車

<asp:GridView ID="productListTable" runat="server" DataSourceID="srcProductListPerCustomer" AutoGenerateColumns="False" AlternatingRowStyle-CssClass="tr_dark" HeaderStyle-CssClass="header_req" BorderWidth="0px" GridLines="None" AllowPaging="true" PageSize="25" EmptyDataText="No records." AllowSorting="false" Width="100%" DataKeyNames="product_ID_key">   
    <Columns>       
      <asp:TemplateField HeaderText="Product Name" HeaderStyle-Width="250px" SortExpression="productName" ItemStyle-CssClass="product_name" > 
       <ItemTemplate> 
       <asp:Label ID="ProductNameField" runat="server" Text='<%# Eval("productName").ToString() %>'></asp:Label> 
       </ItemTemplate>      
      </asp:TemplateField> 

    </Columns> 
    <Columns> 

    <asp:TemplateField HeaderText="Quantity"> 
        <ItemTemplate> 
         <asp:TextBox runat="server" ID="txtQuantity" Columns="5"></asp:TextBox><br /> 
         <asp:LinkButton runat="server" ID="btnRemove" Text="Remove" CommandName="Remove" CommandArgument='<%# Eval("product_ID_key") %>' style="font-size:12px;"></asp:LinkButton> 
        </ItemTemplate> 
       </asp:TemplateField> 
    </Columns> 
    <HeaderStyle CssClass="header_req" /> 
    <AlternatingRowStyle CssClass="tr_dark" /> 
    <PagerStyle CssClass="pagination" /> 
    <PagerSettings PageButtonCount="3" FirstPageText="First" LastPageText="Last" NextPageText="Next" PreviousPageText="Previous" Mode="NumericFirstLast" />  
</asp:GridView> 

然後我也有具有CartItems會話的購物車,這本身工作正常。現在我想要做的是,當用戶將產品添加到他們的購物車,並在預先填充gridview中的數量字段文本框的頁面與兩個產品匹配的cartitems數量的值之間切換時,即我正在嘗試

Protected Sub Page_PreRender(ByVal s As Object, ByVal e As EventArgs)Handles Me.PreRender 
    'code here 
    For i As Integer = 0 To Me.productListTable.Rows.Count - 1 
     Dim txtQuantity As TextBox = CType(productListTable.Rows(i).FindControl("txtQuantity"), TextBox) 

     Dim productId As String = productListTable.DataKeys(i).Value 
     ' Find the item and update the quantity 
     Dim updatedItem = New CartItem(productId) 

     For Each item As CartItem In ShoppingCart.Instance.Items 
      If item.Equals(updatedItem) Then 
       txtQuantity.Text = item.Quantity 
      End If 
     Next 
    Next 
End Sub 

,它實際上似乎初審工作(本書雖然是林不知道上​​面是做一個好辦法,但問題是在GridView上我得到的錯誤使用分頁時

索引超出範圍必須爲非負值且小於集合的大小 參數名稱索引

上線

Dim productId As String = productListTable.DataKeys(i).Value 

任何想法,爲什麼我收到此錯誤,也有沒有更好的辦法做到這一點?

回答

0

可能是因爲不是所有的行向具有datakey,只包含數據的行(所以不是你headerrow,頁腳等) 嘗試添加此條件:

If productListTable.Rows(i).RowType = DataControlRowType.DataRow Then 
    Dim txtQuantity As TextBox = CType(productListTable.Rows(i).FindControl("txtQuantity"), TextBox) 

    Dim productId As String = productListTable.DataKeys(i).Value 
    ' Find the item and update the quantity 
    Dim updatedItem = New CartItem(productId) 

    For Each item As CartItem In ShoppingCart.Instance.Items 
     If item.Equals(updatedItem) Then 
      txtQuantity.Text = item.Quantity 
     End If 
    Next 

End If 

這將是更好不過來把這種代碼在的RowDataBound事件處理程序,這樣,你總是相信你處理所有的行,他們將被添加到電網後直接:

Protected Sub productListTable_RowDataBound(ByVal s As Object, ByVal e As GridViewRowEventArgs) 
    If e.Row.RowType = DataControlRowType.DataRow Then 
     Dim txtQuantity As TextBox = CType(e.Row.FindControl("txtQuantity"), TextBox) 

     Dim productId As String = productListTable.DataKeys(e.Row.RowIndex).Value 
     ' Find the item and update the quantity 
     Dim updatedItem = New CartItem(productId) 

     For Each item As CartItem In ShoppingCart.Instance.Items 
      If item.Equals(updatedItem) Then 
       txtQuantity.Text = item.Quantity 
      End If 
     Next 

    End If 
End Sub 
+0

酷生病嘗試,在一個側面說明這裏是最好的地方放置一個像這樣的函數,即page_load,page_prerender et C甚至可能像RowDataBound? – StevieB

+0

Nah仍然收到錯誤「索引超出範圍,必須是非負數,並小於集合的大小。」 參數名稱:索引「On Dim productId As String = productListTable.DataKeys(i).Value,when I使用第一頁加載時的分頁效果很好。 – StevieB

+0

當我將代碼從page_prerender切換到page_load時,嘗試分頁時出現錯誤似乎消失了。但是現在,我正在執行perfomancing問題,其中回發值始終顯示,但當我刷新頁面時,它們是 – StevieB