Introduction

Online shopping websites, online forums, deal websites and bidding websites are currently in demand and are a high priority, there are even more projects in the queue for me to develop.

I have seen that these kinds of websites require an option for rating each product, deal etc. There are many ways to do this in PHP, Wordpress or a shopping cart but since we know ASP.Net is not as easy as these, because these contain plugins just to install them and copy and paste some code, and ready, but I must say that it is a very quick solution but usually these solution create other problems.

Anyways, here I explain how to "create a rating in ASP.Net".

Requirement

Add Ajaxcontroltoolkit.dll.

Code

Write the following code in your source code:

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
  2. <%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  4. <html xmlns="http://www.w3.org/1999/xhtml">
  5. <head runat="server">
  6. <title>AJAX Rating Control</title>
  7. <style type="text/css">
  8. .ratingStar
  9. {
  10. font-size: 0pt;
  11. width: 13px;
  12. height: 12px;
  13. cursor: pointer;
  14. display: block;
  15. background-repeat: no-repeat;
  16. }
  17. .filledStar
  18. {
  19. background-image: url(image/Filled_Star.png);
  20. }
  21. .emptyStar
  22. {
  23. background-image: url(Image/Empty_Star.png);
  24. }
  25. .savedStar
  26. {
  27. background-image: url(Image/Saved_Star.png);
  28. }
  29. .auto-style1 {
  30. height: 50px;
  31. }
  32. </style>
  33. </head>
  34. <body>
  35. <form id="form1" runat="server">
  36. <div>
  37. <asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
  38. </asp:ToolkitScriptManager>
  39. <table cellpadding="0" cellspacing="0" align="left" width="500" style="color: #333333; background-color: #F0F0F0">
  40. <tr>
  41. <td height="40" style="font-family: Verdana, Geneva, Tahoma, sans-serif; font-size: small;">
  42. </td>
  43. </tr>
  44. <tr>
  45. <td height="50" style="font-family: Verdana, Geneva, Tahoma, sans-serif; font-size: small;">
  46. Rate My Article</td>
  47. </tr>
  48. <tr>
  49. <td align="center" class="auto-style1" style="font-family: Verdana, Geneva, Tahoma, sans-serif; font-size: small;">
  50. <asp:Rating ID="Rating1" runat="server" StarCssClass="ratingStar" WaitingStarCssClass="savedStar"
  51. FilledStarCssClass="filledStar" EmptyStarCssClass="emptyStar" AutoPostBack="true" CurrentRating="1" MaxRating="5"
  52. OnChanged="Rating1_Changed"></asp:Rating>
  53. </td>
  54. </tr>
  55. <tr>
  56. <td height="50" style="font-family: Verdana, Geneva, Tahoma, sans-serif; font-size: small;">
  57. <asp:Label ID="lbl_point" runat="server"></asp:Label>
  58. </td>
  59. </tr>
  60. </table>
  61. </div>
  62. </form>
  63. </body>
  64. </html>
Add the following code for the code behind page:
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7. public partial class _Default : System.Web.UI.Page
  8. {
  9. protected void Page_Load(object sender, EventArgs e)
  10. {
  11. }
  12. protected void Rating1_Changed(object sender, AjaxControlToolkit.RatingEventArgs e)
  13. {
  14. lbl_point.Text="You rated " + e.Value.ToString();
  15. }
  16. }
Save all the work and run the page. It will look as in the following image:

ASP.jpg

Hope it will help .

Thanks.