2011-08-04 3 views
1

Ie. 나는를 Page_LoadASP.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) 정수 = 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 

로서 어둡게 rowscount

페이지로드를,617 다음 문제는개 항목 세션 1 개 항목이 나는 올바른 세션 수에서 읽을 수 있습니다 어떻게

에 카운터가가는 페이지를 새로 고침 실제로있을 때 0

나는 페이지가 여전히 0 항목라는 제품을 추가한다 카운트 ?

답변

2

Page_LoadUpdate_Click 전에 실행되기 때문에 초기 양식 제출 후 카운트가 증가하지 않습니다. Update_Click 실행 후 shoppingCartItems 컨트롤을 업데이트하거나 디스플레이로 돌아가려면 Response.Redirect 페이지로 돌아 가야합니다. 개인적으로 게시물 이후에 Response.Redirect을 사용하고 싶습니다. 사용자가 페이지를 새로 고침하면 데이터를 다시 게시하라는 브라우저 메시지가 표시되지 않기 때문입니다.

ASP.NET Page Life Cycle을 확인할 수도 있습니다.

+0

안녕하세요 rsbarro 질문 답변 모든 클릭에 대한 응답은 매우 무거운 리소스로 보이거나 틀렸습니까? 또한 Update_Click 메서드를 사용하여 ShoppingCartItems를 업데이트 한 다음 어떻게 할 수 있습니까? 예를 들어 ShoppingCart.Instance.AddItem (productId, quantity, price, productName, packSize, stockIndicator) – StevieB

+0

Page_reRender의 세션 수를 표시하는 코드를 배치하는 것은 Update_Click 메서드 트릭을 완료했습니다 – StevieB

+0

@StevieB : 리디렉션 접근 방식이 리소스가 무겁다고 생각하지 않습니다. 재 게시 대화 상자가 혼란스럽고 (페이지 새로 고침이나 뒤로 단추, 앞으로 단추 누르기와 같은 여러 상황에서 표시 될 수 있기 때문에) 사용자 경험을 향상시키는 것이 필요하다고 생각합니다. ASP.NET의 표준 접근법은 Page_Load에 데이터를 바인딩하는 것입니다.이 데이터는 포스트 백일 경우 (예 : if (! Page.IsPostBack) DataBindMyControls();)입니다. 버튼 클릭과 같은 모든 다시 게시 이벤트는 업데이트해야하는 항목을 업데이트 할 수 있으며, Response.Redirect를 사용하여 페이지를 다시로드 할 수 있습니다. – rsbarro

0

장바구니에서 비슷한 문제가 있었지만 AJAX 업데이트 패널을 사용했고 AJAX 방식으로 리디렉션이 작동하지 않았습니다.

상품이 장바구니에 추가 된 후 mycartlistview.DataBind()을 내 코드에 사용했습니다.

관련 문제