控件名:Rating
用途和效果:一种全新的等级评分效果
属性:
     MaxRating :最高可以给予的等级高低。没有特别指定时,最多可以到5。
    CurrentRating :”评级”控件初始时的等级高低,默认值是3。
    CssClass : 指定评级控件所套用的样式表。
    StarCssClass
:为每个可以被选择的等级(一般来说,就是那个星星图标),指定样式表。
    WaitingStarCssClass
:为正通过异步方式保存评级数据的等级,指定样式表。
    FilledStarCssClass :为一个已经被选择的等级,指定样式表。
    EmptyStarCssClass :为一个沿未被选择的等级,指定样式表。
    AutoPostBack :当单击”评级”这个控件时,是否回送。默认不回送。
    OnChanged
:在完成评级之后,触发”客户端回调”事件以便执行某一段程序代码。

Rating控件提供一个具有特殊效果的等级,而且以图片的样式展现的。

1.托放一个label用来现实评分信息。

属性列表:
MaxRating :最高可以给予的等级高低
CurrentRating :当前等级
CssClass : 指定评级控件所套用的样式表。
StarCssClass :星的样式
WaitingStarCssClass :更改等级时星的显示样式
FilledStarCssClass :设置等级后星的显示样式
EmptyStarCssClass :没有选中时等级的显示样式
AutoPostBack :是否支持页面回发
OnChanged :等级变化时触发的事件
Tag:为事件传递的参数

2.拖放一个Rating控件,设置属性。

常见问题和使用技巧:
常见问题:可不可以不使用Rating控件的自动异步回调功能,而是随着表单一起传回服务器?
使用技巧:int rating=myRatingID.CurrentRating;可以获得当前评级。

3.其实该控件的主要效果要考css来完成,编写css样式。注意四个css都必须有,否则会出错的。
4.编写后台处理代码。
this.Rating1.CurrentRating.ToString()将返回当前选择的”星“的个数

实例解析一、简单使用实例(必须为此控件添加样式和图片不然运行错误)
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!”;
}

protected void Rating1_Changed(object sender,
AjaxControlToolkit.RatingEventArgs e) 
//如果将AutoPostBack设置成true则这个事件将在选择改变的时候激发
eg:
aspx页面:
<head runat=”server”>
    <title>无标题页</title>
   
       <style type=”text/css”>
        .RatingStar
        {
            font-size:0pt;
            ;
            height:12px;
            marging:0px;
            padding:0px;
            cursor:pointer;
            display:block;
            background-repeat:no-repeat;
        }
       
        .filledRatingStar
        {
           
background-image:url(img/AJAXEnabledWebSite105/images/FilledStar.png);       
        }
       
        .emptyRatingStar
        {
           
background-image:url(img/AJAXEnabledWebSite105/images/EmptyStar.png);       
        }
       
        .savedRatingStar
        {
           
background-image:url(img/AJAXEnabledWebSite105/images/SavedStar.png);
        }
    </style>
   
   
</head>
<body style=”text-align: center”>
    <form id=”form1″ runat=”server”>
    <div>
        <asp:ScriptManager ID=”ScriptManager1″ runat=”server”>
        </asp:ScriptManager>
   
    </div>
        <asp:UpdatePanel ID=”UpdatePanel1″ runat=”server”>
            <ContentTemplate>
                <asp:Label ID=”Label1″ runat=”server”
Text=”Label”></asp:Label> <br />
                <cc1:rating id=”Rating1″ runat=”server”
MaxRating=”10″ AutoPostBack=”True” EmptyStarCssClass=”emptyRatingStar”
FilledStarCssClass=”filledRatingStar” OnChanged=”Rating1_Changed”
StarCssClass=”RatingStar”
WaitingStarCssClass=”savedRatingStar”></cc1:rating>
                <asp:Button ID=”Button1″ runat=”server”
OnClick=”Button1_Click” Text=”Button” />
            </ContentTemplate>
        </asp:UpdatePanel>
    </form>
</body>

}

cs页面:
  protected void Button1_Click(object sender, EventArgs e)
    {
        this.Label1.Text = “您投了” +
this.Rating1.CurrentRating.ToString() + “颗星!”;
    }
    protected void Rating1_Changed(object sender,
AjaxControlToolkit.RatingEventArgs e)
    {
         this.Label1.Text = “您投了” +
this.Rating1.CurrentRating.ToString() + “颗星!”;
    }

实例解析二、和数据库相关联的实例(两张图片见附件)
<%@ Page Language=”C#” %>


<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN”
“;

效果:图片 1

<script runat=”server”>
protected void rating_Changed(object sender, RatingEventArgs e)
{
MusicData.UpdateMusicRate(int.Parse(e.Tag), int.Parse(e.Value));
}
</script>

图片 2

<html xmlns=”;
<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);
}

http://www.bcsnjd.cn

[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;
}
}
}

最新推出:

}

 

ASP.NET快速入门系列视频讲座

 

张孝祥java视频系列教程

 

更多的教程不用下载直接在线观看