Introduction
This article will help you to create a rating control in ASP.Net with Bootstrap instead of AJAX rating control.
Step 1
Download the CSS and JavaScript files from github.com.
Step 2
Open your Visual Studio, then add your downloaded file into your project then add index.aspx page and call your necessary files within the head tag from that downloaded folder.
- <head runat="server">
- <title></title>
- <link href="Bootstrap/css/bootstrap.min.css" rel="stylesheet" />
- <link href="Bootstrap/css/star-rating.css" rel="stylesheet" />
- <script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
- <script src="Bootstrap/js/star-rating.js"></script>
- </head>
Now call your rating control. Before calling your rating control please learn how to use Bootstrap from my previous article.
- <div class="row">
- <div class="col-lg-12">
- <input id="input-21a" value="0" type="number" class="rating" data-symbol="*" min=0 max=5 step=0.5 data-size="xl" >
- <hr>
- <input id="input-21b" type="number" class="rating" min=0 max=5 step=0.5 data-glyphicon="false" data-star-captions="{}" data-default-caption="{rating} Stars" data-size="lg">
- <hr>
- <input id="input-21c" value="0" type="number" class="rating" min=0 max=8 step=0.5 data-size="xl" data-stars="8">
- <hr>
- <input id="input-21d" value="2" type="number" class="rating" min=0 max=5 step=0.5 data-size="sm">
- <hr>
- <input id="input-21e" value="0" type="number" class="rating" min=0 max=5 step=0.5 data-size="xs" >
- <hr>
- </div>
- </div>
For your better understanding please visit here.
Step 4
Now we can see how to get the rated value from this rating control using jQuery as in the following:
- <script>
- $(document).ready(function () {
- $("#input-21b").on("rating.change", function (event, value, caption) {
- alert("You rated: " + value + " = " + $(caption).text());
- });
- });
- </script>
Let's see how to get the rated value from code behind in C#. Add one hidden field in your design page and assign your rated value into your hidden field.
- <asp:HiddenField ID="hdfRatingValue" runat="server" />
- <script>
- $(document).ready(function () {
- $("#input-21b").on("rating.change", function (event, value, caption) {
- var ratingValue = $('#<%=hdfRatingValue.ClientID%>').val();
- ratingValue = value;
- alert(ratingValue);
- });
- });
- </script>
Full Example
- <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="index.aspx.cs" Inherits="RatingControl.index" %>
- <!DOCTYPE html>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title></title>
- <link href="Bootstrap/css/bootstrap.min.css" rel="stylesheet" />
- <link href="Bootstrap/css/star-rating.css" rel="stylesheet" />
- <script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
- <script src="Bootstrap/js/star-rating.js"></script>
- <script>
- $(document).ready(function () {
- $("#input-21b").on("rating.change", function (event, value, caption) {
- var ratingValue = $('#<%=hdfRatingValue.ClientID%>').val();
- ratingValue = value;
- alert(ratingValue);
- });
- });
- </script>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <div class="row">
- <div class="col-lg-12">
- <input id="input-21a" value="0" type="number" class="rating" data-symbol="*" min=0 max=5 step=0.5 data-size="xl" >
- <hr>
- <input id="input-21b" type="number" class="rating" min=0 max=5 step=0.5 data-glyphicon="false" data-star-captions="{}" data-default-caption="{rating} Stars" data-size="lg">
- <hr>
- <input id="input-21c" value="0" type="number" class="rating" min=0 max=8 step=0.5 data-size="xl" data-stars="8">
- <hr>
- <input id="input-21d" value="2" type="number" class="rating" min=0 max=5 step=0.5 data-size="sm">
- <hr>
- <input id="input-21e" value="0" type="number" class="rating" min=0 max=5 step=0.5 data-size="xs" >
- <hr>
- </div>
- </div>
- </div>
- <asp:HiddenField ID="hdfRatingValue" runat="server" />
- </form>
- </body>
- </html>
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- namespace RatingControl
- {
- public partial class index : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- }
- public void saveRating()
- {
- string val= hdfRatingValue.Value;
- }
- }
- }

Umesh AggarwalPosted May 19, 2021, 1:37 PM
Do you have return bind from database
Wasim AbbasiPosted Jun 23, 2019, 2:21 PM
Just copy paste this code in top javascript codee,,,,simply...HdfRatingValue.value = ratingValue; inside this code...
Wasim AbbasiPosted Jun 23, 2019, 2:20 PM
HdfRatingValue.value = ratingValue; inside this code...
Syed MallickPosted Aug 6, 2018, 10:36 AM
Kindly Correct Step 4; INSTEAD ' on("rating.change") ' USE ' on("rating:change") '
kamlesh yadavPosted Jul 3, 2017, 9:15 AM
VEry helping code...thanks sesuraj
Jordan MichaelPosted Dec 24, 2016, 12:23 AM
Var myHidden = document.getElementById('<%= hdfRatingValue.ClientID %>');myHidden.value = value; (This line cant retrieve value)
Jordan MichaelPosted Dec 24, 2016, 12:23 AM
I placed this in the design page but it is not working
Jordan MichaelPosted Dec 24, 2016, 12:22 AM
Cant retrieve the rating value from cs page
Shahbaz AliPosted Nov 25, 2016, 12:37 AM
An other great article. nice one
Chris HaynesPosted Jun 10, 2016, 9:06 AM
var myHidden = document.getElementById('<%= hdfRatingValue.ClientID %>');myHidden.value = value;
Hitesh PrajapatiPosted May 6, 2016, 7:30 AM
how to display current rating value as a star icon in asp.net design page ??
KarlPosted Sep 27, 2015, 4:59 PM
hdfRatingValue.Value is empty. Some suggest ? :(
KarlPosted Sep 23, 2015, 1:51 PM
Step 4 does not work here :(
KarlPosted Sep 23, 2015, 1:37 PM
stars show ok, but alert event fails here :(
NitinPosted May 11, 2015, 12:19 PM
good one
Sibeesh VenuPosted May 11, 2015, 3:02 AM
Good One :)
Abhishek JaiswalPosted May 11, 2015, 2:18 AM
Informative and interesting! :)
Tom MohanPosted May 11, 2015, 1:48 AM
useful
Saineshwar BageriPosted May 11, 2015, 1:36 AM
Nice one
Santhakumar MunuswamyPosted May 10, 2015, 11:58 PM
Thanks for nice article