zoukankan      html  css  js  c++  java
  • C#设计模式系列 7 Template 模板方法模式之ASP.NET自定义控件 密码强度检测

       1.理论定义

            模板方法模式 预先定义实现了一些基本属性和方法,需要重新计算的部分,通过子类去重写 或  增加新方法来实现。

       2.应用举例

              需求描述: ASP.NET自定义控件有很多通用的属性和事件, 通过继承

                             System.Web.UI.WebControls.WebControl类,可以实现自定义控件。

                             WebControl拥有控件基本的方法和事件,让我们定义控件时,可以站在巨人的肩上,

                             避免重复造轮子。WebControl就相当于一个模板,改变模板的属性,或者往模板里面加东西,显示的内容就不一样。

                             密码强度检测的例子,是通过修改Strength 属性,来控制密码的强度。

       3.具体编码

             1.一个 密码强度的枚举

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace Com.Design.Gof.Template
    {
        /// <summary>
        /// 密码强度枚举属性
        /// </summary>
        public enum StrengthOption
        {
            VeryLow=1,//很差
            Normer=2,//一般
            Good=3,//良好
            Perfect=4//非常棒,非常强,极佳
        }
    }

    2.密码强度 自定义控件

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Linq;
    using System.Text;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    [assembly: TagPrefix("Com.Design.Gof.Template", "asp")]
    namespace Com.Design.Gof.Template
    {
        [DefaultProperty("Text")]
        [ToolboxData("<{0}:PasswdStrength runat=server></{0}:PasswdStrength>")]
        public class PasswdStrength : WebControl
        {
            /// <summary>
            /// 当前密码强度
            /// </summary>
            [Bindable(true)]
            [Category("Appearance")]
            [DefaultValue(StrengthOption.VeryLow)]
            [Localizable(true)]
    
            public StrengthOption Strength
            {
                get
                {
                    object bag = ViewState["StrengthOption"];
                    if (bag == null) {
                        return StrengthOption.VeryLow;
                    }
                    return (StrengthOption)ViewState["StrengthOption"];
                }
                set
                {
                    ViewState["StrengthOption"] = value;
                }
            }
            protected override void RenderContents(HtmlTextWriter output)
            {
                string css = "";
                switch (Strength) {
                    case StrengthOption.VeryLow: css = "bg1"; break;
                    case StrengthOption.Normer: css = "bg2"; break;
                    case StrengthOption.Good: css = "bg3"; break;
                    case StrengthOption.Perfect: css = "bg4"; break;
                    default:  break;
                }
                output.Write("<div class='" + css + "'></div>");
            }
        }
    }

    3.ASPX页面调用控件

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Template_PasswdStrength.aspx.cs" Inherits="Com.Design.Gof.Test.Web.Template_PasswdStrength" %>
    <%@ Register Assembly="Com.Design.Gof" Namespace="Com.Design.Gof.Template" TagPrefix="asp" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title></title>
        <style type="text/css">
    div{ 180px; height: 7px; border-left: 1px solid rgb(255, 117, 6); margin-left: 5px; margin-top:10px}
    div.bg1{background: url("/images/pwd.png") no-repeat scroll 100% 0% transparent; }
    div.bg2{background: url("/images/pwd.png") no-repeat scroll 100% 32% transparent; }
    div.bg3{background: url("/images/pwd.png") no-repeat scroll 100% 65% transparent;  } 
    div.bg4{background: url("/images/pwd.png") no-repeat scroll 100% 100% transparent; }
        </style>
    </head>
    <body>
     
      <h3>密码强度四种情况,Strength是asp:PasswdStrength的控件的自定义属性</h3>
        <p>非常弱</p>
       <asp:PasswdStrength ID="PasswdStrength1" runat="server" />
         <p>一般</p>
       <asp:PasswdStrength Strength=Normer ID="PasswdStrength2" runat="server" />
         <p>良好</p>
    
       <asp:PasswdStrength Strength=Good  ID="PasswdStrength3" runat="server" />
         <p>很强</p>
       <asp:PasswdStrength Strength=Perfect  ID="PasswdStrength4" runat="server" />
    </body>
    </html>

    4.运行结果

    5.总结

    自定义控件知识

      附件里面包括了程序源码。也包括其他项目的测试,有控制台,有web。

      此模式用Com.Design.Gof.Test.Web测试。

      这里是附件下载(Download)

  • 相关阅读:
    HDU1029 Ignatius and the Princess IV
    UVA11039 Building designing【排序】
    UVA11039 Building designing【排序】
    POJ3278 HDU2717 Catch That Cow
    POJ3278 HDU2717 Catch That Cow
    POJ1338 Ugly Numbers(解法二)
    POJ1338 Ugly Numbers(解法二)
    UVA532 Dungeon Master
    UVA532 Dungeon Master
    POJ1915 Knight Moves
  • 原文地址:https://www.cnblogs.com/HCCZX/p/2625424.html
Copyright © 2011-2022 走看看