【问题标题】:ASP.NET Session Shopping Cart Items issue on pageload not updating页面加载时的 ASP.NET 会话购物车项目问题未更新
【发布时间】:2011-10-20 00:26:43
【问题描述】:

即我的页面上有一段代码如下,在page_load中

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()

然后我可以按如下方式将商品添加到我的购物车

Protected Sub Update_Click(ByVal sender As Object, ByVal e As EventArgs) Dim rowscount As Integer = 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

当会话中实际上有 1 个项目时,我添加了一个页面仍然显示 0 个项目的产品 我刷新了计数器转到的页面

如何读取正确的会话数?

【问题讨论】:

    标签: asp.net


    【解决方案1】:

    Page_Load 将在Update_Click 之前触发,这就是为什么在初始表单提交后您没有看到计数增加的原因。您需要在Update_Click 执行后更新shoppingCartItems 控件,或者您可以Response.Redirect 返回页面以更新显示。我个人喜欢在发布后使​​用Response.Redirect,因为如果用户刷新页面,那么您将不会看到显示它将重新发布数据的浏览器消息。

    您可能还想查看ASP.NET Page Life Cycle

    【讨论】:

    • hey rsbarro question response.redirecting for every click 似乎资源很重还是我错了?另外如何在 update_click 之后更新 ShoppingCartItems 我以为我是在 Update_Click 方法中更新它,即 ShoppingCart.Instance.AddItem(productId, quantity, price, productName, packSize, stockIndicator)
    • 啊,将显示会话计数的代码放在 Page_PreRender 中似乎已经成功了
    • @StevieB:我认为重定向方法不会占用大量资源。我认为这是改善用户体验的必要条件,因为该重新发布对话框令人困惑(并且可以在多种情况下显示,例如页面刷新或返回按钮然后单击前进按钮)。 ASP.NET 中的标准方法是在 Page_Load 中绑定数据,同时考虑它是否是回发(即 if(!Page.IsPostBack) DataBindMyControls();)`。任何回发事件(例如单击按钮)都可以更新他们需要更新的任何内容,然后您只需 Response.Redirect 即可重新加载页面。
    【解决方案2】:

    我在购物车方面遇到了类似的问题,但我使用了 AJAX 更新面板,并且重定向无法以 AJAX 方式工作。

    在将商品添加到购物车后,我在代码中使用了mycartlistview.DataBind()

    【讨论】:

      猜你喜欢
      • 2020-05-09
      • 1970-01-01
      • 1970-01-01
      • 2021-09-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-08
      • 1970-01-01
      相关资源
      最近更新 更多