Rating not updating label value?

2 posts, 0 answers
  1. Robert
    Robert avatar
    3 posts
    Member since:
    Aug 2011

    Posted 22 Nov 2011 Link to this post

    Can anyone help me explain why when I rate my items the actual average rating is not being updated???
    (I used the rating + comments demo as the foundation for this code)


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    'Get ratings and display on page
            Dim tbRatings As DataTable = New IMCSelect().getRatings(-1, 8, Request.QueryString("ID"), 1, 0, Application("logMaterials")).Tables(0)
            Dim Sum As Single
            For Each dr As DataRow In tbRatings.Rows
                If dr.RowState <> DataRowState.Deleted Then
                    Sum += Convert.ToInt32(dr("Rating"))
                End If
            Next
      
            Dim iAvgRating As Single = 0
            If tbRatings.Rows.Count <> 0 Then
                iAvgRating = Math.Round(Sum / tbRatings.Rows.Count, 2)
                lblAvgRating.Text = iAvgRating
            End If
      
            Dim tbComments As DataTable = New IMCSelect().getRatings(-1, 8, Request.QueryString("ID"), 1, 0, Application("logPublications")).Tables(0)
            Dim comments = From r In tbComments.AsEnumerable Where r.Field(Of String)("RatingComments") <> "" Select r
            If comments.Count() <> 0 Then
                tbComments = comments.CopyToDataTable()
            Else
                tbComments.Rows.Clear()
            End If
      
            hlComments.Text = "View comments"
            hlComments.NavigateUrl = "Javascript:ViewComments(" & Request.QueryString("ID") & ");"
      
            If tbComments.Rows.Count() <> 0 Then
                divComments.Style.Add("display", "block;")
            Else
                divComments.Style.Add("display", "none;")
            End If
    End Sub
      
    Private Property Rating() As Dictionary(Of String, Decimal)
            Get
                Dim varRating As Dictionary(Of String, Decimal) = DirectCast(ViewState("Rating"), Dictionary(Of String, Decimal))
                If [Object].Equals(varRating, Nothing) Then
                    varRating = New Dictionary(Of String, Decimal)()
                    varRating("sum") = 0
                    varRating("counter") = 0
                End If
                Return varRating
            End Get
            Set(ByVal value As Dictionary(Of String, Decimal))
                ViewState("Rating") = value
            End Set
        End Property
      
        Protected Sub btnPostComment_Click(ByVal sender As Object, ByVal e As EventArgs)
            Dim iRatingID As Integer
            iRatingID = New IMCInsert().addRating(RadRating1.Value, txtComments.Text, 8, Request.QueryString("ID"), Session("RBPOnline_UserID"), Application("logMaterials"))
            txtComments.Text = ""
            'Close tooltip
            Dim str As String = "CloseToolTip1();"
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(Page), "closeTooltip", str, True)
      
        End Sub
      
        Protected Sub RadRating1_Rate(ByVal sender As Object, ByVal e As EventArgs)
            Dim currentRating As Dictionary(Of String, Decimal) = Rating
            currentRating("sum") = currentRating("sum") + RadRating1.Value
            currentRating("counter") = currentRating("counter") + 1
            Rating = currentRating
            Dim averageRating As Decimal = Math.Round(currentRating("sum") / currentRating("counter"), 1)
        End Sub
  2. Slav
    Admin
    Slav avatar
    1355 posts

    Posted 24 Nov 2011 Link to this post

    Hi Robert,

    From the provided code sample it seems that you are using two separate data sources, which is the most probable cause for the encountered discrepancies.

    In the event handler Page_Load you are getting the data from a custom object and then you are calculating the average rating based on this data. However, the rest of the code uses the data, stored in the ViewState variable Rating. As a result, when the page is loaded, the average rating is calculated from the data, stored in IMCSelect, and it is displayed in the Label lblAvgRating. When the article is rated, the average rating is calculated from the data stored in ViewState("Rating"), instead of IMCSelect, and the new rating data is stored again in ViewState("Rating").

    Please try using one data source in order to achieve proper calculation of the average rating and check if the problem persists.

    Note that at the current state of affairs I am mostly guessing as to what your setup is. If you are still experiencing difficulties please open a support ticket and send us a simple, runnable project that displays your issue so that we can examine it locally and provide a more to the point answer.

    Regards,
    Slav
    the Telerik team
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
  3. UI for ASP.NET Ajax is Ready for VS 2017
Back to Top