Web用户控件开发--星型评分控件

简介:

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

一:贴几张测试图片先:

image

image

image

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

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;
作者: 韩兆新
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
分类:  [06]ASP.NET相关
标签:  ASP.NET

本文转自韩兆新博客博客园博客,原文链接:http://www.cnblogs.com/hanzhaoxin/p/4058247.html,如需转载请自行联系原作者
目录
相关文章
|
15天前
|
监控 JavaScript 前端开发
《理解 WebSocket:Java Web 开发的实时通信技术》
【4月更文挑战第4天】WebSocket是Java Web实时通信的关键技术,提供双向持久连接,实现低延迟、高效率的实时交互。适用于聊天应用、在线游戏、数据监控和即时通知。开发涉及服务器端实现、客户端连接及数据协议定义,注意安全、错误处理、性能和兼容性。随着实时应用需求增加,WebSocket在Java Web开发中的地位将更加重要。
|
26天前
|
Web App开发 前端开发 开发工具
介绍Web开发的基础知识
介绍Web开发的基础知识
26 7
|
1天前
|
JSON Java fastjson
Spring Boot 底层级探索系列 04 - Web 开发(2)
Spring Boot 底层级探索系列 04 - Web 开发(2)
11 0
|
1天前
|
安全 编译器 PHP
PHP 8.1版本发布:引领Web开发新潮流
PHP编程语言一直是Web开发的主力军,而最新发布的PHP 8.1版本则为开发者们带来了更多创新和便利。本文将介绍PHP 8.1版本的主要特性,包括更快的性能、新的语言功能和增强的安全性,以及如何利用这些功能来提升Web应用程序的质量和效率。
|
4天前
|
PHP
web简易开发——通过php与HTML+css+mysql实现用户的登录,注册
web简易开发——通过php与HTML+css+mysql实现用户的登录,注册
|
4天前
|
前端开发 数据挖掘 API
使用Python中的Flask框架进行Web应用开发
【4月更文挑战第15天】在Python的Web开发领域,Flask是一个备受欢迎的轻量级Web框架。它简洁、灵活且易于扩展,使得开发者能够快速地构建出高质量的Web应用。本文将深入探讨Flask框架的核心特性、使用方法以及在实际开发中的应用。
|
13天前
|
安全 前端开发 Java
Java Web开发知识点学习总结
Java Web开发涉及Java基础、Servlet、JSP、数据库操作(SQL+JDBC)、MVC设计模式、Spring框架、Hibernate ORM、Web服务(SOAP&RESTful)、安全认证(HTTP Basic/Digest/OAuth)及性能优化(缓存、异步、负载均衡)。
15 3
|
16天前
|
XML JSON JavaScript
使用JSON和XML:数据交换格式在Java Web开发中的应用
【4月更文挑战第3天】本文比较了JSON和XML在Java Web开发中的应用。JSON是一种轻量级、易读的数据交换格式,适合快速解析和节省空间,常用于API和Web服务。XML则提供更强的灵活性和数据描述能力,适合复杂数据结构。Java有Jackson和Gson等库处理JSON,JAXB和DOM/SAX处理XML。选择格式需根据应用场景和需求。
|
20天前
|
前端开发 JavaScript 数据管理
描述一个使用Python开发Web应用程序的实际项目经验,包括所使用的框架和技术栈。
使用Flask开发Web应用,结合SQLite、Flask-SQLAlchemy进行数据管理,HTML/CSS/JS(Bootstrap和jQuery)构建前端。通过Flask路由处理用户请求,模块化代码提高可维护性。unittest进行测试,开发阶段用内置服务器,生产环境可选WSGI服务器或容器化部署。实现了用户注册登录和数据管理功能,展示Python Web开发的灵活性和效率。
14 4
|
26天前
|
API
2024常用Web支付开发讲解教程
本教程为web支付开发,讲解了最常用的两钟支付:支付宝支付和微信支付,服务器配置和API对接,学完本课程可以学会微信支付、和支付宝支付开发。
18 2
2024常用Web支付开发讲解教程

热门文章

最新文章