Star Rating Using AJAX

In this section we will learn about Star rating concept using Ajax.
 
Introduction
 
AJAX (Asynchronous JavaScript and XML) is a technique for creating fast and dynamic web pages. AJAX allows web pages to be updated asynchronously by exchanging small amounts of data with the server behind the scenes. This means that it is possible to update parts of a web page, without reloading the whole page.
 
Step 1 Create table (table name like UserRating)
 
 
 
Step 2 Open your Visual Studio and add folder in your solution explorer
 
 
 
Step 3 And insert image for rating
 
 
 
Step 4 Add a web from (like Default.aspx), and define CSS
  1. <style type="text/css">  
  2. body {  
  3. margin0px auto;  
  4. width980px;  
  5. font-family"Trebuchet MS"ArialHelveticasans-serif;  
  6. background#C9C9C9;  
  7. }  
  8. .blankstar {  
  9. background-imageurl(images/blank_star.png);  
  10. width16px;  
  11. height16px;  
  12. }  
  13. .waitingstar {  
  14. background-imageurl(images/half_star.png);  
  15. width16px;  
  16. height16px;  
  17. }  
  18. .shiningstar {  
  19. background-imageurl(images/shining_star.png);  
  20. width16px;  
  21. height16px;  
  22. }  
  23. </style>  
Design your web form
  1. <asp:ScriptManager ID="ScriptManager1" runat="server">  
  2. </asp:ScriptManager>  
  3. <%-- Use ScriptManager from your Ajax toolkit--%>  
  4. <br />  
  5. <h1>C-sharpcorner</h1>  
  6. <br />  
  7. <asp:Rating ID="Rating1" runat="server" AutoPostBack="true" StarCssClass="blankstar"  
  8. WaitingStarCssClass="Halfstar" FilledStarCssClass="shiningstar"  
  9. EmptyStarCssClass="blankstar" OnChanged="Rating1_Changed">  
  10. </asp:Rating>  
  11. <%--use rating control from your ajax toolkit--%>  
  12. <br />  
  13. <br />  
  14. <br />  
  15. <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>,  
  16. <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>  
Step 5 Code behind (Default.aspx.cs)
 
On page load 
  1. Label1.Text = "0 Users have rated this Product";  
  2. Label2.Text = "Average rating for this Product is 0";  
  3.   
  4. if (!IsPostBack)  
  5. {  
  6. BindRatings();  
  7. }  
Step 6 Create change event insert your rating to database table
  1. protected void Rating1_Changed(object sender, AjaxControlToolkit.RatingEventArgs e)  
  2. {  
  3. con.Open();  
  4. SqlCommand cmd = new SqlCommand("INSERT INTO UserRating(Rating) VALUES (@Rating)", con);// insert rating to UserRating table  
  5. cmd.Parameters.AddWithValue("@Rating", SqlDbType.Int).Value = Rating1.CurrentRating;  
  6. cmd.ExecuteNonQuery();  
  7. con.Close();  
  8. BindRatings();  
  9. }  
Step 7 Bind rating value from database table and calculate average rating
  1. public void BindRatings()  
  2. {  
  3. int Total = 0;  
  4. con.Open();  
  5. SqlCommand cmd = new SqlCommand("SELECT Rating FROM UserRating", con);  
  6. SqlDataAdapter da = new SqlDataAdapter(cmd);  
  7. DataTable dt = new DataTable();  
  8. da.Fill(dt);  
  9. if (dt.Rows.Count > 0)  
  10. {  
  11. for (int i = 0; i < dt.Rows.Count; i++)  
  12. {  
  13. Total += Convert.ToInt32(dt.Rows[i][0].ToString());  
  14. }  
  15. int Average = Total / (dt.Rows.Count); // calculate Average rating  
  16. Rating1.CurrentRating = Average;  
  17. Label1.Text = dt.Rows.Count + " " + "Users have rated ";  
  18. Label2.Text = "Average rating for C-sharpcorner is" + " " + Convert.ToString(Average);  
  19. }  
  20. }  
Here is your complete code ( Default.aspx.cs)
  1. using System;  
  2. using System.Data;  
  3. using System.Data.SqlClient;  
  4. using System.Configuration;  
  5. public partial class _Default : System.Web.UI.Page  
  6. {  
  7. SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);  
  8. protected void Page_Load(object sender, EventArgs e)  
  9. {  
  10. Label1.Text = "0 Users have rated this Product";  
  11. Label2.Text = "Average rating for this Product is 0";  
  12.   
  13. if (!IsPostBack)  
  14. {  
  15. BindRatings();  
  16. }  
  17. }  
  18. protected void Rating1_Changed(object sender, AjaxControlToolkit.RatingEventArgs e)  
  19. {  
  20. con.Open();  
  21. SqlCommand cmd = new SqlCommand("INSERT INTO UserRating(Rating) VALUES (@Rating)", con);// insert rating to UserRating table  
  22. cmd.Parameters.AddWithValue("@Rating", SqlDbType.Int).Value = Rating1.CurrentRating;  
  23. cmd.ExecuteNonQuery();  
  24. con.Close();  
  25. BindRatings();  
  26. }  
  27. public void BindRatings()  
  28. {  
  29. int Total = 0;  
  30. con.Open();  
  31. SqlCommand cmd = new SqlCommand("SELECT Rating FROM UserRating", con);  
  32. SqlDataAdapter da = new SqlDataAdapter(cmd);  
  33. DataTable dt = new DataTable();  
  34. da.Fill(dt);  
  35. if (dt.Rows.Count > 0)  
  36. {  
  37. for (int i = 0; i < dt.Rows.Count; i++)  
  38. {  
  39. Total += Convert.ToInt32(dt.Rows[i][0].ToString());  
  40. }  
  41. int Average = Total / (dt.Rows.Count); // calculate Average rating  
  42. Rating1.CurrentRating = Average;  
  43. Label1.Text = dt.Rows.Count + " " + "Users have rated ";  
  44. Label2.Text = "Average rating for C-sharpcorner is" + " " + Convert.ToString(Average);  
  45. }  
  46. }  
  47. }  
Output
 
 
Next Recommended Reading Custom Alert Message using Ajax