ASP.NET Public Property ViewState

1.1k views Asked by At

I am producing a report that totals some columns from a repeater and puts it in the footer of the repeater.

I have done this by setting a Public Property for each total value as below:

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

There are some filter options on the report so users can select dates etc to filter the report by.

When someone does this the Public Property value is still retained (as it is in the viewstate) for any filters set so the totals then do not add up. Is there a way to set the Public Property to 0 again if the page is posted back or better way of doing this altogether?

ItemDataBound Code

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

Thanks for any help. J.

1

There are 1 answers

1
Bhavesh Kachhadiya On BEST ANSWER

you can try on this way:

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