2013-12-10 183 views
1

我正在生成一個總計來自轉發器的一些列並將其放入轉發器的頁腳的報告。ASP.NET公共屬性ViewState

我已經通過設置一個公共屬性爲如下每個總價值做到了這一點:

Public Property [CostTotal] As Decimal 
    Get 
     Return CStr(ViewState("CostTotal")) 
    End Get 
    Set(ByVal value As Decimal) 
     ViewState("CostTotal") = value 
    End Set 
End Property 

有對報告中的一些過濾選項,使用戶可以選擇日期等,以濾除報告。

當有人這樣做時,公共屬性值仍然保留(因爲它在視圖狀態)設置的任何過濾器,因此總數不會累加。有沒有辦法將公共屬性重新設置爲0,如果頁面被重新發布或者完全採用這種方式?

的ItemDataBound代碼

Protected Sub reMileage_ItemDataBound(sender As Object, e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles reMileage.ItemDataBound 



    If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then 

     Dim rowView As System.Data.DataRowView 
     rowView = CType(e.Item.DataItem, System.Data.DataRowView) 


     Dim lMileageDate As Literal = CType(e.Item.FindControl("lMileageDate"), Literal) 
     If Not IsDBNull(rowView("Date")) Then 
      Dim MileageDate As Date = rowView("Date") 
      lMileageDate.Text = MileageDate.ToString("dd/MM/yyyy") 
     End If 

     Dim lMileageDescription As Literal = CType(e.Item.FindControl("lMileageDescription"), Literal) 
     If Not IsDBNull(rowView("Location")) Then 
      lMileageDescription.Text = Format.TextboxHtmlDecode(rowView("Location")) 
     End If 

     Dim lMileageRate As Literal = CType(e.Item.FindControl("lMileageRate"), Literal) 
     If Not IsDBNull(rowView("Rate")) Then 
      Dim MileageRate As Decimal = rowView("Rate") 
      lMileageRate.Text = MileageRate.ToString("N3") 
     End If 

     Dim lMileageMiles As Literal = CType(e.Item.FindControl("lMileageMiles"), Literal) 
     If Not IsDBNull(rowView("Mileage")) Then 
      Dim MileageMiles As Decimal = rowView("Mileage") 
      lMileageMiles.Text = MileageMiles.ToString("N2") 

      MileageTotal += MileageMiles 
      'Response.Write(MileageTotal & "<br>" & MileageMiles) 
     End If 


     Dim lMileageCost As Literal = CType(e.Item.FindControl("lMileageCost"), Literal) 
     If Not IsDBNull(rowView("Cost")) Then 
      Dim MileageCost As Decimal = rowView("Cost") 
      lMileageCost.Text = MileageCost.ToString("C2") 

      CostTotal += MileageCost 
     End If 


    ElseIf e.Item.ItemType = ListItemType.Footer Then 


     Dim lMileageTotal As Literal = CType(e.Item.FindControl("lMileageTotal"), Literal) 
     lMileageTotal.Text = MileageTotal.ToString("N2") 

     Dim lCostTotal As Literal = CType(e.Item.FindControl("lCostTotal"), Literal) 
     lCostTotal.Text = CostTotal.ToString("C2") 

    End If 

End Sub 

感謝您的幫助。 J.

回答

1

你可以試試這個方法:

Protected Sub reMileage_ItemDataBound(sender As Object, e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles reMileage.ItemDataBound 

    If e.Item.ItemType = ListItemType.Header Then 
     CostTotal =0 

    ElseIf e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then 

     Dim rowView As System.Data.DataRowView 
     rowView = CType(e.Item.DataItem, System.Data.DataRowView) 


     Dim lMileageDate As Literal = CType(e.Item.FindControl("lMileageDate"), Literal) 
     If Not IsDBNull(rowView("Date")) Then 
      Dim MileageDate As Date = rowView("Date") 
      lMileageDate.Text = MileageDate.ToString("dd/MM/yyyy") 
     End If 

     Dim lMileageDescription As Literal = CType(e.Item.FindControl("lMileageDescription"), Literal) 
     If Not IsDBNull(rowView("Location")) Then 
      lMileageDescription.Text = Format.TextboxHtmlDecode(rowView("Location")) 
     End If 

     Dim lMileageRate As Literal = CType(e.Item.FindControl("lMileageRate"), Literal) 
     If Not IsDBNull(rowView("Rate")) Then 
      Dim MileageRate As Decimal = rowView("Rate") 
      lMileageRate.Text = MileageRate.ToString("N3") 
     End If 

     Dim lMileageMiles As Literal = CType(e.Item.FindControl("lMileageMiles"), Literal) 
     If Not IsDBNull(rowView("Mileage")) Then 
      Dim MileageMiles As Decimal = rowView("Mileage") 
      lMileageMiles.Text = MileageMiles.ToString("N2") 

      MileageTotal += MileageMiles 
      'Response.Write(MileageTotal & "<br>" & MileageMiles) 
     End If 


     Dim lMileageCost As Literal = CType(e.Item.FindControl("lMileageCost"), Literal) 
     If Not IsDBNull(rowView("Cost")) Then 
      Dim MileageCost As Decimal = rowView("Cost") 
      lMileageCost.Text = MileageCost.ToString("C2") 

      CostTotal += MileageCost 
     End If 


    ElseIf e.Item.ItemType = ListItemType.Footer Then 


     Dim lMileageTotal As Literal = CType(e.Item.FindControl("lMileageTotal"), Literal) 
     lMileageTotal.Text = MileageTotal.ToString("N2") 

     Dim lCostTotal As Literal = CType(e.Item.FindControl("lCostTotal"), Literal) 
     lCostTotal.Text = CostTotal.ToString("C2") 

    End If 

End Sub 
+0

我想我剛剛整理它,不知道這是很好的做法,但我可以只申報reMileage_ItemDataBound子外的總變量,謝謝你的幫助。 – JBoom