Rating控件提供一个具有特殊效果的等级,而且以图片的样式展现的。
属性列表:MaxRating :最高可以给予的等级高低CurrentRating :当前等级CssClass : 指定评级控件所套用的样式表。StarCssClass :星的样式WaitingStarCssClass :更改等级时星的显示样式FilledStarCssClass :设置等级后星的显示样式EmptyStarCssClass :没有选中时等级的显示样式AutoPostBack :是否支持页面回发OnChanged :等级变化时触发的事件Tag:为事件传递的参数
常见问题和使用技巧:常见问题:可不可以不使用Rating控件的自动异步回调功能,而是随着表单一起传回服务器?使用技巧:int rating=myRatingID.CurrentRating;可以获得当前评级。
实例解析一、简单使用实例(必须为此控件添加样式和图片不然运行错误)1.UI界面<head runat="server"> <title>无标题页</title> <style> .ratingstar{ font-size:0pt; width:15px; height:12px; margin:1px; padding:1px; currsor:pointer; display:block; background-repeat:no-repeat; } .filledratingstar{ background-image:url(1.gif); } .emptyratingstar{ background-image:url(2.gif); } .savedratingstar{ background-image:url(3.gif); } </style></head><body> <form id="form1" runat="server"> <div> <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager> </div> <asp:UpdatePanel ID="UpdatePanel1" runat="server"> <ContentTemplate> <table style="width: 361px; height: 81px"> <tr> <td style="width: 56px">姓名:</td> <td style="width: 100px"><asp:TextBox ID="TextBox1" runat="server"></asp:TextBox></td> </tr> <tr> <td style="width: 56px; height: 20px"> 等级:</td> <td > <cc1:rating id="Rating1" runat="server" currentrating="2" height="13px" maxrating="8" AutoPostBack="true" OnChanged="Rating1_Changed" StarCssClass="ratingstar" EmptyStarCssClass="emptyratingstar" FilledStarCssClass="filledratingstar" WaitingStarCssClass="savedratingstar"></cc1:rating> </td> </tr> <tr> <td style="width: 56px"></td> <td style="width: 100px"><asp:Label ID="Label1" runat="server" Text="Label"></asp:Label></td> </tr> </table> </ContentTemplate> </asp:UpdatePanel> </form></body>2.后台protected void Rating1_Changed(object sender, AjaxControlToolkit.RatingEventArgs e) { if (int.Parse(e.Value) < 4) { Label1.Text = "对不起,最低为4级!"; } int temp; Math.DivRem(int.Parse(e.Value),2,out temp); if (temp != 0) { Label1.Text = "对不起,等级必须是468!"; }}
实例解析二、和数据库相关联的实例(两张图片见附件)
<%@ Page Language="C#" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void rating_Changed(object sender, RatingEventArgs e) { MusicData.UpdateMusicRate(int.Parse(e.Tag), int.Parse(e.Value)); }</script><html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server"> <title>Rating Demo</title> <link href="StyleSheet.css" rel="stylesheet" type="text/css" /></head><body> <form id="form1" runat="server"> <asp:ScriptManager ID="sm" runat="server" /> <asp:GridView ID="musicGrid" runat="server" AutoGenerateColumns="False" DataSourceID="musicDataSource" Width="400px"> <Columns> <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" /> <asp:TemplateField HeaderText="Rating" SortExpression="Rating"> <ItemTemplate> <ajaxToolkit:Rating ID="rating" runat="server" StarCssClass="ratingStar" FilledStarCssClass="filledRatingStar" EmptyStarCssClass="emptyRatingStar" WaitingStarCssClass="filledRatingStar" CurrentRating='<%# Bind("Rating") %>' MaxRating="5" Tag='<%# Bind("Id") %>' OnChanged="rating_Changed"> </ajaxToolkit:Rating> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> <asp:ObjectDataSource ID="musicDataSource" runat="server" DataObjectTypeName="MusicEntry" SelectMethod="GetMusicList" TypeName="MusicData"></asp:ObjectDataSource> </form></body>2.类文件
using System;using System.Data;using System.Configuration;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Web.UI.HtmlControls;using System.ComponentModel;using System.Collections.Generic;/// <summary>
/// Summary description for MusicData/// </summary>[DataObject(true)]public class MusicData{ private static List<MusicEntry> m_data;private static List<MusicEntry> data
{ get { if (m_data == null) { m_data = new List<MusicEntry>(); Random rand = new Random(); m_data.Add(new MusicEntry(0, "Polonaise in A. Op 40, No. 1 (\"Military\")", rand.Next(6), 0)); m_data.Add(new MusicEntry(1, "Nocturne in E-Flat, Op. 9, No. 2", rand.Next(6), 1)); m_data.Add(new MusicEntry(2, "Waltz in A-Flat, Op. 34, No. 1 (\"Valse brillante\")", rand.Next(6), 2)); m_data.Add(new MusicEntry(3, "Waltz in C-sharp Minor, Op. 64, No. 2", rand.Next(6), 3)); m_data.Add(new MusicEntry(4, "Waltz in D-Flat, Op. 64, No. 1 (\"Minute\" Waltz)", rand.Next(6), 4)); m_data.Add(new MusicEntry(5, "Polonaise in A-Flat, Op. 53 (\"Heroic\")", rand.Next(6), 5)); m_data.Add(new MusicEntry(6, "Mazurka in D, Op. 33, No. 2", rand.Next(6), 6)); m_data.Add(new MusicEntry(7, "Fantaisie-Impromptu in C-Sharp Minor, Op. 66", rand.Next(6), 7)); m_data.Add(new MusicEntry(8, "Mazurka in B-Flat, Op. 7, No. 1", rand.Next(6), 8)); m_data.Add(new MusicEntry(9, "Nocturne in G Minor, Op. 37, No. 1", rand.Next(6), 9)); } return m_data; } }[DataObjectMethod(DataObjectMethodType.Select)]
public static List<MusicEntry> GetMusicList() { data.Sort(CompareMusicEntry); return data; }private static int CompareMusicEntry(MusicEntry x, MusicEntry y)
{ return x.Order.CompareTo(y.Order); }[DataObjectMethod(DataObjectMethodType.Update)]
public static void UpdateMusic(MusicEntry music) { for (int i = 0; i < data.Count; ++i) { if (data[i].Id == music.Id) { m_data[i] = music; break; } } }public static void UpdateMusicRate(int id, int rating)
{ for (int i = 0; i < data.Count; ++i) { if (data[i].Id == id) { m_data[i].Rating = rating; break; } } }}