Click here to Skip to main content
15,881,938 members
Articles / Web Development / XHTML

ASP.NET Rating Control

Rate me:
Please Sign up or sign in to vote.
4.05/5 (38 votes)
18 Apr 2011CPOL 112.2K   3.8K   76   38
An ASP.NET rating control

Introduction

There are many free rating controls on the internet, but I didn't find any control which fits these features:

  • Works well in UpdatePanel, GridView
  • Easy to customize UI and behavior
  • Auto generates JavaScript (does not need an external JS file)

So I decided to write an ASP.NET custom control as per my requirements.

Using the Code

How to use this control?

  1. Add a reference to the control,
  2. and:
    ASP.NET
    <%@ Page Language="C#" AutoEventWireup="true" 
    	Codebehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>
    
    <%@ Register Namespace="ASPnetRater" Assembly="ASPnetRater" TagPrefix="cc" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>Untitled Page</title>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <cc:Rater ID="Rater1" runat="server" 
    		ImageOff="/assets/images/rating_grey_star.gif"
                    Value="3" MaxValue="5" ImageOn="/assets/images/rating_red_star.gif"
                    ToolTip="I'm a rater" OnCommand="Rater1_Command" 
    		ImageOver="/assets/images/rating_yellow_star.gif"></cc:Rater>
            </div>
        </form>
    </body>
    </html>	 

This is the result:

Normal Status

Hover Status

Points of Interest

If you want the control to work well in an UpdatePanel, please make the control become a trigger of UpdatePanel:

C#
ScriptManager1.RegisterAsyncPostBackControl(Rater1);

History

  • April 08, 2009: First draft
  • April 15, 2010: Updates
    • Fixed a bug while posting back, as reported by yuyejian
    • Uses the Enabled property to enable/disable the control
    • Image's URL supports URLs like "~/assets/..."
  • June 29, 2010: Updated source code
    • Fixed bug reported by maorray (Doesn't save/load the value of the rater to ControlState)
  • April 18, 2011: Updated source code
    • Added CommandName property
    • Calls base.RaiseBubbleEvent() in OnCommand()
    • This update will help the parent control such as GridView, DataList and the like to catch OnItemCommand event.

License

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


Written By
Web Developer SEA-Solutions
Vietnam Vietnam
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questionhow to remove imageover Pin
Priyanka.Terse19-Apr-17 23:52
Priyanka.Terse19-Apr-17 23:52 
AnswerRe: how to remove imageover Pin
taihip13-May-18 20:20
taihip13-May-18 20:20 
QuestionHow to get the value in Javascript? Pin
lakshmant.khms27-Apr-15 1:19
lakshmant.khms27-Apr-15 1:19 
AnswerRe: How to get the value in Javascript? Pin
taihip3-Sep-15 18:31
taihip3-Sep-15 18:31 
GeneralThank you Pin
BLD5198-Jan-14 5:37
BLD5198-Jan-14 5:37 
GeneralMy vote of 1 Pin
Member 1021431822-Oct-13 23:56
Member 1021431822-Oct-13 23:56 
GeneralMy vote of 3 Pin
somnathmandal5-Oct-13 1:21
somnathmandal5-Oct-13 1:21 
GeneralMy vote of 3 Pin
Tapan Parida18-Oct-12 2:12
Tapan Parida18-Oct-12 2:12 
Question5 Stars! Pin
ldsgomes16-Jun-12 7:14
ldsgomes16-Jun-12 7:14 
AnswerRe: 5 Stars! Pin
vijayviki728-Nov-12 23:05
vijayviki728-Nov-12 23:05 
GeneralRe: 5 Stars! Pin
taihip28-Nov-12 23:59
taihip28-Nov-12 23:59 
GeneralRe: 5 Stars! Pin
vijayviki729-Nov-12 0:58
vijayviki729-Nov-12 0:58 
GeneralRe: 5 Stars! Pin
taihip9-Dec-12 16:29
taihip9-Dec-12 16:29 
GeneralRe: 5 Stars! Pin
vijayviki712-Dec-12 20:18
vijayviki712-Dec-12 20:18 
QuestionExample needed on Rater Control Pin
premalvala25-Jul-11 3:48
premalvala25-Jul-11 3:48 
AnswerRe: Example needed on Rater Control Pin
taihip25-Jul-11 4:15
taihip25-Jul-11 4:15 
QuestionDataList [modified] Pin
zoran Jancic zozo18-Apr-11 1:46
zoran Jancic zozo18-Apr-11 1:46 
AnswerRe: DataList Pin
taihip18-Apr-11 15:34
taihip18-Apr-11 15:34 
GeneralRe: DataList Pin
zoran Jancic zozo18-Apr-11 20:25
zoran Jancic zozo18-Apr-11 20:25 
GeneralMouse Over Pin
Siya Dilimeni6-Dec-10 21:41
Siya Dilimeni6-Dec-10 21:41 
GeneralRe: Mouse Over Pin
taihip6-Dec-10 21:56
taihip6-Dec-10 21:56 
QuestionRe: Mouse Over Pin
Siya Dilimeni6-Dec-10 22:15
Siya Dilimeni6-Dec-10 22:15 
AnswerRe: Mouse Over Pin
taihip6-Dec-10 22:31
taihip6-Dec-10 22:31 
GeneralRe: Mouse Over Pin
Siya Dilimeni7-Dec-10 22:13
Siya Dilimeni7-Dec-10 22:13 
Questionhow i get vote value in cs Pin
yuyejian13-Apr-10 21:41
yuyejian13-Apr-10 21:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.