2011-08-04 38 views
1

也就是說,我有一段代碼,我的頁面,如下所示,這是在Page_Load中ASP.NET會議購物車頁面上的項目問題沒有更新

Dim subTotal As Integer = 0 
    For Each item As CartItem In ShoppingCart.Instance.Items 
     subTotal += 1 
    Next 

    strShoppingCart.Append(subTotal).Append(" Items") 
    shoppingCartItems.Text = strShoppingCart.ToString() 

,然後我可以將項目添加到我的購物車如下

保護小組Update_Click(BYVAL發件人爲對象,BYVALË作爲EventArgs的) 昏暗rowscount爲整數= productListTable.Rows.Count

For Each row As GridViewRow In productListTable.Rows 
     If row.RowType = DataControlRowType.DataRow Then 
      ' We'll use a try catch block in case something other than a number is typed in. If so, we'll just ignore it. 
      Try 
       ' Get the productId from the GridView's datakeys 
       Dim productId = Convert.ToInt32(productListTable.DataKeys(row.RowIndex).Value) 
       ' Find the quantity TextBox and retrieve the value 
       Dim quantity = Integer.Parse(CType(row.Cells(1).FindControl("txtQuantity"), TextBox).Text) 
       'Dim price = Decimal.Parse(CType(row.Cells(1).FindControl("TradePriceField"), Label).Text) 
       Dim price = Decimal.Parse("16.00") 
       Dim productName = CType(row.Cells(1).FindControl("ProductNameField"), Label).Text 
       Dim packSize = Integer.Parse(CType(row.Cells(1).FindControl("PackSizeField"), Label).Text) 
       Dim stockIndicator = Integer.Parse(CType(row.Cells(1).FindControl("PackSizeField"), Label).Text) 
       ShoppingCart.Instance.AddItem(productId, quantity, price, productName, packSize, stockIndicator) 


      Catch ex As FormatException 

      End Try 
     End If 
    Next 
End Sub 

的問題是如下

頁面加載 個項目計數爲0

我添加產品頁面仍然說0項目時,居然有在會議 1項我刷新計數器進入的頁面,以一個

我怎樣才能從正確的會話數讀?

回答

2

Page_Load將在Update_Click之前觸發,這就是爲什麼您在初始表單提交後沒有看到計數增加的原因。在執行Update_Click之後,您需要更新shoppingCartItems控件,或者您可以將Response.Redirect回到頁面以使顯示更新。我個人喜歡在帖子後面使用Response.Redirect,因爲如果用戶刷新頁面,那麼您將不會看到表示要重新發布數據的瀏覽器消息。您也可以查看ASP.NET Page Life Cycle

+0

嘿rsbarro問題response.redirecting每一次點擊似乎非常資源重或我錯了嗎?另外我怎樣才能update_click更新ShoppingCartItems我以爲我正在更新Update_Click方法即ShoppingCart.Instance.AddItem(productId,數量,價格,productName,packSize,stockIndicator) – StevieB

+0

啊把我的代碼顯示會話計數在Page_PreRender似乎已經完成了這個技巧 – StevieB

+0

@StevieB:我不認爲重定向方法是資源沉重的。我認爲它是改善用戶體驗的必要條件,因爲轉貼對話框很混亂(並且可以在多種情況下顯示,例如頁面刷新或後退按鈕,然後轉發按鈕單擊)。 ASP.NET中的標準方法是綁定Page_Load中的數據,並考慮它是否爲回發(即if(!Page.IsPostBack)DataBindMyControls();)'。任何回發事件(比如按鈕點擊)都可以更新他們需要更新的任何內容,然後您可以直接使用Response.Redirect重新加載頁面。 – rsbarro

0

我有一個購物車類似的問題,但我使用AJAX更新面板和重定向不工作的AJAX方式。

我在代碼中使用了mycartlistview.DataBind(),在將商品添加到購物車後。