本文中分享一个实现简单,使用方便的星型评分控件。

一:贴几张测试图片先:

二、星型评分控件的实现:

RatingBar.ascx:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="RatingBar.ascx.cs" Inherits="UserControls.Controls.RatingBar" %>

<style type="text/css">

        .rating {

            float:left;

        }

        .rating:not(:checked) > input {

            position:absolute;

            top:-9999px;

            clip:rect(0,0,0,0);

        }

 

        .rating:not(:checked) > label {

            float:right;

            width:1em;

            padding:0 .1em;

            overflow:hidden;

            white-space:nowrap;

            cursor:pointer;

            font-size:150%;

            line-height:1.2;

            color:#ddd;

            text-shadow:1px 1px #bbb, 2px 2px #666, .1em .1em .2em rgba(0,0,0,.5);

        }

 

        .rating:not(:checked) > label:before {

            content: '★ ';

        }

 

        .rating > input:checked ~ label {

            color: #f70;

            text-shadow:1px 1px #c60, 2px 2px #940, .1em .1em .2em rgba(0,0,0,.5);

        }

 

        .rating:not(:checked) > label:hover,

        .rating:not(:checked) > label:hover ~ label {

            color: gold;

            text-shadow:1px 1px goldenrod, 2px 2px #B57340, .1em .1em .2em rgba(0,0,0,.5);

        }

 

        .rating > input:checked + label:hover,

        .rating > input:checked + label:hover ~ label,

        .rating > input:checked ~ label:hover,

        .rating > input:checked ~ label:hover ~ label,

        .rating > label:hover ~ input:checked ~ label {

            color: #ea0;

            text-shadow:1px 1px goldenrod, 2px 2px #B57340, .1em .1em .2em rgba(0,0,0,.5);

        }

 

        .rating > label:active {

            position:relative;

            top:2px;

            left:2px;

        }

    </style>

<span class="rating">

    <input type="radio" id="star5" name="rating" value="5" runat="server" /><label for="<%=this.ClientID + "_star5"%>"

        title="5分">5 stars</label>

    <input type="radio" id="star4" name="rating" value="4" runat="server" /><label for="<%=this.ClientID + "_star4"%>"

        title="4分">4 stars</label>

    <input type="radio" id="star3" name="rating" value="3" runat="server" /><label for="<%=this.ClientID + "_star3"%>"

        title="3分">3 stars</label>

    <input type="radio" id="star2" name="rating" value="2" runat="server" /><label for="<%=this.ClientID + "_star2"%>"

        title="2分">2 stars</label>

    <input type="radio" id="star1" name="rating" value="1" runat="server" /><label for="<%=this.ClientID + "_star1"%>"

        title="1分">1 star</label>

</span>

RatingBar.ascx.cs:

using System;

 

namespace UserControls.Controls

{

    public partial class RatingBar : System.Web.UI.UserControl

    {

        protected void Page_Load(object sender, EventArgs e)

        {

            SyncInterfaceByReadOnlyProperties();

        }

 

        public Grade Value

        {

            get

            {

                if (star5.Checked)

                {

                    return Grade.Five;

                }

                else if (star4.Checked)

                {

                    return Grade.Four;

                }

                else if (star3.Checked)

                {

                    return Grade.Three;

                }

                else if (star2.Checked)

                {

                    return Grade.Two;

                }

                else if (star1.Checked)

                {

                    return Grade.One;

                }

                else

                {

                    return Grade.Zero;

                }

            }

            set

            {

                star5.Checked = false;

                star4.Checked = false;

                star3.Checked = false;

                star2.Checked = false;

                star1.Checked = false;

                switch (value)

                {

                    case Grade.Five:

                        star5.Checked = true;

                        break;

                    case Grade.Four:

                        star4.Checked = true;

                        break;

                    case Grade.Three:

                        star3.Checked = true;

                        break;

                    case Grade.Two:

                        star2.Checked = true;

                        break;

                    case Grade.One:

                        star1.Checked = true;

                        break;

                    default:

                        break;

                }

            }

        }

 

        public bool ReadOnly

        {

            set

            {

                this.ViewState["ReadOnly"] = value;

                SyncInterfaceByReadOnlyProperties();

            }

            get

            {

                object obj = this.ViewState["ReadOnly"];

                if (obj == null)

                {

                    return false;

                }

                else

                {

                    return (bool)obj;

                }

            }

        }

 

        private void SyncInterfaceByReadOnlyProperties()

        {

            if (this.ReadOnly)

            {

                star1.Attributes.Add("disabled", "disabled");

                star2.Attributes.Add("disabled", "disabled");

                star3.Attributes.Add("disabled", "disabled");

                star4.Attributes.Add("disabled", "disabled");

                star5.Attributes.Add("disabled", "disabled");

            }

            else

            {

                star1.Attributes.Remove("disabled");

                star2.Attributes.Remove("disabled");

                star3.Attributes.Remove("disabled");

                star4.Attributes.Remove("disabled");

                star5.Attributes.Remove("disabled");

            }

        }

    }

    public enum Grade

    {

        Zero = 0,

        One = 1,

        Two = 2,

        Three = 3,

        Four = 4,

        Five = 5

    }

}

三、控件使用演示:

为RatingBar控件赋值:

RatingBar1.Value = Grade.Three;

打印RatingBar控件的值:

ClientScript.RegisterStartupScript(this.GetType(), null, string.Format("<script>alert('{0}');</script>", RatingBar1.Value));

RatingBar控件的ReadOnly属性(属性值取反):

RatingBar1.ReadOnly = !RatingBar1.ReadOnly;

Web用户控件开发--星型评分控件的更多相关文章

  1. UWP开发---DIY星级评分控件

    一,需求来源 在开发韩剧TV UWP过程中,遇到了星级评分的控件问题,在安卓和html中很容易用现有的轮子实现星级评分,搜索了一下目前UWP还未有相关文章,在WPF的一篇文章中使用Photo shop ...

  2. iOS- 非整星的评分控件(支持小数)

    概述 订单评论里实现星级评分控件: 简单整星评价与非整星的精评价. 详细 代码下载:http://www.demodashi.com/demo/10711.html 现在很多应用都有评分功能. 有了订 ...

  3. WinForm控件开发总结目录

    WinForm控件开发总结(一)------开篇 WinForm控件开发总结(二)------使用和调试自定义控件 WinForm控件开发总结(三)------认识WinForm控件常用的Attrib ...

  4. Atitit.hybrid混合型应用 浏览器插件,控件的实现方式 浏览器运行本地程序的解决方案大的总结---提升用户体验and开发效率..

    Atitit.hybrid混合型应用 浏览器插件,控件的实现方式 浏览器运行本地程序的解决方案大的总结---提升用户体验and开发效率.. 1. hybrid App 1 1.1. Hybrid Ap ...

  5. Android星星评分控件RatingBar的使用

    在Android的开发中,有一个叫做评分控件RatingBar,我们可以使用该控件做等级划分.评分等作用,星星形状显示,也可以半星级别,我们来看一下评分控件如何使用. 布局文件中定义控件以及属性,这里 ...

  6. VC2005从开发MFC ActiveX ocx控件到发布到.net网站的全部过程

      开篇语:最近在弄ocx控件发布到asp.net网站上使用,就是用户在使用过程中,自动下载安装ocx控件.(此文章也是总结了网上好多人写的文章,我只是汇总一下,加上部分自己的东西,在这里感谢所有在网 ...

  7. 最全的基于MFC的ActiveX控件开发教程

    浏览器插件之ActiveX开发(一) 一般的Web应用对于浏览器插件能不使用的建议尽量不使用,因为其涉及到安全问题以及影响用户安装(或自动下载注册安装)体验问题.在有特殊需求(如涉及数据安全的金融业务 ...

  8. ActiveX控件开发

    VC2005从开发MFC ActiveX ocx控件到发布到.net网站的全部过程 开篇语:最近在弄ocx控件发布到asp.net网站上使用,就是用户在使用过程中,自动下载安装ocx控件.(此文章也是 ...

  9. MFC-[转]基于MFC的ActiveX控件开发

    作者:lidan | 出处:博客园 | 2012/3/13 16:10:34 | 阅读22次 ActiveX 控件是基于组件对象模型 (COM) 的可重用软件组件,广泛应用于桌面及Web应用中.在VC ...

随机推荐

  1. Android studio 将 Module 打包成 Jar 包

    整理记录 AndroidStudio 把一个 module 项目打包成 jar 包. 一.默认自动生成的 jar 包 众所周知 android studio 会在library所依赖的 app运行 或 ...

  2. 微信小程序 - 骨架屏

    骨架屏 - “与其等待网络加载,不如提前给点暗示” 注:不适用复杂交互效果 演示 示例解释以及使用全在index.wxml中,观看需了解组件使用. 示例下载:微信小程序-骨架屏演示

  3. word2013总是出现未响应卡一下如何解决?

    最近在记笔记,word很烦很烦,总是会卡一下,过一会卡一下.本来以为是自动保存后来发现跟自动保存没有关系. 解决方法:禁用硬件图形加速就好了,不行的话再在硬件加速下面有个"使用子像素定位平滑 ...

  4. html中文显示乱码的处理方法

    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> 1. ht ...

  5. 【Zookeeper】源码分析之持久化(一)之FileTxnLog

    一.前言 前一篇已经分析了序列化,这篇接着分析Zookeeper的持久化过程源码,持久化对于数据的存储至关重要,下面进行详细分析. 二.持久化总体框架 持久化的类主要在包org.apache.zook ...

  6. 【PMP】财务测量指标ROI、PBP、NPV、IRR、BCR

    各指标说明: 1.投资回报率(ROI) 定义:是指通过投资而应返回的价值,即企业从一项投资活动中得到的经济回报. 1.1 年平均利润相等的情况下 公式:ROI=年平均利润/投资额 案例: 1.2 年平 ...

  7. tablib把数据导出为Excel、JSON、CSV等格式的Py库(写入数据并导出exl)

    #tablib把数据导出为Excel.JSON.CSV等格式的Py库 #python 3 import tablib #定义列标题 headers = ('1列', '2列', '3列', '4列', ...

  8. 【MySQL】局域网内:在一台电脑访问另一台电脑的mysql数据库

    1. 假设192.168.1.3为服务器2. 首先在ip为192.168.1.103的机子上能够ping 通运行->cmd>ping 192.168.1.3检查是否能ping通3. 把两台 ...

  9. Ios中checkBox

    //使用tableview来进行布局checkBox.便于全选,全不选//radiobutton 适合用RadioButton #import <UIKit/UIKit.h> @inter ...

  10. Java中存储金额用什么数据类型?

    转自:https://blog.csdn.net/u011277123/article/details/70214630 很早之前, 记得一次面试, 面试官问存储金钱用什么数据类型? 当时只知道8种数 ...