Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#ASP.NETAjax
Hi All,
I am using AJAX Rating Control.I want half star rating.
For Example,
If My AVG Rating Count is 3 Then My Stars Wants to be show 3
If My AVG Rating Count is 2.6 Then My Stars Wants to be Show 2 1/2 mark i.e., two & half stars.But,it was not showing then also it is showing 3 stars.
 
My Database & My Code goes like this,
 
Create Table Averagerating
( 
    ID int IDENTITY(1,1) Primary Key,
    Rating Float,
    UserName Varchar(50) 
)
<style type="text/css">
        .ratingStar
        {
            font-size: 0pt;
            width: 13px;
            height: 12px;
            margin: 0px;
            padding: 0px;
            cursor: pointer;
            display: block;
            background-repeat: no-repeat;
        }
        .filledRatingStar
        {
            background-image: url(FilledStar.PNG);
        }
        .emptyRatingStar
        {
            background-image: url(EmptyStar.PNG);
        }
        .savedRatingStar
        {
            background-image: url(SavedStar.PNG);
        }
</style>
<form id="form1" runat="server">
    <AJAX:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
    </AJAX:ToolkitScriptManager>
    <div>
        <asp:UpdatePanel ID="pnlRating" runat="server">
            <ContentTemplate>
                <table>
                    <tr>
                        <td>
                            <asp:Label ID="lblMyRating" runat="server" Text="Average Rating" Font-Bold="true"></asp:Label>
                        </td>
                        <td>
                            <ajax:Rating ID="ArticleRating"
                                         runat="server"
                                         CurrentRating="2"
                                         MaxRating="5"
                                         StarCssClass="ratingStar"
                                         WaitingStarCssClass="savedRatingStar"
                                         FilledStarCssClass="filledRatingStar"
                                         EmptyStarCssClass="emptyRatingStar"
                                         onchanged="ArticleRating_Changed"/>
                        </td>
                        <td>
                            <asp:Label ID="lblDisplay" runat="server" Font-Bold="true"></asp:Label>
                        </td>
                    </tr>
                    </table>
            </ContentTemplate>
        </asp:UpdatePanel>
    </div>
    </form>
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
 
public partial class _Default : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection("MyConnection");
 
    float COUNT = 0;
    float RATING = 0;
    float Result = 0;
    float MyCurrentRating = 0;
 
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindAverageRatingControl();
        }
    }
    public void BindAverageRatingControl()
    {
        DataTable dt = new DataTable();
        con.Open();
        SqlCommand cmd = new SqlCommand("SELECT COUNT(ID) AS NumberOfUsers,SUM(Rating) AS Total FROM Averagerating", con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        da.Fill(dt);
 
        if (Convert.ToInt32(dt.Rows[0]["NumberOfUsers"].ToString()) != 0)
        {
            COUNT = float.Parse(dt.Rows[0]["NumberOfUsers"].ToString());
            RATING = float.Parse(dt.Rows[0]["Total"].ToString());
            Result = Convert.ToInt32(Math.Ceiling((RATING / COUNT) * 18));
            MyCurrentRating = (float)Math.Round((RATING / COUNT), 1);
            ArticleRating.CurrentRating = Convert.ToInt32(MyCurrentRating);
        }
        else
        {
            ArticleRating.CurrentRating = 0;
        }
        lblDisplay.Text = MyCurrentRating.ToString();
        ArticleRating.Style.Add("width", MyCurrentRating + "px");
    }
 
    protected void ArticleRating_Changed(object sender, AjaxControlToolkit.RatingEventArgs e)
    {
        con.Open();
        SqlCommand cmd = new SqlCommand("Insert Into Averagerating(Rating,UserName)values(@Rating,@UserName)", con);
        cmd.Parameters.AddWithValue("@Rating", ArticleRating.CurrentRating);
        cmd.Parameters.AddWithValue("@UserName", "DESAI");
        cmd.ExecuteNonQuery();
        con.Close();
        BindAverageRatingControl();
    }
}
Please Give solution for this...
 
Thanks in advance,
VenkiDesai.
Posted 13-Sep-12 19:56pm
Edited 13-Sep-12 20:04pm
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

You need to have different images, 5*, 4.5 * , 4*, ... etc
Then you can use a Image Label, and set it image source(url) run time, a sample could be:
 
Markup:
<asp:Image runat="server" ID="imgRating" AlternateText="Average rating" />
 
code behind:
string url = "~/images/stars{0}.gif";
           if (_value <= 1.3)
              url = string.Format(url, "10");
           else if (_value <= 1.8)
              url = string.Format(url, "15");
           else if (_value <= 2.3)
              url = string.Format(url, "20");
           else if (_value <= 2.8)
              url = string.Format(url, "25");
           else if (_value <= 3.3)
              url = string.Format(url, "30");
           else if (_value <= 3.8)
              url = string.Format(url, "35");
           else if (_value <= 4.3)
              url = string.Format(url, "40");
           else if (_value <= 4.8)
              url = string.Format(url, "45");
           else
              url = string.Format(url, "50");
           imgRating.ImageUrl = url;
 
see 2.5 image, we can save it as star25.png:
http://mmmjustlovely.files.wordpress.com/2012/07/2-5-stars.png[^]
 
Se 4.5 image, save it as star45.png
http://www.easywaystosavemoney.com.au/images/689.jpg[^]
  Permalink  
v4
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Try changing this line:
MyCurrentRating = (float)Math.Round((RATING / COUNT), 1);
to
MyCurrentRating = (float)Math.Round((RATING / COUNT),  1, MidpointRounding.AwayFromZero);
 
If this does not resolve, look here at the different overloads and see what works for you: MSDN: Math.Round Method [^]
 

BTW, strange you want to lower your rating then actual!
  Permalink  
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Dnyaneshwar@Pune 1,300
1 Sergey Alexandrovich Kryukov 802
2 Tadit Dash 330
3 ravikhoda 300
4 OriginalGriff 272
0 Sergey Alexandrovich Kryukov 11,441
1 OriginalGriff 6,930
2 Peter Leow 4,850
3 Abhinav S 3,838
4 Maciej Los 3,575


Advertise | Privacy | Mobile
Web04 | 2.8.140421.2 | Last Updated 14 Sep 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid