Click here to Skip to main content
15,889,034 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.5K   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

 
AnswerRe: how i get vote value in cs Pin
taihip14-Apr-10 5:36
taihip14-Apr-10 5:36 
GeneralRe: how i get vote value in cs Pin
yuyejian14-Apr-10 18:56
yuyejian14-Apr-10 18:56 
GeneralRe: how i get vote value in cs Pin
taihip15-Apr-10 2:58
taihip15-Apr-10 2:58 
GeneralProblem with UpdatePanel Pin
Member 39168742-Feb-10 2:03
Member 39168742-Feb-10 2:03 
GeneralRe: Problem with UpdatePanel Pin
taihip2-Feb-10 5:11
taihip2-Feb-10 5:11 
GeneralRe: Problem with UpdatePanel Pin
Member 39168742-Feb-10 5:13
Member 39168742-Feb-10 5:13 
GeneralRe: Problem with UpdatePanel Pin
taihip3-Feb-10 3:47
taihip3-Feb-10 3:47 
GeneralRe: Problem with UpdatePanel Pin
maorray29-Jun-10 1:31
maorray29-Jun-10 1:31 
GeneralRe: Problem with UpdatePanel Pin
taihip29-Jun-10 4:14
taihip29-Jun-10 4:14 
GeneralRe: Problem with UpdatePanel Pin
maorray29-Jun-10 4:30
maorray29-Jun-10 4:30 
GeneralRe: Problem with UpdatePanel Pin
taihip29-Jun-10 5:08
taihip29-Jun-10 5:08 
GeneralResolveUrl Pin
ernest_elias18-Jun-09 2:21
ernest_elias18-Jun-09 2:21 
AnswerRe: ResolveUrl [modified] Pin
taihip18-Jun-09 15:37
taihip18-Jun-09 15:37 

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.