A common way to use the SlickRater control is to subclass it with hard-coded properties and the functionality required to tie it to rating data within a database:
C#using System; using System.Data; using System.Data.SqlClient; using System.Configuration; using System.Web.UI; using KrispWare; namespace localhost { [ToolboxData("<{0}:DBSlickRater runat=server></{0}:DBSlickRater>")] public class DBSlickRater : SlickRater { public override string Image { get { return "smiley.gif"; } } // custom rating scale protected override RatingCollection DefaultRatings { get { return new RatingCollection(new string[]{"Thumbs WAY Down", "Thumbs Down", "Thumbs Sideways", "Thumbs Up", "Thumbs WAY Up"}); } } public override string OnClickJScript { get { if (!(bool)(Page.Session["IsLoggedOn"])) { return "function(){document.location.href='/signup.aspx'}"; } return String.Empty; } } // set the UserRating and AverageRating properties protected override void OnLoad(EventArgs e) { string connectionStr = ConfigurationSettings.AppSettings["ConnectionString"]; SqlConnection connection = new SqlConnection(connectionStr); SqlCommand command = new SqlCommand("spGetRatings",connection); command.CommandType = CommandType.StoredProcedure; command.Parameters.Add("@RatedID",this.RatedID); command.Parameters.Add("@UserID",Page.Session["UserID"]); SqlDataReader reader = null; try { connection.Open(); reader = command.ExecuteReader( CommandBehavior.SingleRow | CommandBehavior.CloseConnection); reader.Read(); this.UserRating = reader.GetInt32(0); this.AverageRating = reader.GetSqlDecimal(1).Value; } finally { if (reader != null && !reader.IsClosed) reader.Close(); } base.OnLoad (e); } protected override void OnRatingChanged(SlickRaterRatingChangedEventArgs e) { string connectionStr = ConfigurationSettings.AppSettings["ConnectionString"]; SqlConnection connection = new SqlConnection(connectionStr); SqlCommand command = new SqlCommand("spSetRating",connection); command.CommandType = CommandType.StoredProcedure; command.Parameters.Add("@RatedID",this.RatedID); command.Parameters.Add("@UserID",Page.Session["UserID"]); command.Parameters.Add("@UserRating",this.UserRating); try { connection.Open(); command.ExecuteNonQuery(); } finally { if (connection.State != ConnectionState.Closed) connection.Close(); } base.OnRatingChanged(e); } } }
VB.NETImports System Imports System.Data Imports System.Data.SqlClient Imports System.Configuration Imports System.Web.UI Imports KrispWare Namespace localhost <ToolboxData("<{0}:DBSlickRater runat=server></{0}:DBSlickRater>")> _ Public Class DBSlickRater Inherits SlickRater Public Overrides ReadOnly Property Image() As String Get Return "smiley.gif" End Get End Property ' custom rating scale Protected Overrides ReadOnly Property DefaultRatings() As RatingCollection Get Return New RatingCollection(New String(){"Thumbs WAY Down", "Thumbs Down", "Thumbs Sideways", "Thumbs Up", "Thumbs WAY Up" End Get End Property Public Overrides ReadOnly Property OnClickJScript() As String Get If Not CType((Page.Session("IsLoggedOn")),Boolean) Then Return "function(){document.location.href=/'signup.aspx'}" End If Return String.Empty End Get End Property ' set the UserRating and AverageRating properties Protected Overrides Sub OnLoad(ByVal e As EventArgs) Dim connectionStr As String = ConfigurationSettings.AppSettings("ConnectionString") Dim connection As SqlConnection = New SqlConnection(connectionStr) Dim command As SqlCommand = New SqlCommand("spGetRatings",connection) command.CommandType = CommandType.StoredProcedure command.Parameters.Add("@RatedID",Me.RatedID) command.Parameters.Add("@UserID",Page.Session("UserID")) Dim reader As SqlDataReader = Nothing Try connection.Open() reader = command.ExecuteReader( CommandBehavior.SingleRow | CommandBehavior.CloseConnection) reader.Read() Me.UserRating = reader.GetInt32(0) Me.AverageRating = reader.GetSqlDecimal(1).Value Finally If reader <> Nothing And Not reader.IsClosed Then reader.Close() End If End Try MyBase.OnLoad (e) End Sub Protected Overrides Sub OnRatingChanged(ByVal e As SlickRaterRatingChangedEventArgs) Dim connectionStr As String = ConfigurationSettings.AppSettings("ConnectionString") Dim connection As SqlConnection = New SqlConnection(connectionStr) Dim command As SqlCommand = New SqlCommand("spSetRating",connection) command.CommandType = CommandType.StoredProcedure command.Parameters.Add("@RatedID",Me.RatedID) command.Parameters.Add("@UserID",Page.Session("UserID")) command.Parameters.Add("@UserRating",Me.UserRating) Try connection.Open() command.ExecuteNonQuery() Finally If connection.State <> ConnectionState.Closed Then connection.Close() End If End Try MyBase.OnRatingChanged(e) End Sub End Class End Namespace