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
任何想法,爲什麼我收到此錯誤,也有沒有更好的辦法做到這一點?
酷生病嘗試,在一個側面說明這裏是最好的地方放置一個像這樣的函數,即page_load,page_prerender et C甚至可能像RowDataBound? – StevieB
Nah仍然收到錯誤「索引超出範圍,必須是非負數,並小於集合的大小。」 參數名稱:索引「On Dim productId As String = productListTable.DataKeys(i).Value,when I使用第一頁加載時的分頁效果很好。 – StevieB
當我將代碼從page_prerender切換到page_load時,嘗試分頁時出現錯誤似乎消失了。但是現在,我正在執行perfomancing問題,其中回發值始終顯示,但當我刷新頁面時,它們是 – StevieB