ASP.NET公共属性ViewState

时间:2013-12-10 17:40:55

标签: asp.net vb.net repeater asp.net-4.0

我正在制作一份报告,该报告总结了转发器中的某些列,并将其放在转发器的页脚中。

我通过为每个总值设置公共属性来完成此操作,如下所示:

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

感谢您的帮助。 学家

1 个答案:

答案 0 :(得分: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