|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
IntroductionThis is an article on how to use the Rating control from the AJAX Control Toolkit, and create CSS and images to display it as a gauge or thermometer. It is useful for those who need to show ratings in ASP.NET more graphically. BackgroundOnce there was a demand on creating a thermometer on some AJAX ASP.NET site. Simple configuration of parameter ranges and different graphics were required. Setting upPre-requisities to use the
Using the codeThe first thing you need to do is to add the <%@ Page Language="VB" AutoEventWireup="true"
CodeFile="Default.aspx.vb" Inherits="_Default" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<div>
<cc1:Rating ID="Rating1" runat="server">
</cc1:Rating>
</div>
</form>
</body>
</html>
You need to set up the basic attributes of the <ajaxToolkit:Rating runat="server" ID="Rating1"
MaxRating="5"
CurrentRating="2"
CssClass="ratingStar"
StarCssClass="ratingItem"
WaitingStarCssClass="Saved"
FilledStarCssClass="Filled"
EmptyStarCssClass="Empty"
>
</ajaxToolkit:Rating>
The control provides a selection of five stars; therefore, it is possible to select a rating from 1 to 5. There are three basic images to display the control's state, recognized by CSS class names:
Also, there are two more styles:
The control renders like this: But what if we want to use another graphics? What if we want to have a ratio from 1 to 10? What if we want to be able to select even a zero or "not-selected" value? Simply, the The main idea is:
The sample MaxRating="21"
CssClass="ratingThermometer"
StarCssClass="ratingItem"
WaitingStarCssClass="Saved"
FilledStarCssClass="Filled"
EmptyStarCssClass="Empty"
... and shows a thermometer with values from 0 to 10 with a 0.5 step. Hence, The modified control renders like this: Finally, you need to compute the value that corresponds with your graphics, according to the control's Public Shared Function EvaluateRating(ByVal CurrentValue As Integer, _
ByVal MaxRating As Integer, _
ByVal MinimumRange As Integer, _
ByVal MaximumRange As Integer) As Double
' If MinimumRange = 0 than compute value differently otherwise not
Dim FromZero As Integer = IIf(MinimumRange = 0, 1, 0)
Dim Delta As Double = (MaximumRange - MinimumRange) / (MaxRating - 1)
Dim Result As Double = Delta * CurrentValue - Delta * FromZero
Return result
End Function
Points of interest
History
|
||||||||||||||||||||||