C#设计模式之Template模板方法模式实现ASP.NET自定义控件 密码强度检测功能

时间:2019-04-06
本文章向大家介绍C#设计模式之Template模板方法模式实现ASP.NET自定义控件 密码强度检测功能,主要包括C#设计模式之Template模板方法模式实现ASP.NET自定义控件 密码强度检测功能使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

本文实例讲述了C#设计模式之Template模板方法模式实现ASP.NET自定义控件 密码强度检测功能。分享给大家供大家参考,具体如下:

一、理论定义

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

二、应用举例

需求描述: ASP.NET自定义控件有很多通用的属性和事件, 通过继承System.Web.UI.WebControls.WebControl类,可以实现自定义控件。

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

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

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

三、具体编码

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{width: 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测试。

附:完整实例代码点击此处本站下载

PS:这里再为大家提供两款相关在线工具供大家参考使用:

密码安全性在线检测:
http://tools.jb51.net/password/my_password_safe

高强度密码生成器:
http://tools.jb51.net/password/CreateStrongPassword

在线随机数字/字符串生成工具:
http://tools.jb51.net/aideddesign/suijishu

更多关于C#相关内容还可查看本站专题:《C#数据结构与算法教程》、《C#窗体操作技巧汇总》、《C#常见控件用法教程》、《WinForm控件用法总结》、《C#数组操作技巧总结》及《C#面向对象程序设计入门教程

希望本文所述对大家C#程序设计有所帮助。